-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
624 lines (574 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
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
/* eslint-disable new-cap */
/* eslint-disable max-len */
/* eslint-disable quotes */
'use strict';
// Require Gulp first
const gulp = require('gulp');
// packageJson = require('./package.json'),
// Load plugins
const $ = require('gulp-load-plugins')({
lazy: true,
});
// Static Web Server stuff
const browsersync = require('browser-sync').create();
// const reload = browsersync.reload;
const historyApiFallback = require('connect-history-api-fallback');
// postcss
const postcss = require('gulp-postcss');
const autoprefixer = require('autoprefixer');
// SASS
const sassProc = require('gulp-sass')(require('sass'));
// Critical CSS
const criticalPlugin = require('critical').stream;
// Image Processing
// We need path for the task to work when detecting extesions
const path = require('path');
// This replaces imagemin and plugins in previous versions
// of this build file
const squoosh = require('gulp-libsquoosh');
// Utilities
const del = require('del');
// Act only on newer files
const newer = require('gulp-newer');
// Tap into Gulp pipelines
const tap = require('gulp-tap');
// Replace extensions
const replaceExt = require('replace-ext');
// MARKDOWN AND PLUGINS
// Testing Markdown configuration and whether this will be enough
const markdownIt = require('markdown-it');
// load the plugins
const abbr = require("markdown-it-abbr");
const alerts = require("markdown-it-alerts");
const anc = require("markdown-it-anchor");
const attrs = require("markdown-it-attrs");
const embed = require("markdown-it-block-embed");
const fn = require("markdown-it-footnote");
const figs = require("markdown-it-implicit-figures");
const kbd = require("markdown-it-kbd");
const prism = require("markdown-it-prism");
const toc = require("markdown-it-table-of-contents");
const list = require("markdown-it-task-lists");
const mermaid = require("markdown-it-textual-uml");
// explicitly require eslint
const eslintPlugin = require('gulp-eslint');
// ------------
// HTML & Markdown
// ------------
// Markdown-It Options
const options = {
preset: 'commonmark',
html: true,
xhtmlOut: true,
linkify: true,
typographer: true,
};
/**
* @name md
* @description Instantiates Markdown-It and loads the listed plugins. The plugins must already be installed and available for this to work.
*/
const md = new markdownIt(options);
md.use(abbr);
md.use(alerts);
md.use(anc);
md.use(attrs);
md.use(embed);
md.use(fn);
md.use(figs);
md.use(kbd);
md.use(prism);
md.use(toc);
md.use(list);
md.use(mermaid);
/**
* @name markdownToHtml
* @description Converts markdown to HTML using gulp-markdown-it and a set of plugins.
* @param {*} file
* @return {void}
*/
function markdownToHtml(file) {
const result = md.render(file.contents.toString());
file.contents = new Buffer(result);
file.path = replaceExt(file.path, '.html');
return;
}
/**
* @name markdown
* @description converts markdown to HTML using gulp-markdown-it and a set of plugins.
* @return {void}
*
* The idea is to eventually generate two Markdown files:
* * One that works in WordPress
* * One standalone Markdown that will work with a set of extension and will be easy to convert to HTML
*
*/
function markdown() {
return gulp
.src('src/md-content/*.md')
.pipe(tap(markdownToHtml))
.pipe(gulp.dest('src/html-content/'));
}
/**
* @name buildTemplate
* @description Builds the full HTML templates from the markdown generated fragments
* @param {string} done
* @return {void}
*/
function buildTemplate(done) {
gulp.src('./src/html-content/*.html')
.pipe($.wrap({
src: './src/templates/template.html',
}))
.pipe(gulp.dest('./dist'));
done();
};
// ------------
// Paged Media and PDF
// ------------
/**
* @name buildPMTemplate
* @description Builds HTML files to use with PrinceXMML later
* @param {String} done
* @return {void}
*/
function buildPMTemplate(done) {
gulp.src('./src/html-content/*.html')
.pipe($.wrap({
src: './src/templates/template-pm.html',
}))
.pipe(gulp.dest('./src/pm-content'));
done();
};
/**
* @name buildPDF
* @description Builds the PDF from the HTML using PrinceXML
* @return {void}
*/
function buildPDF() {
const options = {
continueOnError: true, // default = false, true means don't emit error event
pipeStdout: false, // default = false, true means stdout is written to file.contents
};
const reportOptions = {
err: true, // default = true, false means don't write err
stderr: true, // default = true, false means don't write stderr
stdout: true, // default = true, false means don't write stdout
};
return gulp.src('./src/pm-content/*.html')
.pipe(newer('src/pdf/'))
.pipe($.exec((file) => `prince --verbose --input=html --javascript --style src/css/article-styles.css ${file.path}`, options))
.pipe($.exec.reporter(reportOptions));
};
/**
* @name copyPDF
* @description Copies the PDF to the build folder
* @param {string} done
* @return {void}
*
*/
function copyPDF(done) {
gulp.src('src/pm-content/*.pdf', {
dot: true,
base: 'src/pm-content',
})
.pipe(gulp.dest('dist/pdf'))
.pipe($.size({
pretty: true,
title: 'copy',
}));
done();
};
// ------------
// SASS & CSS
// ------------
/**
* @name sass
* @description SASS conversion task to produce development css with expanded syntax.
*
* We run this task agains Dart SASS, not lib SASS.
*
* @return {void}
* @see {@link http://sass-lang.com|SASS}
* @see {@link http://sass-compatibility.github.io/|SASS Feature Compatibility}
*/
function sass() {
return gulp.src('src/sass/**/*.{scss,sass}')
.pipe($.sourcemaps.init())
.pipe(sassProc.sync({outputStyle: 'expanded'}))
.pipe($.sourcemaps.write('./maps'))
.pipe(gulp.dest('src/css'));
};
/**
* @name processCSS
*
* @description Run autoprefixer and cleanCSS on the CSS files under src/css
*
* Moved from gulp-autoprefixer to postcss. It may open other options in the future like cssnano to compress the files
* @return {void}
*
* @see {@link https://github.com/postcss/autoprefixer|autoprefixer}
*/
function processCSS() {
// What processors/plugins to use with PostCSS
const PROCESSORS = [
autoprefixer(),
];
return gulp
.src('src/css/**/*.css')
.pipe($.sourcemaps.init())
.pipe(postcss(PROCESSORS))
.pipe($.sourcemaps.write('.'))
.pipe(gulp.dest('./dist/css'));
};
/**
* @name uncss
* @description Taking a css and an html file, UNCSS will strip all CSS selectors not used in the page
* @return {void}
* @see {@link https://github.com/giakki/uncss|uncss}
*/
function uncss() {
return gulp.src('src/css/**/*.css')
.pipe($.concat('main.css'))
.pipe($.uncss({
html: ['index.html'],
}))
.pipe(gulp.dest('css/main.css'))
.pipe($.size({
pretty: true,
title: 'Uncss',
}));
};
/**
* @name critical
* @description calculates and extracts the critical path CSS from the given file
* @return {void}
*/
function critical() {
return gulp.src('src/*.html')
.pipe(criticalPlugin({
base: 'src/',
inline: true,
css: ['src/css/main.css'],
minify: true,
extract: false,
ignore: ['font-face'],
dimensions: [{
width: 320,
height: 480,
}, {
width: 768,
height: 1024,
}, {
width: 1280,
height: 960,
}],
}))
.pipe($.size({
pretty: true,
title: 'Critical',
}))
.pipe(gulp.dest('dist'));
};
// ------------
// Javascript Tasks
// ------------
/**
* @name babel
* @description Transpiles ES6 to ES5 using Babel. As Node and browsers support more of the spec natively this will move to supporting ES2016 and later transpilation
* @return {void}
* It requires the `@babel/preset-modules` plugin
*
* @see {@link http://babeljs.io/|Babel}
* @see {@link http://babeljs.io/docs/learn-es2015/|Learn ES2015}
* @see {@link http://www.ecma-international.org/ecma-262/6.0/|ECMAScript 2015 specification}
*/
function babel() {
return gulp.src('src/js/**/*.js')
.pipe($.sourcemaps.init())
.pipe($.babel({
presets: [
"@babel/preset-modules",
],
}))
.pipe($.sourcemaps.write('.'))
.pipe(gulp.dest('./dist/scripts'))
.pipe($.size({
pretty: true,
title: 'Babel',
}));
};
/**
* @name eslint
* @description Runs eslint on all javascript files
* @return {void}
*/
function eslint() {
return gulp.src([
'scr/scripts/**/*.js',
])
.pipe(eslintPlugin())
.pipe(eslintPlugin.format('checkstyle'))
.pipe(eslintPlugin.failAfterError());
};
/**
* @name jsdoc
* @description runs jsdoc on the gulpfile and README.md to genereate documentation using the Docdash theme
* @return {void}
* @see {@link https://example.com/|gulp-jsdoc3}
* @see {@link http://clenemt.github.io/docdash/|DocDash}
* @see {@link https://github.com/clenemt/docdash|DocDash GitHub}
* @see {@link https://github.com/jsdoc3/jsdoc|JSDOC}
*/
function jsdoc() {
const config = require('./jsdocConfig.json');
return gulp.src(['README.md', 'gulpfile.js'])
.pipe($.jsdoc3(config));
};
// ------------
// Image Manipulation Tasks
// ------------
/**
* @name imageResize
* @description Resize images to the specified dimensions
* @return {void}
*
* @see {@link https://www.npmjs.com/package/gulp-libsquoosh|gulp-libsquoosh}
* @see {@link https://github.com/GoogleChromeLabs/squoosh/tree/dev/libsquoosh|libsquoosh}
*/
function imageResize() {
return gulp.src('src/original-images/**/*.{jpg, jpeg, png, webp, avif}')
.pipe(
squoosh((src) => ({
preprocessOptions: {
resize: {
enabled: true,
// Using only the width will preserve aspect ratio
width: Math.round(src.width / 2),
// height: Math.round(src.height / 2),
},
},
})),
)
.pipe(gulp.dest('src/original-images/'));
};
/**
* @name imageCompress
* @description Squoosh all images in the src/originals folder using gulp-libsquoosh
* @return {void}
*
* @see {@link https://www.npmjs.com/package/gulp-libsquoosh|gulp-libsquoosh}
* @see {@link https://github.com/GoogleChromeLabs/squoosh/tree/dev/libsquoosh|libsquoosh}
*/
function imageCompress() {
return gulp.src(['src/original-images/**/*.{png,jpg,webp}'])
.pipe(
squoosh((src) => {
// console.log(src);
const extname = path.extname(src.path);
let options = {
encodeOptions: squoosh.DefaultEncodeOptions[extname],
};
if (extname === '.jpg') {
options = {
encodeOptions: {
jxl: {},
mozjpeg: {},
},
};
}
if (extname === '.png') {
options = {
encodeOptions: {
avif: {},
},
preprocessOptions: {
quant: {
enabled: true,
numColors: 16,
},
},
};
}
return options;
}),
)
.pipe(gulp.dest('dist/images'));
};
// ------------
// Utilities
// ------------
/**
* @name copyFonts
* @description Copies fonts into the distribution directory.
* @return {void}
*
*/
function copyFonts() {
return gulp.src([
'./src/fonts/*',
]).pipe(gulp.dest('dist/fonts'));
};
/**
* @name copyCSS
* @description Copies third-party CSS into the distribution directory.
* @return {void}
*/
function copyCSS() {
return gulp.src([
"./src/css/normalize.css",
"./src/css/prism.css",
"./src/css/image-load.css",
"./src/css/video-load.css",
]).pipe(gulp.dest('dist/css'));
}
/**
* @name copyLocalJS
* @description copies third-party JS into distribution directory
* @return {void}
*/
function copyLocalJS() {
return gulp.src([
"./src/scripts/lazy-load.js",
"./src/scripts/load-fonts.js",
"./src/scripts/lazy-load-video.js",
]).pipe(gulp.dest('dist/scripts'));
}
/**
* @name copyVendorJS
* @description copies third-party JS into distribution directory
* @return {void}
*/
function copyVendorJS() {
return gulp.src([
"./src/scripts/vendor/clipboard.min.js",
"./src/scripts/vendor/fontfaceobserver.js",
"./src/scripts/vendor/prism.js",
])
.pipe(gulp.dest('dist/scripts/vendor'));
}
/**
* @name clean
* @description deletes specified files
* @return {void}
*/
function clean() {
return del([
'dist/',
'.tmp',
'src/html-content/*.html',
'src/*.html',
'src/pm-content/*',
'src/pdf/*.pdf',
]);
};
// ------------
// Development Server
// ------------
/**
* @name serve
* @description Serves the files from the dist directory
* @return {void}
*/
function serve() {
browsersync.init({
port: 2509,
notify: false,
logPrefix: 'ATHENA',
snippetOptions: {
rule: {
match: "<span id='browser-sync-binding'></span>",
fn: function(snippet) {
return snippet;
},
},
},
server: {
baseDir: ['./dist'],
middleware: [historyApiFallback()],
},
});
};
// ------------
// Compound Tasks
// ------------
const pdfBuild = gulp.series(
markdown,
buildPMTemplate,
buildPDF,
copyPDF,
);
const htmlBuild = gulp.series(
markdown,
buildTemplate,
);
const fullCSS = gulp.series(
sass,
processCSS,
copyCSS,
);
const processJS = gulp.series(
babel,
copyLocalJS,
copyVendorJS,
);
const processImages = gulp.series(
imageResize,
imageCompress,
);
const copyAll = gulp.parallel(
copyFonts,
copyCSS,
copyLocalJS,
copyVendorJS,
);
// ------------
// Default Task Definition
// ------------
const defaultTask = gulp.series(
clean,
htmlBuild,
fullCSS,
imageCompress,
babel,
copyAll,
);
// ------------
// Exports
// ------------
// HTML TASKS
exports.markdown = markdown;
exports.buildHTML = buildTemplate;
// PDF TASKS
exports.buildPM = buildPMTemplate;
exports.buildPDF = buildPDF;
exports.copyPDF = copyPDF;
// CSS TASKS
exports.sass = sass;
exports.processCSS = processCSS;
exports.copyCSS = copyCSS;
exports.uncss = uncss;
exports.critical = critical;
// JAVASCRIPT TASKS
exports.babel = babel;
exports.eslint = eslint;
exports.jsdoc = jsdoc;
exports.copyLocalJS = copyLocalJS;
exports.copyVendorJS = copyVendorJS;
exports.copyJS = gulp.series(copyLocalJS, copyVendorJS);
// IMAGE TASKS
exports.imageResize = imageResize;
exports.imageCompress = imageCompress;
// UTILITY TASKS
exports.clean = clean;
exports.serve = serve;
exports.copyFonts = copyFonts;
// Copy Everything that is not put in dist by a task
exports.copyAll = copyAll;
// Compound Tasks
exports.pdf = pdfBuild;
exports.html = htmlBuild;
exports.js = processJS;
exports.images = processImages;
exports.css = fullCSS;
// Default
exports.default = defaultTask;