forked from mozilla/bedrock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
444 lines (394 loc) · 11.8 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict';
const gulp = require('gulp');
const cached = require('gulp-cached');
const filter = require('gulp-filter');
const log = require('fancy-log');
const colors = require('ansi-colors');
const gulpif = require('gulp-if');
const sass = require('gulp-sass');
const cleanCSS = require('gulp-clean-css');
const uglify = require('gulp-uglify');
const concat = require('gulp-concat');
const sourcemaps = require('gulp-sourcemaps');
const del = require('del');
const karma = require('karma');
const eslint = require('gulp-eslint');
const gulpStylelint = require('gulp-stylelint');
const gulpJsonLint = require('gulp-jsonlint');
const argv = require('yargs').argv;
const browserSync = require('browser-sync').create();
const merge = require('merge-stream');
const staticBundles = require('./media/static-bundles.json');
// directory for building LESS, SASS, and bundles
const buildDir = 'static_build';
// directory for the final assets ready for consumption
const finalDir = 'static_final';
const lintPathsJS = [
'media/js/**/*.js',
'!media/js/libs/*.js',
'tests/unit/spec/**/*.js',
'gulpfile.js'
];
const lintPathsCSS = [
'media/css/**/*.scss',
'media/css/**/*.css',
'!media/css/libs/*'
];
const lintPathsJSON = [
'bedrock/base/templates/includes/structured-data/**/*.json'
];
const cachedOpts = {
optimizeMemory: true
};
global.watching = false;
// gulp build --production
const production = !!argv.production;
const allBundleFiles = (fileType, fileExt) => {
let allFiles = [];
staticBundles[fileType].forEach(bundle => {
bundle.files.forEach(bFile => {
if (bFile.endsWith(fileExt)) {
allFiles.push(bFile);
}
});
});
return allFiles;
};
const handleError = task => {
return err => {
log.warn(colors.bold(colors.red(`[ERROR] ${task}:`)), colors.red(err));
};
};
const bundleCssFiles = bundle => {
let bundleFilename = `css/BUNDLES/${bundle.name}.css`;
if (global.watching) {
log.info(`building: ${bundleFilename}`);
}
let cssFiles = bundle.files.map(fileName => {
if (!fileName.endsWith('.css')) {
return fileName.replace(/\.scss$/i, '.css');
}
return fileName;
});
return gulp.src(cssFiles, {base: finalDir, 'cwd': finalDir})
.pipe(concat(bundleFilename))
.pipe(gulp.dest(finalDir));
};
const bundleJsFiles = bundle => {
let bundleFilename = `js/BUNDLES/${bundle.name}.js`;
if (global.watching) {
log.info(`building: ${bundleFilename}`);
}
return gulp.src(bundle.files, {base: finalDir, cwd: finalDir})
.pipe(gulpif(!production, sourcemaps.init()))
.pipe(concat(bundleFilename))
.pipe(gulpif(!production, sourcemaps.write({
'includeContent': true
})))
.pipe(gulp.dest(finalDir));
};
/***********************
* Start Tasks
*/
/**
* Delete the static_build directory and start fresh.
*/
function clean(cb) {
del([buildDir, finalDir]).then(() => {
cb();
});
}
/**
* Copy assets from various sources into the static_build dir for processing.
*/
function assetsCopy() {
return merge([
// SASS and LESS go to build dir
gulp.src([
'media/**/*.scss',
'node_modules/@mozilla-protocol/core/**/*',
'!node_modules/@mozilla-protocol/core/*'])
.pipe(gulpif(global.watching, cached('all', cachedOpts)))
.pipe(gulp.dest(buildDir)),
// Everything else goes to final dir
gulp.src([
'media/**/*',
'!media/**/*.scss',
'node_modules/@mozilla-protocol/core/**/*',
'!node_modules/@mozilla-protocol/core/**/*.scss',
'!node_modules/@mozilla-protocol/core/*'])
.pipe(gulpif(global.watching, cached('all', cachedOpts)))
.pipe(gulp.dest(finalDir)),
]);
}
/**
* Find all SASS files from bundles in the static_build directory and compile them.
*/
function sassCompileAllFiles() {
return gulp.src(allBundleFiles('css', '.scss'), { base: buildDir, cwd: buildDir })
.pipe(gulpif(!production, sourcemaps.init()))
.pipe(sass({
sourceComments: !production,
outputStyle: production ? 'compressed' : 'nested'
}).on('error', handleError('SASS')))
.pipe(gulpif(!production, sourcemaps.write({
'includeContent': true
})))
.pipe(gulp.dest(finalDir));
}
/**
* Watch and only compile those SASS files that have changed
*/
function sassWatch(cb) {
const sassWatcher = gulp.watch(buildDir + '/css/**/*.scss', {base: buildDir});
sassWatcher.on('change', function(path) {
return gulp.src(path, {base: buildDir})
// filter out internal imports (files starting with "_" )
.pipe(filter(file => {
return !file.relative.startsWith('_');
}))
.pipe(sourcemaps.init())
.pipe(sass({
sourceComments: !production,
outputStyle: production ? 'compressed' : 'nested'
}).on('error', handleError('SASS')))
.pipe(sourcemaps.write({
'includeContent': true
}))
.pipe(gulp.dest(finalDir));
});
cb();
}
/**
* Combine the CSS files after SASS/LESS compilation into bundles
* based on definitions in the `media/static-bundles.json` file.
*/
function cssCompileBundles() {
return merge(staticBundles.css.map(bundleCssFiles));
}
/**
* Watch for changes to CSS files in the `static_final` directory
* and recompile bundles when necessary.
*/
function cssWatch(cb) {
const cssWatcher = gulp.watch(finalDir + '/css/**/*.css');
cssWatcher.on('change', function(path) {
let modBundles = staticBundles.css.filter(bundle => {
let contains = false;
bundle.files.every(filename => {
let cssfn = filename.replace(/\.scss$/i, '.css');
if (path.endsWith(cssfn)) {
contains = true;
return false;
}
return true;
});
return contains;
});
if (modBundles) {
modBundles.map(bundleCssFiles);
browserSync.reload();
}
});
cb();
}
/**
* Combine the JS files into bundles
* based on definitions in the `media/static-bundles.json` file.
*/
function jsCompileBundles() {
return merge(staticBundles.js.map(bundleJsFiles));
}
/**
* Watch for changes to JS files in the `static_final` directory
* and recompile bundles when necessary.
*/
function jsWatch(cb) {
const jsWatcher = gulp.watch(finalDir + '/js/**/*.js');
jsWatcher.on('change', function(path) {
let modBundles = staticBundles.js.filter(bundle => {
let contains = false;
bundle.files.every(filename => {
if (path.endsWith(filename)) {
contains = true;
return false;
}
return true;
});
return contains;
});
if (modBundles) {
modBundles.map(bundleJsFiles);
browserSync.reload();
}
});
cb();
}
/**
* Minify all of the CSS files after compilation.
*/
function cssMinify() {
return gulp.src(finalDir + '/css/**/*.css', {base: buildDir})
.pipe(cleanCSS({
compatibility: {
properties: {
iePrefixHack: true, // controls keeping IE prefix hack used in oldIE.scss
},
}
}).on('error', handleError('CLEANCSS')))
.pipe(gulp.dest(finalDir));
}
/**
* Minify all of the JS files after compilation.
*/
function jsMinify() {
return gulp.src(finalDir + '/js/**/*.js', {base: buildDir})
.pipe(uglify({ie8: true}).on('error', handleError('UGLIFY')))
.pipe(gulp.dest(finalDir));
}
/**
* Run the JS test suite.
*/
function jsTest(cb) {
new karma.Server({
configFile: `${__dirname}/tests/unit/karma.conf.js`,
singleRun: true
}, cb).start();
}
gulp.task('js:test', jsTest);
/**
* Run eslint style check on all JS.
*/
function jsLint() {
return gulp.src(lintPathsJS)
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
}
gulp.task('js:lint', jsLint);
/**
* Run CSS style check on all CSS.
*/
function cssLint() {
return gulp.src(lintPathsCSS)
.pipe(gulpStylelint({
reporters: [{
formatter: 'string',
console: true
}],
debug: true
}));
}
gulp.task('css:lint', cssLint);
/**
* Run JSON lint on JSON files
*/
function jsonLint() {
return gulp.src(lintPathsJSON)
.pipe(gulpJsonLint())
.pipe(gulpJsonLint.reporter());
}
gulp.task('json:lint', jsonLint);
/**
* Watch for changes in the `media` directory and copy changed files to
* either `static_build` or `static_final` depending on the file type.
*/
function assetsWatch(cb) {
const assetsWatcher = gulp.watch(['media/**/*', '!media/**/*.scss'], {base: 'media'});
const cssWatcher = gulp.watch('media/**/*.scss', {base: 'media'});
// send everything but scss to the final dir
assetsWatcher.on('change', function(path) {
return gulp.src(path, {base: 'media'})
.pipe(cached('all', cachedOpts))
.pipe(gulp.dest(finalDir));
});
// send scss to the build dir
cssWatcher.on('change', function(path) {
return gulp.src(path, {base: 'media'})
.pipe(cached('all', cachedOpts))
.pipe(gulp.dest(buildDir));
});
cb();
}
/**
* Start the browser-sync daemon for local development.
*/
function browserSyncTask(cb) {
const proxyURL = process.env.BS_PROXY_URL || 'localhost:8080';
const openBrowser = !(process.env.BS_OPEN_BROWSER === 'false');
browserSync.init({
port: 8000,
proxy: proxyURL,
open: openBrowser,
notify: true,
reloadDelay: 300,
reloadDebounce: 500,
injectChanges: false,
ui: {
port: 8001
},
serveStatic: [{
route: '/media',
dir: finalDir
}]
});
cb();
}
/**
* Reload tasks used by `gulp watch`.
*/
function reloadBrowser(cb) {
browserSync.reload();
cb();
}
const reloadSass = gulp.series(
sassCompileAllFiles,
reloadBrowser
);
// --------------------------
// DEV/WATCH TASK
// --------------------------
function devWatch(cb) {
global.watching = true;
// --------------------------
// watch:sass library files
// --------------------------
gulp.watch(buildDir + '/css/**/_*.scss', reloadSass);
// --------------------------
// watch:js
// --------------------------
gulp.watch('media/js/**/*.js', jsLint);
// --------------------------
// watch:html
// --------------------------
gulp.watch('bedrock/*/templates/**/*.html', reloadBrowser);
log.info(colors.green('Watching for changes...'));
cb();
}
/**
* Build all assets in prep for production.
* Pass the `--production` flag to turn off sourcemaps.
*/
const buildTask = gulp.series(
clean,
assetsCopy,
sassCompileAllFiles,
gulp.parallel(jsCompileBundles, cssCompileBundles),
gulp.parallel(jsMinify, cssMinify)
);
gulp.task('build', buildTask);
const defaultTask = gulp.series(
clean,
assetsCopy,
sassCompileAllFiles,
gulp.parallel(jsCompileBundles, cssCompileBundles),
assetsWatch,
browserSyncTask,
gulp.parallel(sassWatch, cssWatch, jsWatch),
devWatch
);
gulp.task('default', defaultTask);
module.exports = defaultTask;