forked from kimfucious/sakura
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
639 lines (573 loc) · 17.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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
// *************************************
//
// Gulpfile
//
// *************************************
//
// Available tasks:
// copy: bootstrap-css
// copy: node-js-src
// clean
// - clean:images
// - clean:jekyll
// - clean:scripts
// - clean:styles
// build
// - build:images
// - build:normal-images
// - copy:normal-temp-images-pre
// - build:optimize-normal-images
// - copy:normal-temp-images-post
// - clean:normal-temp-images
// - build:responsive-images
// - build:index-algolia
// - build:jekyll
// - build:scripts
// - build:babel-uglify
// - build:concat
// - build:styles:main
// build:jekyll:watch
// build:scripts:watch
// build:travis
// - test:html-proofer
// install
// serve
//
// *************************************
// -------------------------------------
// Modules
// -------------------------------------
//
// autoprefixer : Prefix CSS
// browserSync : Development Server
// del : Deletes things
// gulp : The streaming build system
// pump : Recommended to handles errors for Uglify
// gulp-babel : Use next generation JavaScript, today, with Babel
// gulp-clean-css : Minifies CSS
// gulp-concat : Concatenate files
// gulp-fancy-log : Logs things (replaced gulp-util)
// gulp-imagemin : Minify PNG, JPEG, GIF and SVG images
// gulp-newer : Only copy newer files
// gulp-postcss : CSS transforms
// gulp-plumber : Prevents pipe breaking caused by errors from gulp plugins
// gulp-rename : Rename files
// gulp-responsive : Generates responsive images
// gulp-run : Run shell commands
// gulp-sass : Compile Sass
// gulp-sourcemaps : Generate sourcemaps
// gulp-uglify : Minify JavaScript with UglifyJS
// gulp-util : Utility functions
// run-sequence : Run a series of dependent Gulp tasks in order
//
// -------------------------------------
const autoprefixer = require("autoprefixer");
const browserSync = require("browser-sync").create();
const babel = require("gulp-babel");
const cleanCSS = require("gulp-clean-css");
const concat = require("gulp-concat");
const critical = require("critical").stream;
const del = require("del");
const log = require("fancy-log");
const gulp = require("gulp");
const gutil = require("gulp-util");
const imagemin = require("gulp-imagemin");
const newer = require("gulp-newer");
const plumber = require("gulp-plumber");
const postcss = require("gulp-postcss");
const pump = require("pump");
const rename = require("gulp-rename");
const responsive = require("gulp-responsive");
const run = require("gulp-run");
const runSequence = require("run-sequence");
const sass = require("gulp-ruby-sass");
const sourcemaps = require("gulp-sourcemaps");
const uglify = require("gulp-uglify");
// -------------------------------------
// Import: paths file
// -------------------------------------
const paths = require("./_assets/gulp_config/paths");
// -------------------------------------
// Global variables
// -------------------------------------
const onError = function(error) {
gutil.beep();
log.error(gutil.log);
};
// -------------------------------------------------------
// Task: Copy : Bootstrap SCSS
// copies Bootstrap SCSS from node src
// !!! If you run this, you'll remove the commented
// out modules in _assets/scss/bootstrap/bootstrap.scss
// -------------------------------------------------------
gulp.task("copy:bootstrap-scss", () => {
return gulp
.src(paths.nodeSrcDir + "bootstrap/scss/**/*")
.pipe(newer(paths.scssFiles + "/bootstrap"))
.pipe(gulp.dest(paths.scssFiles + "/bootstrap"));
});
// -------------------------------------
// Task: Copy Node JS Source
// copies JS source files from node
// -------------------------------------
gulp.task("copy:node-js-src", () => {
const src_files = [
paths.nodeSrcDir + "/instantsearch.js/dist/instantsearch.min.js",
paths.nodeSrcDir + "/jquery/dist/jquery.slim.min.js",
paths.nodeSrcDir + "/popper.js/dist/umd/popper.min.js",
paths.nodeSrcDir + "/bootstrap/dist/js/bootstrap.min.js",
paths.nodeSrcDir + "/clipboard/dist/clipboard.min.js"
];
return gulp
.src(src_files)
.pipe(newer(paths.jsFiles + "/vendor/node"))
.pipe(gulp.dest(paths.jsFiles + "/vendor/node"));
});
// ------------------------------------------------
// Task: Clean
// combines all clean tasks, running in parallel
// ------------------------------------------------
gulp.task("clean", [
"clean:jekyll",
"clean:images",
"clean:scripts",
"clean:styles"
]);
// ---------------------------------------------
// Task: Clean : Images
// removes all images creates by build:images
// from Jekyll and _site assets folder
// ---------------------------------------------
gulp.task("clean:images", cb => {
del([paths.jekyllImageFiles + "/*", paths.siteImageFiles + "/*"]);
cb();
});
// -------------------------------------
// Task: Clean : Jekyll
// wipes the entire _site folder
// -------------------------------------
gulp.task("clean:jekyll", function(cb) {
del(["_site"]);
cb();
});
// --------------------------------------------
// Task: Clean : Scripts
// removes JS files created by build:scripts
// from Jekyll and _site folders
// --------------------------------------------
gulp.task("clean:scripts", cb => {
del([paths.jekyllJsFiles + "/*", paths.siteJsFiles + "/*"]);
cb();
});
// -----------------------------------------------
// Task: Clean : Styles
// removes all css created by build:styles:main
// from Jekyll and _Site assets
// -----------------------------------------------
gulp.task("clean:styles", cb => {
del([paths.jekyllCssFiles + "/*", paths.siteCssFiles + "/*"]);
cb();
});
// -----------------------------------------
// Task: Build
// runs main Clean task first
// then runs all Build tasks in Parallel
// then runs Jekyll build
// -----------------------------------------
gulp.task("build", cb => {
runSequence(
"clean",
["build:scripts", "build:images", "build:styles:main"],
"build:jekyll",
cb
);
});
// -----------------------------------------
// Task: Build Images
// builds normal and responsive images
// -----------------------------------------
gulp.task("build:images", cb => {
runSequence(["build:normal-images", "build:responsive-images"], cb);
});
// -----------------------------------------
// Task: Build Normal Images
// performs all normal image tasks
// -----------------------------------------
gulp.task("build:normal-images", cb => {
runSequence(
"copy:normal-temp-images-pre",
"build:optimize-normal-images",
"copy:normal-temp-images-post",
"clean:normal-temp-images",
cb
);
});
// -------------------------------------------------------
// Task: Copy : Normal Temp Images Pre
// copies images for pre-processing
// allows for non optimized images to get copied
// to Jekyll and _site directories when ignored
// _assets/images/normal/temp gets deleted on end
// -------------------------------------------------------
gulp.task("copy:normal-temp-images-pre", cb => {
return gulp
.src(paths.normalImageFilesGlob)
.pipe(gulp.dest(paths.tempImageFiles));
cb();
});
// -------------------------------------------------------
// Task: Copy : Normal Temp Images Post
// copies temp images after processing
// to Jekyll and _site directories
// -------------------------------------------------------
gulp.task("copy:normal-temp-images-post", cb => {
return gulp
.src(paths.tempImageFilesGlob)
.pipe(gulp.dest(paths.jekyllImageFiles + "/normal"))
.pipe(gulp.dest(paths.siteImageFiles + "/normal"));
cb();
});
// ------------------------------------------------
// Task: Build : Optimize Normal Images
// generates optimaized images based on
// contents of _assets/images/normal/temp folder
// processes files in place
// ------------------------------------------------
gulp.task("build:optimize-normal-images", cb => {
return gulp
.src(paths.tempImageFilesGlob)
.pipe(
imagemin([
imagemin.gifsicle({ interlaced: true }),
imagemin.jpegtran({ responsive: true }),
imagemin.optipng({ optimizationLevel: 5 }),
imagemin.svgo({
plugins: [{ removeViewBox: true }, { cleanupIDs: false }]
})
])
)
.pipe(gulp.dest(paths.tempImageFiles));
cb();
});
// -----------------------------------------------
// Task: Clean : Temp Normal Images
// removes _assets/images/normal/temp directory
// -----------------------------------------------
gulp.task("clean:normal-temp-images", cb => {
del(paths.tempImageFiles);
cb();
});
// ----------------------------------------------
// Task: Build : Responsive Images
// generates responsive images based on
// contents of _assets/images/responsive folder
// outputs to Jekyll and _site assets folder
// ----------------------------------------------
gulp.task("build:responsive-images", cb => {
return gulp
.src(paths.responsiveImageFilesGlob)
.pipe(
responsive(
{
"*.jpg": [
{
width: 1140,
rename: { suffix: "-xl-1x" }
},
{
width: 1140,
rename: {
suffix: "-xl-1x",
extname: ".webp"
}
},
{
width: 1140 * 2,
rename: { suffix: "-xl-2x" }
},
{
width: 1140 * 2,
rename: {
suffix: "-xl-2x",
extname: ".webp"
}
},
{
width: 768,
rename: { suffix: "-md-1x" }
},
{
width: 768,
rename: {
suffix: "-md-1x",
extname: ".webp"
}
},
{
width: 768 * 2,
rename: { suffix: "-md-2x" }
},
{
width: 768 * 2,
rename: {
suffix: "-md-2x",
extname: ".webp"
}
},
{
width: 540,
height: 405,
rename: { suffix: "-sm-1x" }
},
{
width: 540,
height: 405,
rename: {
suffix: "-sm-1x",
extname: ".webp"
}
},
{
width: 540 * 2,
height: 405 * 2,
rename: { suffix: "-sm-2x" }
},
{
width: 540 * 2,
height: 405 * 2,
rename: {
suffix: "-sm-2x",
extname: ".webp"
}
}
]
},
{
crop: "centre",
quality: 60,
responsive: true,
withMetadata: false,
withoutEnlargement: true
}
)
)
.pipe(gulp.dest(paths.jekyllImageFiles + "/responsive"))
.pipe(gulp.dest(paths.siteImageFiles + "/responsive"));
cb();
});
// --------------------------------------------------
// Task: Build : Index Algolia
// runs 'jekyll algolia index` with ENV variable
// --------------------------------------------------
gulp.task("build:index-algolia", cb => {
var algolia = process.env.ALGOLIA_API_KEY;
var shellCommand = "ALGOLIA_API_KEY=" + algolia + " jekyll algolia";
return gulp
.src("")
.pipe(run(shellCommand))
.on("error", gutil.log);
cb();
});
// -------------------------------------------
// Task: Build : Jekyll
// runs the Jekyll build command, which
// processes all Jekyll files and outputs
// to the _site folder
// -------------------------------------------
gulp.task("build:jekyll", () => {
var shellCommand = "bundle exec jekyll build --config _config.yml";
return gulp
.src("")
.pipe(run(shellCommand))
.on("error", gutil.log);
});
// -------------------------------------
// Task: Build : Scripts
// combines babel-uglify and concat scripts
// which are separated intentionally
// for greater control
// -------------------------------------
gulp.task("build:scripts", cb => {
runSequence("build:babel-uglify", "build:concat", cb);
});
// -------------------------------------
// Task: Build : Babel-Uglify
// minifies JS and preserves comments
// -------------------------------------
gulp.task("build:babel-uglify", cb => {
const options = {
output: {
comments: true
}
};
pump(
[
gulp.src(paths.jsFiles + "/pretty/**/*.js"),
babel({
presets: ["env"]
}),
uglify(options),
rename({ extname: ".min.js" }),
gulp.dest(paths.jsFiles + "/ugly")
],
cb
);
});
// ---------------------------------------------
// Task: Build : Concat
// concatenates JS files in a specific order
// creates sourcemap
// outputs to both Jekyll and _site assets
// ---------------------------------------------
gulp.task("build:concat", () => {
const src_files = [
paths.jsFiles + "/vendor/node/jquery.slim.min.js",
paths.jsFiles + "/vendor/node/popper.min.js",
paths.jsFiles + "/vendor/node/bootstrap.min.js",
paths.jsFiles + "/vendor/node/instantsearch.min.js",
paths.jsFiles + "/vendor/clipboard.min.js",
paths.jsFiles + "/vendor/fontawesome.min.js",
paths.jsFiles + "/vendor/picturefill.min.js",
paths.jsFiles + "/ugly/algolia.min.js",
paths.jsFiles + "/ugly/vendor/fa-brands.min.js",
paths.jsFiles + "/ugly/vendor/fa-solid.min.js",
// paths.jsFiles + "/ugly/vendor/sitesearch.min.js",
// paths.jsFiles + "/ugly/search_ux.min.js",
paths.jsFiles + "/ugly/select_source.min.js"
];
return gulp
.src(src_files)
.pipe(sourcemaps.init())
.pipe(concat("main.min.js"))
.pipe(sourcemaps.write("./"))
.pipe(gulp.dest(paths.jekyllJsFiles))
.pipe(gulp.dest(paths.siteJsFiles))
.on("error", gutil.log);
});
// -------------------------------------
// Task: Build : Styles : Critical
// creates a main.critical.css file
// in Jekyll and _site assets folder
// result doesn't render well
// I'll probably remove this task
// -------------------------------------
gulp.task("build:styles:critical", () => {
return gulp
.src(paths.siteDir + "/index.html")
.pipe(
critical({
base: paths.siteDir,
css: [paths.jekyllCssFiles + "/main.css"],
minify: true,
dimensions: [
{
width: 1200,
width: 1024,
width: 768,
width: 576,
width: 320
}
]
})
)
.on("error", gutil.log)
.pipe(rename("main.critical.css"))
.pipe(gulp.dest(paths.jekyllCssFiles))
.pipe(gulp.dest(paths.siteCssFiles));
});
// -------------------------------------
// Task: Build : Styles : Main
// generates the main.min.css file
// and ceates sourcemap
// in Jekyll and _site assests folders
// -------------------------------------
gulp.task("build:styles:main", () => {
return sass(paths.scssFiles + "/main.scss", {
style: "compressed",
trace: true
})
.pipe(postcss([autoprefixer({ browsers: ["last 2 versions"] })]))
.pipe(sourcemaps.init())
.pipe(cleanCSS())
.pipe(sourcemaps.write("./"))
.pipe(gulp.dest(paths.jekyllCssFiles))
.pipe(gulp.dest(paths.siteCssFiles))
.pipe(browserSync.stream())
.on("error", gutil.log);
});
// -------------------------------------
// Task: Build : Jekyll : Watch
// reloads BrowserSync on Jekyll build
// -------------------------------------
gulp.task("build:jekyll:watch", ["build:jekyll"], cb => {
browserSync.reload();
cb();
});
// -------------------------------------
// Task: Build : Scripts : Watch
// reloads BrowserSync on Scripts build
// -------------------------------------
gulp.task("build:scripts:watch", ["build:scripts"], cb => {
browserSync.reload();
cb();
});
// -----------------------------------------
// Task: Build Travis
// adds test:html-proofer to build
// -----------------------------------------
gulp.task("build:travis", cb => {
runSequence(
"clean",
["build:scripts", "build:images", "build:styles:main"],
"build:jekyll",
"test:html-proofer",
cb
);
});
// ------------------------------------------------------
// Task: Test HTML Proofer
// runs HTML Proofer for valid links and other things
// ------------------------------------------------------
gulp.task("test:html-proofer", () => {
var shellCommand =
"htmlproofer ./_site --disable-external --check-opengraph --allow-hash-href";
return gulp
.src("")
.pipe(run(shellCommand))
.on("error", gutil.log);
});
// -------------------------------------------------
// Task: Install
// run this only once for initiall install
// otherwise it can be destructive to your SCSS
// copies source code from node
// runs jekyll serve
// -------------------------------------------------
gulp.task("install", () => {
runSequence(["copy:bootstrap-scss", "copy:node-js-src"], "serve");
});
// -------------------------------------------------
// Task: Serve
// runs development server, watching for changes
// -------------------------------------------------
gulp.task("serve", ["build"], () => {
browserSync.init({
server: paths.siteDir,
ghostMode: true,
logFileChanges: true
});
gulp.watch(["_config.yml"], ["build:jekyll:watch"]);
gulp.watch("_assets/scss/**/*.scss", ["build:styles:main"]);
gulp.watch("_assets/js/**/*.js", ["build:scripts"]);
gulp.watch("_assets/images/normal/**/*", ["build:normal-images"]);
gulp.watch("_assets/images/responsive/**/*", ["build:responsive-images"]);
gulp.watch("search.json", ["build:jekyll:watch"]);
gulp.watch("_posts/**/*.+(md|markdown|MD)", ["build:jekyll:watch"]);
gulp.watch(
["**/*.+(html|md|markdown|MD)", "!_site/**/*.*"],
["build:jekyll:watch"]
);
if (module.exports.drafts) {
gulp.watch("_drafts/**/*.(md|markdown|MD)", ["build:jekyll:watch"]);
}
});