-
Notifications
You must be signed in to change notification settings - Fork 334
/
compile-assets.js
126 lines (117 loc) · 3.83 KB
/
compile-assets.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
'use strict'
const gulp = require('gulp')
const configPaths = require('../../config/paths.json')
const sass = require('gulp-sass')
const plumber = require('gulp-plumber')
const postcss = require('gulp-postcss')
const autoprefixer = require('autoprefixer')
const merge = require('merge-stream')
const rollup = require('gulp-better-rollup')
const taskArguments = require('./task-arguments')
const gulpif = require('gulp-if')
const uglify = require('gulp-uglify')
const eol = require('gulp-eol')
const rename = require('gulp-rename')
const cssnano = require('cssnano')
const postcssnormalize = require('postcss-normalize')
const postcsspseudoclasses = require('postcss-pseudo-classes')
// Compile CSS and JS task --------------
// --------------------------------------
// check if destination flag is dist
const isDist = taskArguments.destination === 'dist' || false
const errorHandler = function (error) {
// Log the error to the console
console.error(error.message)
// Ensure the task we're running exits with an error code
this.once('finish', () => process.exit(1))
this.emit('end')
}
// different entry points for both streams below and depending on destination flag
const compileStyleshet = isDist ? configPaths.app + 'assets/scss/govuk-frontend.scss' : configPaths.app + 'assets/scss/app.scss'
const compileOldIeStyleshet = isDist ? configPaths.app + 'assets/scss/govuk-frontend-old-ie.scss' : configPaths.app + 'assets/scss/app-old-ie.scss'
gulp.task('scss:compile', () => {
let compile = gulp.src(compileStyleshet)
.pipe(plumber(errorHandler))
.pipe(sass())
// minify css add vendor prefixes and normalize to compiled css
.pipe(gulpif(isDist, postcss([
autoprefixer,
cssnano,
postcssnormalize
])))
.pipe(gulpif(!isDist, postcss([
autoprefixer,
// Auto-generate 'companion' classes for pseudo-selector states - e.g. a
// :hover class you can use to simulate the hover state in the review app
postcsspseudoclasses
])))
.pipe(gulpif(isDist,
rename({
extname: '.min.css'
})
))
.pipe(gulp.dest(taskArguments.destination + '/css/'))
let compileOldIe = gulp.src(compileOldIeStyleshet)
.pipe(plumber(errorHandler))
.pipe(sass())
// minify css add vendor prefixes and normalize to compiled css
.pipe(gulpif(isDist, postcss([
autoprefixer,
cssnano,
postcssnormalize,
// transpile css for ie https://github.com/jonathantneal/oldie
require('oldie')({
rgba: {filter: true},
rem: {disable: true},
unmq: {disable: true},
pseudo: {disable: true}
// more rules go here
})
])))
.pipe(gulpif(!isDist, postcss([
autoprefixer,
require('oldie')({
rgba: {filter: true},
rem: {disable: true},
unmq: {disable: true},
pseudo: {disable: true}
// more rules go here
})
])))
.pipe(gulpif(isDist,
rename({
extname: '.min.css'
})
))
.pipe(gulp.dest(taskArguments.destination + '/css/'))
return merge(compile, compileOldIe)
})
// Compile js task for preview ----------
// --------------------------------------
gulp.task('js:compile', () => {
return gulp.src([
'!' + configPaths.src + '**/*.test.js',
configPaths.src + '**/*.js'
])
.pipe(rollup({
// Legacy mode is required for IE8 support
legacy: true,
// UMD allows the published bundle to work in CommonJS and in the browser.
format: 'umd'
}))
.pipe(gulpif(isDist, uglify()))
.pipe(gulpif(isDist,
rename({
extname: '.min.js'
})
))
.pipe(eol())
.pipe(gulp.dest(
// output files to dist/components if destination is dist, otherwise copy to packages/
gulpif(
isDist,
taskArguments.destination + '/components/',
taskArguments.destination + '/'
))
)
})