-
Notifications
You must be signed in to change notification settings - Fork 25
/
gulpfile.js
593 lines (543 loc) · 14.9 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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/**
* Configuration for Gulp. Based largely on Sage by Roots.
*/
/**
* Site config
*/
const manifest = require('./assets/manifest.js');
const timestamps = require('./assets/last-edited.json');
/**
* Global modules
*/
const argv = require('minimist')(process.argv.slice(2));
const autoprefixer = require('gulp-autoprefixer');
const beeper = require('beeper');
const browsersync = require('browser-sync').create();
const concat = require('gulp-concat');
const flatten = require('gulp-flatten');
const gulp = require('gulp');
const del = require('del');
const gulpif = require('gulp-if');
const imagemin = require('gulp-imagemin');
const jshint = require('gulp-jshint');
const lazypipe = require('lazypipe');
const merge = require('merge-stream');
const cleancss = require('gulp-clean-css');
const plumber = require('gulp-plumber');
const sass = require('gulp-sass');
const sourcemaps = require('gulp-sourcemaps');
const uglify = require('gulp-uglify');
const rename = require('gulp-rename');
const svgstore = require('gulp-svgstore');
const file = require('gulp-file');
const babel = require('gulp-babel');
const fs = require('fs');
const path_module = require('path');
/**
* Asset paths
*/
const path = {
"base" : {
"source": "assets/",
"dist": "dist/",
},
"scripts" : {
"source": "assets/scripts/",
"dist": "dist/scripts/",
},
"styles" : {
"source": "assets/styles/",
"dist": "dist/styles/",
},
"fonts" : {
"source": "assets/fonts/",
"dist": "dist/fonts/",
},
"images" : {
"source": "assets/images/",
"dist": "dist/images/",
},
"sprite" : {
"source": "assets/sprite/",
"dist": "dist/sprite/",
},
"favicon" : {
"source": "assets/favicon/",
"dist": "dist/favicon/",
},
"modules" : {
"source": "modules/",
}
};
/**
* Disable or enable features
*/
const enabled = {
// disable source maps when `--production`
maps: !argv.production,
// fail styles task on error when `--production`
failStyleTask: argv.production,
// fail due to JSHint warnings only when `--production`
failJSHint: argv.production,
// strip debug statments from javascript when `--production`
stripJSDebug: argv.production
};
/**
* Timestamps
*
* Update asset class timestamp to last-edited.json
*/
const updateTimestamp = (stamp) => {
timestamps[stamp] = Date.now();
return file(
'last-edited.json',
JSON.stringify(timestamps, null, 2),
{src: true}
)
.pipe(gulp.dest('./assets'));
};
/**
* getModules helper function for collecting all modules
*/
const getModules = () => {
return fs.readdirSync(path.modules.source)
.filter(function(module) {
if (!module.includes("_", 0)) {
return fs.statSync(path_module.join('./modules/', module)).isDirectory();
}
});
}
/**
* getModuleJsons helper function for collecting all modules _.json files
*/
var getModuleJsons = () => {
var jsons = [];
var modules = getModules();
for (let i = 0; i < modules.length; i++) {
if (!modules[i].includes("_", 0) && !modules[i].includes(".")) {
jsons.push({
'name': modules[i],
'json': require('./modules/' + modules[i] + '/_.json'),
});
}
}
return jsons;
}
const getAssets = () => {
let manifest_json = {
'js': manifest.js(),
'css': manifest.css(),
}
getModuleJsons().forEach(module => {
if (module.json.css) {
Object.entries(module.json.css).forEach(([target, sources]) => {
if (typeof sources !== 'undefined' && sources.length > 0) {
sources.forEach(source => {
manifest_json.css[target].push('modules/' + module.name + '/' + source);
});
}
});
}
if (module.json.js) {
Object.entries(module.json.js).forEach(([target, sources]) => {
if (typeof sources !== 'undefined' && sources.length > 0) {
sources.forEach(source => {
manifest_json.js[target].push('modules/' + module.name + '/' + source);
});
}
});
}
});
return manifest_json;
}
/**
* Build variable for assets
*
* Create array with information of assets:
* {
* 'name': main.css,
* 'globs': 'assets/main.scss,assets/print.scss'
* }
*/
var buildAssets = (buildFiles) => {
let result = [];
for (let buildFile in buildFiles) {
// set correct asset paths
/*for (i = 0; i < buildFiles[buildFile].length; i++) {
buildFiles[buildFile][i] = path.base.source + buildFiles[buildFile][i];
}*/
result.push({
'name': buildFile,
'globs': buildFiles[buildFile],
});
}
return result;
};
var jsAssets = buildAssets(getAssets().js);
var cssAssets = buildAssets(getAssets().css);
/**
* Process: CSS
*
* SASS, autoprefix, sourcemap styles.
*
* gulp.src(cssFiles)
* .pipe(cssTasks('main.css')
* .pipe(gulp.dest(path.base.dist + 'styles'))
*/
const cssTasks = (filename) => {
return lazypipe()
// catch syntax errors (don't break pipe)
.pipe(function() {
return gulpif(!enabled.failStyleTask, plumber());
})
// init sourcemaps
.pipe(function() {
return gulpif(enabled.maps, sourcemaps.init());
})
// sass
.pipe(function() {
return gulpif('*.scss', sass({
outputStyle: 'nested', // libsass doesn't support expanded yet
precision: 8,
includePaths: ['.'],
errLogToConsole: !enabled.failStyleTask
}));
})
// autoprefix
.pipe(autoprefixer, {
"grid": "no-autoplace"
})
// combine files
.pipe(concat, filename)
// minify
.pipe(cleancss, {})
// build sourcemaps
.pipe(function() {
return gulpif(enabled.maps, sourcemaps.write('.', {
sourceRoot: path.styles.source
}));
})();
};
/**
* Process: JS
*
* Sourcemap, combine, minimize.
*
* gulp.src(jsFiles)
* .pipe(jsTasks('main.js')
* .pipe(gulp.dest(path.base.dist + 'scripts'))
* ```
*/
const jsTasks = (filename) => {
updateTimestamp('js');
return lazypipe()
// init sourcemaps
.pipe(function() {
return gulpif(enabled.maps, sourcemaps.init());
})
// transpile
.pipe(function() {
return babel({
presets: ["@babel/preset-env", "@babel/preset-react"],
overrides: [{
test: manifest.babelIgnores(),
sourceType: "script",
}],
});
})
// combine files
.pipe(concat, filename)
// minify
.pipe(uglify, {
compress: {
'drop_debugger': enabled.stripJSDebug
}
})
// build sourcemaps
.pipe(function() {
return gulpif(enabled.maps, sourcemaps.write('.', {
sourceRoot: path.scripts.source
}));
})();
};
/**
* Task: Styles
*
* `gulp styles` - Compiles, combines, and optimizes Bower CSS and project CSS.
* By default this task will only log a warning if a precompiler error is
* raised. If the `--production` flag is set: this task will fail outright.
*/
gulp.task('styles', () => {
const merged = merge();
cssAssets = buildAssets(getAssets().css);
// update last-edited.json
updateTimestamp('css');
// process all assets
for (i = 0; i < cssAssets.length; i++) {
let asset = cssAssets[i];
const cssTasksInstance = cssTasks(asset.name);
// handle possible errors
if (!enabled.failStyleTask) {
cssTasksInstance.on('error', function(err) {
console.error(err.message);
this.emit('end');
});
}
// merge
merged.add(
gulp.src(asset.globs, {base: 'styles'})
.pipe(cssTasksInstance)
);
}
return merged
.on('error', function(err) {
beeper();
})
.pipe(gulp.dest(path.styles.dist))
.pipe(gulpif(!argv.q, browsersync.stream({match: '**/*.css'})))
});
/**
* Task: Scripts
*
* `gulp scripts` - Runs JSHint then compiles, combines, and optimizes JS.
*/
gulp.task('scripts', () => {
const merged = merge();
jsAssets = buildAssets(getAssets().js);
// process all assets
for (i = 0; i < jsAssets.length; i++) {
let asset = jsAssets[i];
const jsTasksInstance = jsTasks(asset.name);
//merge
merged.add(
gulp.src(asset.globs, {base: 'scripts'})
.pipe(jsTasksInstance)
);
}
return merged
.on('error', function(err) {
beeper();
console.log(err);
})
.pipe(gulp.dest(path.scripts.dist))
.pipe(gulpif(!argv.q, browsersync.stream({match: '**/*.js'})));
});
/**
* Task: Fonts
*
* `gulp fonts` - Grabs all the fonts and outputs them in a flattened directory
* structure. See: https://github.com/armed/gulp-flatten
*/
gulp.task('fonts', () => {
return gulp.src([path.fonts.source + '**/*'])
// flatten directory structures
.pipe(flatten())
// send to /dist/fonts/
.pipe(gulp.dest(path.fonts.dist))
// browsersync result
.pipe(gulpif(!argv.q, browsersync.stream()));
});
/**
* Task: Images
*
* `gulp images` - Run lossless compression on all the images.
*/
gulp.task('images', () => {
// Gather all image sources to one array
let image_sources = [
path.images.source + '**/*',
path.modules.source + '**/images/*'
];
return gulp
.src(image_sources)
// optimize images
.pipe(
imagemin({
progressive: true,
interlaced: true,
svgoPlugins: [{removeUnknownsAndDefaults: false}, {cleanupIDs: false}, {removeDimensions: true}]
})
)
// send to /dist/images
.pipe(flatten())
.pipe(gulp.dest(path.images.dist))
// browsersync result
.pipe(gulpif(!argv.q, browsersync.stream()));
});
/**
* Task: Svgstore
*
* Create a single sprite.svg file from files in /assets/sprite/.
*/
gulp.task('svgstore', async () => {
updateTimestamp('svg');
// Gather all svg sources to one array
let spriteSources = [];
// Get names of icons in /assets/sprite/ directory for later name comparison
await fs.promises.readdir(path.sprite.source).then(
files => files.forEach(file => spriteSources.push(path.sprite.source + file)),
err => console.error({err})
);
// Add module sprites to spriteSources if icon with the same name is not found
// @todo maybe turn into async function
const dropIns = getModules();
// Get module filenames
for (const module of dropIns) {
await fs.promises.readdir(path.modules.source + module + '/assets/sprite').then(
files => files.forEach(file => spriteSources.push(path.modules.source + module + '/assets/sprite/' + file)),
err => null // directory doesn't exist
);
}
// Make reference with path and filename
let spriteFilenameReference = [];
for (const file of spriteSources) {
spriteFilenameReference[file] = file.replace(/^.*[\\\/]/, '');
}
// Remove duplicates
for (const [path, file] of Object.entries(spriteFilenameReference)) {
for (const [searchFilePath, searchFile] of Object.entries(spriteFilenameReference)) {
if (path !== searchFilePath && file == searchFile && spriteFilenameReference[path] && file !== '.DS_Store') {
delete spriteFilenameReference[searchFilePath];
console.log(`SVG sprite duplicate: ${searchFilePath}`);
}
}
}
return gulp.src(Object.keys(spriteFilenameReference))
// rename SVG IDs by "icon-filename"
.pipe(rename({prefix: 'icon-'}))
// optimize SVG
.pipe(imagemin([
imagemin.svgo({
plugins: [
{ removeViewBox: false },
{ collapseGroups: true },
{ convertColors: {
currentColor: true
}
}
]
})
]))
// store SVG into sprite
.pipe(svgstore())
.pipe(gulp.dest(path.sprite.dist))
// browsersync result
.pipe(gulpif(!argv.q, browsersync.stream()));
});
/**
* Task: JSHint
*
* `gulp jshint` - Lints configuration JSON and project JS.
*/
gulp.task('jshint', () => {
let allJS = [];
for (i = 0; i < jsAssets.length; i++) {
let globsArray = jsAssets[i].globs;
for (j = 0; j < globsArray.length; j++) {
allJS.push(globsArray[j]);
}
}
return gulp.src([
'gulpfile.js'
].concat(allJS))
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish'))
.pipe(gulpif(enabled.failJSHint, jshint.reporter('fail')));
});
/**
* Task: Favicon
*
* `gulp favicon` - Run lossless compression for favicons.
*/
gulp.task('favicon', () => {
return gulp.src([path.favicon.source + '**/*'])
// optimize images
.pipe(imagemin({
progressive: true,
interlaced: true,
svgoPlugins: [
{removeUnknownsAndDefaults: false},
{cleanupIDs: false},
{removeDimensions: true}
]
}))
// send to /dist/favicon
.pipe(gulp.dest(path.favicon.dist));
});
/**
* Task: Clean
*
* `gulp clean` - Deletes the build folder entirely.
*/
gulp.task('clean', () => {
return del(path.base.dist);
});
/**
* Task: Watch
*
* `gulp watch` - Use BrowserSync to proxy your dev server and synchronize code
* changes across devices. Specify the hostname of your dev server at
* `manifest.devUrl`. When a modification is made to an asset, run the
* build step for that asset and inject the changes into the page.
* See: http://www.browsersync.io
*/
gulp.task('watch', () => {
var new_tab = 'local';
if (!argv.q) {
// browsersync changes unless in quiet mode
browsersync.init({
files: [
'{inc,blocks,modules}/**/*.php',
'*.php'
],
proxy: manifest.devUrl(),
snippetOptions: {
whitelist: ['/wp-admin/admin-ajax.php'],
blacklist: ['/wp-admin/**']
},
open: 'local'
});
}
// watch these files
gulp.watch(path.styles.source + '**/*', gulp.task('styles'));
gulp.watch(path.scripts.source + '**/*', gulp.task('scripts'));
gulp.watch(path.fonts.source + '**/*', gulp.task('fonts'));
gulp.watch(path.images.source + '**/*', gulp.task('images'));
gulp.watch(path.sprite.source + '*', gulp.task('svgstore'));
gulp.watch(path.favicon.source + '*', gulp.task('favicon'));
// modules
gulp.watch(path.modules.source + '**/*.scss', gulp.task('styles'));
gulp.watch(path.modules.source + '**/*.js', gulp.task('scripts'));
gulp.watch(path.modules.source + '**/images/*', gulp.task('images'));
gulp.watch(path.modules.source + '**/sprite/*', gulp.task('svgstore'));
gulp.watch([
'gulpfile.js',
'assets/manifest.js',
path.modules.source + '*/_.json'
], () => {
console.error("\n⚠️ Congifuration modified. Restart gulp. ⚠️\n");
beeper();
process.exit();
});
gulp.watch([
path.modules.source + '*',
],
gulp.task('default')
)
});
/**
* Task: Build
*
* `gulp build` - Run all the build tasks but don't clean up beforehand.
* Generally you should be running `gulp` instead of `gulp build`.
*/
gulp.task('build', gulp.series(
gulp.parallel('styles', 'scripts','jshint'),
gulp.parallel('fonts', 'images', 'svgstore', 'favicon')
));
/**
* Task: Default
*
* `gulp` - Run a complete build. To compile for production run `gulp --production`.
*/
gulp.task('default', gulp.series('clean', 'build'));