-
Notifications
You must be signed in to change notification settings - Fork 21
/
gulpfile.js
319 lines (280 loc) · 8.78 KB
/
gulpfile.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
const gulp = require('gulp')
const uglify = require('gulp-uglify')
const del = require('del')
const sass = require('gulp-sass')(require('sass'))
const filelog = require('gulp-filelog')
const include = require('gulp-include')
const colours = require('colors/safe')
const path = require('path')
// Paths
let environment = 'development'
const repoRoot = path.join(__dirname)
const npmRoot = path.join(repoRoot, 'node_modules')
const govukCountryPickerDist = path.join(npmRoot, 'govuk-country-and-territory-autocomplete', 'dist')
const govukToolkitRoot = path.join(npmRoot, 'govuk_frontend_toolkit')
const govukElementsRoot = path.join(npmRoot, 'govuk-elements-sass')
const govukFrontendRoot = path.join(npmRoot, 'govuk-frontend')
const dmToolkitRoot = path.join(npmRoot, 'digitalmarketplace-frontend-toolkit', 'toolkit')
const sspContentRoot = path.join(npmRoot, 'digitalmarketplace-frameworks')
const assetsFolder = path.join(repoRoot, 'app', 'assets')
const staticFolder = path.join(repoRoot, 'app', 'static')
const govukFrontendFontsFolder = path.join(govukFrontendRoot, 'assets', 'fonts')
const govukFrontendImagesFolder = path.join(govukFrontendRoot, 'assets', 'images')
// JavaScript paths
const jsSourceFile = path.join(assetsFolder, 'javascripts', 'application.js')
const jsDistributionFolder = path.join(staticFolder, 'javascripts')
const jsDistributionFile = 'application.js'
const jsPageSpecific = path.join(assetsFolder, 'javascripts', 'page_specific')
// CSS paths
const cssSourceGlob = path.join(assetsFolder, 'scss', 'application*.scss')
const cssDistributionFolder = path.join(staticFolder, 'stylesheets')
// Configuration
const sassOptions = {
development: {
outputStyle: 'expanded',
lineNumbers: true,
includePaths: [
path.join(assetsFolder, 'scss'),
path.join(dmToolkitRoot, 'scss'),
path.join(govukToolkitRoot, 'stylesheets'),
path.join(govukElementsRoot, 'public', 'sass')
],
sourceComments: true,
errLogToConsole: true
},
production: {
outputStyle: 'compressed',
lineNumbers: true,
includePaths: [
path.join(assetsFolder, 'scss'),
path.join(dmToolkitRoot, 'scss'),
path.join(govukToolkitRoot, 'stylesheets'),
path.join(govukElementsRoot, 'public', 'sass')
]
}
}
const uglifyOptions = {
development: {
mangle: false,
output: {
beautify: true,
semicolons: true,
comments: true,
indent_level: 2
},
compress: false
},
production: {
mangle: true
}
}
const logErrorAndExit = function logErrorAndExit (err) {
const printError = function (type, message) {
console.log('gulp ' + colours.red('ERR! ') + type + ': ' + message)
}
printError('message', err.message)
printError('file name', err.fileName)
printError('line number', err.lineNumber)
process.exit(1)
}
gulp.task('clean:js', function () {
return del(path.join(jsDistributionFolder, '**', '*')).then(function (paths) {
console.log('💥 Deleted the following JavaScript files:\n', paths.join('\n'))
})
})
gulp.task('clean:css', function () {
return del(path.join(cssDistributionFolder, '**', '*')).then(function (paths) {
console.log('💥 Deleted the following CSS files:\n', paths.join('\n'))
})
})
gulp.task('clean', gulp.parallel('clean:js', 'clean:css'))
gulp.task('sass', function () {
const stream = gulp.src(cssSourceGlob)
.pipe(filelog('Compressing SCSS files'))
.pipe(
sass(sassOptions[environment]))
.on('error', logErrorAndExit)
.pipe(gulp.dest(cssDistributionFolder))
stream.on('end', function () {
console.log('💾 Compressed CSS saved as .css files in ' + cssDistributionFolder)
})
return stream
})
gulp.task('js', function () {
const stream = gulp.src(jsSourceFile, { sourcemaps: true })
.pipe(filelog('Compressing JavaScript files'))
.pipe(include({ hardFail: true }))
.pipe(uglify(
uglifyOptions[environment]
))
.pipe(gulp.dest(jsDistributionFolder, { sourcemaps: 'maps' }))
stream.on('end', function () {
console.log('💾 Compressed JavaScript saved as ' + path.join(jsDistributionFolder, jsDistributionFile))
})
return stream
})
function copyFactory (resourceName, sourceFolder, targetFolder) {
return function () {
return gulp
.src(path.join(sourceFolder, '**', '*'), { base: sourceFolder })
.pipe(gulp.dest(targetFolder))
.on('end', function () {
console.log('📂 Copied ' + resourceName)
})
}
}
function copyFiletypeFactory (resourceName, sourceFolder, sourceFileExtension, targetFolder) {
return function () {
return gulp
.src(path.join(sourceFolder, '**', '*.' + sourceFileExtension), { base: sourceFolder })
.pipe(gulp.dest(targetFolder))
.on('end', function () {
console.log('📂 Copied ' + resourceName)
})
}
}
gulp.task(
'copy:govuk_frontend_assets:stylesheets',
copyFiletypeFactory(
'stylesheets from GOV.UK Frontend',
govukFrontendRoot,
'scss',
path.join('app', 'assets', 'scss', 'govuk')
)
)
gulp.task(
'copy:dm_toolkit_assets:stylesheets',
copyFactory(
'stylesheets from the Digital Marketplace frontend toolkit',
path.join(dmToolkitRoot, 'scss'),
path.join('app', 'assets', 'scss', 'toolkit')
)
)
gulp.task(
'copy:dm_toolkit_assets:images',
copyFactory(
'images from the Digital Marketplace frontend toolkit',
path.join(dmToolkitRoot, 'images'),
path.join(staticFolder, 'images')
)
)
gulp.task(
'copy:govuk_toolkit_assets:images',
copyFactory(
'images from the GOVUK frontend toolkit',
path.join(govukToolkitRoot, 'images'),
path.join(staticFolder, 'images')
)
)
gulp.task(
'copy:dm_toolkit_assets:templates',
copyFactory(
'templates from the Digital Marketplace frontend toolkit',
path.join(dmToolkitRoot, 'templates'),
path.join('app', 'templates', 'toolkit')
)
)
gulp.task(
'copy:images',
copyFactory(
'image assets from app to static folder',
path.join(assetsFolder, 'images'),
path.join(staticFolder, 'images')
)
)
gulp.task(
'copy:frameworks',
copyFactory(
'frameworks YAML into app folder',
path.join(sspContentRoot, 'frameworks'),
path.join('app', 'content', 'frameworks')
)
)
gulp.task(
'copy:govuk_frontend_assets:fonts',
copyFactory(
'fonts from GOV.UK Frontend assets',
govukFrontendFontsFolder,
path.join(staticFolder, 'fonts')
)
)
gulp.task(
'copy:govuk_frontend_assets:images',
copyFactory(
'images from GOV.UK Frontend assets',
govukFrontendImagesFolder,
path.join(staticFolder, 'images')
)
)
gulp.task(
'copy:country_picker:jsons',
copyFiletypeFactory(
'country picker jsons to static folder',
govukCountryPickerDist,
'json',
staticFolder
)
)
gulp.task(
'copy:country_picker:stylesheets',
copyFiletypeFactory(
'country picker stylesheets to static folder',
govukCountryPickerDist,
'css',
cssDistributionFolder
)
)
gulp.task(
'copy:country_picker_package:javascripts',
copyFiletypeFactory(
'country picker package javascripts to static folder',
govukCountryPickerDist,
'{js,js.map}',
jsDistributionFolder
)
)
gulp.task(
'copy:page_specific:javascripts',
copyFactory(
'page specific javascript to static folder',
jsPageSpecific,
jsDistributionFolder
)
)
gulp.task('set_environment_to_development', function (cb) {
environment = 'development'
cb()
})
gulp.task('set_environment_to_production', function (cb) {
environment = 'production'
cb()
})
gulp.task('copy', gulp.parallel(
'copy:frameworks',
'copy:govuk_toolkit_assets:images',
'copy:dm_toolkit_assets:stylesheets',
'copy:dm_toolkit_assets:images',
'copy:dm_toolkit_assets:templates',
'copy:images',
'copy:govuk_frontend_assets:stylesheets',
'copy:govuk_frontend_assets:fonts',
'copy:govuk_frontend_assets:images',
'copy:country_picker:jsons',
'copy:country_picker:stylesheets',
'copy:country_picker_package:javascripts',
'copy:page_specific:javascripts'
))
gulp.task('compile', gulp.series('copy', gulp.parallel('sass', 'js')))
gulp.task('build:development', gulp.series(gulp.parallel('set_environment_to_development', 'clean'), 'compile'))
gulp.task('build:production', gulp.series(gulp.parallel('set_environment_to_production', 'clean'), 'compile'))
gulp.task('watch', gulp.series('build:development', function () {
const jsWatcher = gulp.watch([path.join(assetsFolder, '**', '*.js')], gulp.series('js'))
const cssWatcher = gulp.watch([path.join(assetsFolder, '**', '*.scss')], gulp.series('sass'))
const dmWatcher = gulp.watch([path.join(npmRoot, 'digitalmarketplace-frameworks', '**')], gulp.series('copy:frameworks'))
const notice = function (event) {
console.log('File ' + event.path + ' was ' + event.type + ' running tasks...')
}
cssWatcher.on('change', notice)
jsWatcher.on('change', notice)
dmWatcher.on('change', notice)
}))