This repository has been archived by the owner on Jul 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgulpfile.js
526 lines (470 loc) · 18 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
'use strict';
require('babel-polyfill');
var gulp = require('gulp');
var gulpLoadPlugins = require('gulp-load-plugins');
var del = require('del');
var lazypipe = require('lazypipe');
var sseries = require('stream-series');
var chalk = require('chalk');
var crypto = require('crypto');
var fs = require('fs');
var hashstore = require('gulp-hashstore');
// Auto load Gulp plugins
const plugins = gulpLoadPlugins({
rename: {
'gulp-util': 'gulpUtil',
'gulp-inject': 'inject',
'gulp-htmlmin': 'htmlmin',
'gulp-htmltidy': 'htmltidy'
}
});
//
// Import task configuration
var config = require('./config/config.js');
var dependencies = require('./config/dependencies.js');
//
// Copy dependencies
function copyDependenciesCss () {
if (dependencies.css.length == 0)
return gulp.src('.');
return gulp.src(dependencies.css)
.pipe(gulp.dest('hugo/static/styles_vendor/'));
}
function copyDependenciesJsHead () {
if (dependencies.jsHead.length == 0)
return gulp.src('.');
return gulp.src(dependencies.jsHead)
.pipe(gulp.dest('hugo/static/scripts_head/'));
}
function copyDependenciesJsFooter () {
if (dependencies.jsFooter.length == 0)
return gulp.src('.');
return gulp.src(dependencies.jsFooter)
.pipe(gulp.dest('hugo/static/scripts/'));
}
//
// Responsive images
// Generate different sized images for srcset
function imgResponsive() {
return gulp.src('hugo/static/uploads/**/*.*')
.pipe(plugins.filter(file => /\.(jpg|jpeg|png)$/i.test(file.path)))
.pipe(plugins.rename(makeLowerCaseExt))
.pipe(plugins.hashstore.sources(config.responsiveHashstore, {
invalidateObject: [ config.responsiveOptions, config.responsiveGlobals ],
outputPostfixes: Object.values(config.responsiveOptions).map(
pattern => pattern.map(item => item.rename.suffix)).flatten()
}))
.pipe(plugins.responsive(config.responsiveOptions, config.responsiveGlobals))
.pipe(gulp.dest('hugo/images-cache/'))
.pipe(plugins.hashstore.results(config.responsiveHashstore));
}
//
// Optimize responsive images and copy to final location
function imgMinJpg () {
return gulp.src(['src/images/**/*.*', 'hugo/images-cache/**/*.*'])
.pipe(plugins.filter(file => /\.(jpg|jpeg|png)$/i.test(file.path)))
.pipe(plugins.rename(makeLowerCaseExt))
.pipe(plugins.hashstore.sources(config.imageminJpgHashstore, { logTree: false }))
.pipe(plugins.imagemin({verbose: true}))
.pipe(gulp.dest('hugo/static/images/'))
.pipe(plugins.hashstore.results(config.imageminJpgHashstore));
}
//
// Optimize and copy svg or gif images to final destination
function imgMinGif () {
return gulp.src(['src/images/**/*.*', 'hugo/images-cache/**/*.*'])
.pipe(plugins.filter(file => /\.(gif|svg)$/i.test(file.path)))
.pipe(plugins.rename(makeLowerCaseExt))
.pipe(plugins.hashstore.sources(config.imageminGifHashstore, { logTree: false }))
.pipe(plugins.imagemin({verbose: true}))
.pipe(gulp.dest('hugo/static/images/'))
.pipe(plugins.hashstore.results(config.imageminGifHashstore));
}
// Image output generation can be iffy unless lowercase extensions are used..
function makeLowerCaseExt (path) {
path.extname = path.extname.toLowerCase();
}
// Generate favicon files. favicon.png is recommended to be 280 x 280px.
// It will run once to create the icons and then only when changes are
// made. Delete the hugo/static/faviconData.json file to force it.
gulp.task('generate-favicon', function(done) {
var sha1sum = crypto.createHash('sha1');
var filename = 'src/images/favicon.png';
sha1sum.update(fs.readFileSync(filename));
var generated_hash = sha1sum.digest('hex');
if (fs.existsSync(config.faviconDataFile)) {
var dataFile = JSON.parse(fs.readFileSync(config.faviconDataFile));
if (dataFile.generated_hash === generated_hash) {
console.log('Favicon files up-to-date for sha1: ' + generated_hash + '\n');
return done();
}
}
console.log('Generated sha1 for new favicon: ' + generated_hash + '\n');
plugins.realFavicon.generateFavicon(config.faviconOptions(filename, generated_hash), function() {
// Add generated_hash to details file
var dataFile = JSON.parse(fs.readFileSync(config.faviconDataFile));
dataFile["generated_hash"] = generated_hash;
fs.writeFileSync(config.faviconDataFile, JSON.stringify(dataFile, null, 2));
console.log("Generated new favicon files from " + chalk.underline("https://realfavicongenerator.net"));
done();
});
});
// Inject favicon links in the favicon html partial
gulp.task('inject-favicon', function() {
fs.writeFileSync('hugo/layouts/partials/favicon.html', "");
return gulp.src('hugo/layouts/partials/favicon.html')
.pipe(plugins.realFavicon.injectFaviconMarkups(JSON.parse(fs.readFileSync(config.faviconDataFile)).favicon.html_code))
.pipe(gulp.dest('hugo/layouts/partials'));
});
//
// CSS processing
function postCss () {
return gulp.src('src/styles/*.{css,pcss}')
.pipe(plugins.inlinerjs())
.pipe(plugins.sourcemaps.init())
.pipe(plugins.postcss(config.processors()))
.pipe(plugins.rename({extname: '.css'}))
.pipe(plugins.sourcemaps.write('.'))
.pipe(gulp.dest('hugo/static/styles/'));
}
// CSS processing, minification
function minpostCss () {
return gulp.src('src/styles/*.{css,pcss}')
.pipe(plugins.inlinerjs())
.pipe(plugins.sourcemaps.init())
.pipe(plugins.postcss(config.minProcessors()))
.pipe(plugins.rename({extname: '.min.css'}))
.pipe(plugins.sourcemaps.write('.'))
.pipe(gulp.dest('hugo/static/styles/'));
}
// Copy Vendor CSS into hugo/static
gulp.task('vendorStyles', () => {
return gulp.src('src/styles_vendor/*.css', { since: gulp.lastRun('vendorStyles') })
.pipe(gulp.dest('hugo/static/styles_vendor/'));
});
//
// Javascript processing
// Linting
// .eslintrc.json can be used by your editor (see README.md)
// eslint rules for js aimed at browser are in config/
let lintJs = lazypipe()
.pipe(plugins.eslint, 'config/eslint.json')
.pipe(plugins.eslint.format);
// dev js tasks
function scripts () {
return gulp.src('src/scripts/*.js', { since: gulp.lastRun(scripts) })
.pipe(lintJs())
.pipe(plugins.inlinerjs())
.pipe(gulp.dest('hugo/static/scripts/'));
}
function scriptsHead () {
return gulp.src('src/scripts_head/*.js', { since: gulp.lastRun(scriptsHead) })
.pipe(lintJs())
.pipe(plugins.inlinerjs())
.pipe(gulp.dest('hugo/static/scripts_head/'));
}
// Javascript minification and source mapping
// TODO Possibly add concatenation (not really needed when served via HTTP2)
let minJs = lazypipe()
.pipe(plugins.sourcemaps.init)
.pipe(plugins.uglify)
.pipe(plugins.rename, {extname: '.min.js'})
.pipe(plugins.sourcemaps.write, '.');
// stage and live js tasks
function minscripts () {
return gulp.src('src/scripts/*.js')
.pipe(minJs())
.pipe(gulp.dest('hugo/static/scripts/'));
}
function minscriptsHead () {
return gulp.src('src/scripts_head/*.js')
.pipe(minJs())
.pipe(gulp.dest('hugo/static/scripts_head/'));
}
// Modernizr
// Read custom config and generate a custom build , already minified
gulp.task('custoModernizr', () => {
let exec = require('child_process').exec;
let cmd = __dirname + '/node_modules/.bin/modernizr ';
cmd += '-c ./config/modernizr-config.json ';
cmd += '-d ./hugo/static/scripts_vendor/modernizr.custom.js';
return exec(cmd, {encoding: 'utf-8'});
});
//
// Copy other assets like icons and txt files from src to hugo/static
gulp.task('copy', () => {
return gulp.src('src/*', { since: gulp.lastRun('copy') })
.pipe(gulp.dest('hugo/static/'));
});
//
// HTML templates
// Copy HTML templates from src/layouts to hugo/layouts
// We cant lint and minify here because of hugo specific code
function html () {
return gulp.src('src/layouts/**/*.html', { since: gulp.lastRun(html) })
.pipe(plugins.inlinerjs())
.pipe(gulp.dest('hugo/layouts/'));
}
//
// Inject css and js into templates
function injectHead () {
let modernizrPath = gulp.src('hugo/static/scripts_vendor/modernizr.custom.js', {read: false});
let scriptsHead = gulp.src('hugo/static/scripts_head/*.js', {read: false});
let projectCss = gulp.src('hugo/static/styles/*.css', {read: false});
let vendorCss = gulp.src('hugo/static/styles_vendor/*.css', {read: false});
return gulp.src('hugo/layouts/partials/head-meta.html')
.pipe(plugins.inject(sseries(modernizrPath, scriptsHead),
{transform: transformToHugoPaths, selfClosingTag: true, ignorePath: 'hugo/static/', name: 'head'}))
.pipe(plugins.inject(sseries(vendorCss, projectCss),
{transform: transformToHugoPaths, ignorePath: 'hugo/static/'}))
.pipe(gulp.dest('hugo/layouts/partials/'));
}
function injectFoot () {
let scriptsFooter = gulp.src('hugo/static/scripts/*.js', {read: false});
return gulp.src('hugo/layouts/partials/footer-scripts.html')
.pipe(plugins.inject(scriptsFooter,
{transform: transformToHugoPaths, ignorePath: 'hugo/static/'}))
.pipe(gulp.dest('hugo/layouts/partials/'));
}
function transformToHugoPaths(filepath) {
if (filepath.slice(-3) === '.js') {
return '<script src="{{ \"' + filepath + '\" | relURL }}"></script>';
}
if (filepath.slice(-4) === '.css') {
return '<link href="{{ \"' + filepath + '\" | relURL }}" rel="stylesheet" type="text/css">';
}
// Use the default transform as fallback:
return inject.transform.apply(inject.transform, arguments);
}
//
// Hugo
// -D is buildDrafts
// -F is buildFuture
function hugo (status) {
let exec = require('child_process').exec;
let cmd = 'hugo --quiet --config=hugo/config.toml -s hugo/';
if (status === 'stage') {
cmd += ' -D -d published/stage/ --baseURL="' + config.hugoBaseUrl.stage + '"';
console.log(chalk.green('hugo command: \n') + cmd + '\n');
} else if (status === 'live') {
cmd += ' -d published/live/ --baseURL="' + config.hugoBaseUrl.live + '"';
console.log(chalk.green('hugo command: \n') + cmd + '\n');
} else {
cmd += ' -DF -d published/dev/ --baseURL="http://localhost:3000"';
console.log(chalk.green('hugo command: \n') + cmd + '\n');
}
var child = exec(cmd, {encoding: 'utf-8'});
var promise = promiseFromChildProcess(child);
var output = function (data) {
if (data.startsWith("'hugo' is not recognized")) {
throw Error(data);
}
if (data.startsWith("ERROR")) {
console.error(chalk.red(data));
} else {
console.log(data);
}
}
child.stdout.on('data', output);
child.stderr.on('data', output);
return promise;
}
function promiseFromChildProcess(child) {
return new Promise(function (exit, error) {
child.addListener("error", error);
child.addListener("exit", exit);
});
}
gulp.task('hugoDev', () => {
return Promise.all([ hugo() ]);
});
gulp.task('hugoStage', () => {
return Promise.all([ hugo('stage') ]);
});
gulp.task('hugoLive', () => {
return Promise.all([ hugo('live') ]);
});
//
// HTML linting & minification
gulp.task('htmlDev', () => {
return gulp.src('hugo/published/dev/**/*.html', { since: gulp.lastRun('htmlDev') })
.pipe(plugins.htmltidy(config.htmltidyOptions))
.pipe(gulp.dest('hugo/published/dev/'));
});
gulp.task('htmlStage', () => {
return gulp.src('hugo/published/stage/**/*.html')
.pipe(plugins.htmltidy(config.htmltidyOptions))
.pipe(plugins.htmlmin(config.htmlminOptions))
.pipe(gulp.dest('hugo/published/stage/'));
});
gulp.task('htmlLive', () => {
return gulp.src('hugo/published/live/**/*.html')
.pipe(plugins.htmltidy(config.htmltidyOptions))
.pipe(plugins.htmlmin(config.htmlminOptions))
.pipe(gulp.dest('hugo/published/live/'));
});
//
// Cleaning
// Specific cleaning functions for dev/stage/live of hugo/published.
function cleanDev (done) {
return del(['hugo/published/dev/'], done);
}
function cleanStage (done) {
return del(['hugo/published/stage/'], done);
}
function cleanLive (done) {
return del(['hugo/published/live/'], done);
}
// Clean any assets we output into hugo/static (except images)
function cleanStatic (done) {
return del([
'hugo/static/scripts/*',
'hugo/static/scripts_head/*',
'hugo/static/scripts_vendor/*',
'hugo/static/styles/*',
'hugo/static/styles_vendor/*'
], done);
}
// Clean any assets we output into hugo/layouts
function cleanLayouts (done) {
return del(['hugo/layouts/**/*.html'], done);
}
// Clean final image files created during processing
function cleanImages (done) {
return del([
'hugo/static/images/*'
], done);
}
//
// Serve and sync
// Wrap browsersync reload
// This wrapper seems to be required to get around a bug, see recent comments on
// this issue https://github.com/BrowserSync/browser-sync/issues/711
function reload(done) {
// return browserSync.reload(); switched to callback because some tasks don't
// signal completion to gulp, example issues
// https://github.com/BrowserSync/browser-sync/pull/987
// https://github.com/BrowserSync/browser-sync/issues/1065
browserSync.reload();
done();
}
var browserSync;
// Watch files and serve with Browsersync
gulp.task('watcher', (done) => {
browserSync = require('browser-sync').create();
// Start a server
browserSync.init({
server: {
baseDir: 'hugo/published/dev'
}
}, done());
var addWatcher = function (globs, action) {
var debounced = debounce(action, 100);
gulp.watch(globs).on('add', debounced);
gulp.watch(globs).on('change', debounced);
gulp.watch(globs).on('unlink', debounced);
}
// Watch files for changes
addWatcher('hugo/static/uploads/**/*', imgResponsive);
addWatcher(['src/images/**/*', 'hugo/images-cache/**/*'], gulp.series(imgMinJpg, imgMinGif, 'hugoDev', 'htmlDev', reload));
addWatcher('src/styles/*.{css,pcss}', gulp.series(postCss, injectHead, 'hugoDev', 'htmlDev', reload));
addWatcher('src/styles/partials/*.{css,pcss}', gulp.series(postCss, injectHead, 'hugoDev', 'htmlDev', reload));
addWatcher('src/styles_vendor/*.css', gulp.series('vendorStyles', injectHead, 'hugoDev', 'htmlDev', reload));
addWatcher('src/*', gulp.series('copy', 'hugoDev', 'htmlDev', reload));
addWatcher('config/modernizr-config.json', gulp.series('custoModernizr', injectHead, 'hugoDev', 'htmlDev', reload));
addWatcher('src/scripts/**/*.js', gulp.series(scripts, injectFoot, 'hugoDev', 'htmlDev', reload));
addWatcher('src/scripts_head/**/*.js', gulp.series(scriptsHead, injectHead, 'hugoDev', 'htmlDev', reload));
addWatcher('src/layouts/**/*.html', gulp.series(html, injectHead, injectFoot, 'hugoDev', 'htmlDev', reload));
addWatcher(['hugo/archetypes/**/*', 'hugo/content/**/*', 'hugo/data/**/*', 'hugo/config.*'], gulp.series('hugoDev', reload));
});
// Debounce helper
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
function debounce(func, wait, immediate) {
var timeout;
return function () {
var context = this, args = arguments;
var later = function () {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
//
// 'gulp' is the main development task, essentially dev + watch + browsersync
gulp.task('default',
gulp.series(
gulp.parallel(cleanStatic, cleanLayouts, cleanDev),
gulp.series(cleanImages, imgResponsive, imgMinJpg, imgMinGif),
gulp.parallel('custoModernizr', postCss, scripts, scriptsHead),
gulp.parallel(copyDependenciesCss, copyDependenciesJsFooter,
'copy', html, 'vendorStyles', gulp.series('generate-favicon', 'inject-favicon')),
gulp.parallel(injectHead, injectFoot),
'hugoDev',
'htmlDev',
'watcher'
)
);
// 'gulp dev' a single run, hugo will generate pages for drafts and future posts
gulp.task('dev',
gulp.series(
gulp.parallel(cleanStatic, cleanLayouts, cleanDev),
gulp.series(cleanImages, imgResponsive, imgMinJpg, imgMinGif),
gulp.parallel('custoModernizr', postCss, scripts, scriptsHead),
gulp.parallel(copyDependenciesCss, copyDependenciesJsHead, copyDependenciesJsFooter,
'copy', html, 'vendorStyles', gulp.series('generate-favicon', 'inject-favicon')),
gulp.parallel(injectHead, injectFoot),
'hugoDev',
'htmlDev'
)
);
// 'gulp stage' a single run, hugo will generate pages for drafts
gulp.task('stage',
gulp.series(
gulp.parallel(cleanStatic, cleanLayouts, cleanStage),
gulp.series(cleanImages, imgResponsive, imgMinJpg, imgMinGif),
gulp.parallel('custoModernizr', minpostCss, minscripts, minscriptsHead),
gulp.parallel(copyDependenciesCss, copyDependenciesJsHead, copyDependenciesJsFooter,
'copy', html, 'vendorStyles', gulp.series('generate-favicon', 'inject-favicon')),
gulp.parallel(injectHead, injectFoot),
'hugoStage',
'htmlStage'
)
);
// 'gulp live' a single run, production only
gulp.task('live',
gulp.series(
gulp.parallel(cleanStatic, cleanLayouts, cleanLive),
gulp.series(cleanImages, imgResponsive, imgMinJpg, imgMinGif),
gulp.parallel('custoModernizr', minpostCss, minscripts, minscriptsHead),
gulp.parallel(copyDependenciesCss, copyDependenciesJsHead, copyDependenciesJsFooter,
'copy', html, 'vendorStyles', gulp.series('generate-favicon', 'inject-favicon')),
gulp.parallel(injectHead, injectFoot),
'hugoLive',
'htmlLive'
)
);
// Task used for debugging function based task or tasks
gulp.task('dt', gulp.series(imgResponsive, imgMinGif, imgMinJpg));
// Same task as 'gulp live' for production only
// Optimized a bit to parallelize image processing
gulp.task('CircleCI-build',
gulp.parallel(
gulp.series(imgResponsive, imgMinJpg, imgMinGif),
gulp.series(
gulp.parallel(cleanStatic, cleanLayouts, cleanLive),
// gulp.parallel('custoModernizr', minpostCss, minscripts, minscriptsHead), -- not working right now
gulp.parallel('custoModernizr', minpostCss, scripts, scriptsHead),
gulp.parallel(copyDependenciesCss, copyDependenciesJsHead, copyDependenciesJsFooter,
'copy', html, 'vendorStyles', gulp.series('generate-favicon', 'inject-favicon')),
gulp.parallel(injectHead, injectFoot),
'htmlLive'
)
)
);