-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
gulpfile.mjs
84 lines (76 loc) · 2.89 KB
/
gulpfile.mjs
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
import { src, dest, watch, series, parallel } from 'gulp';
import changed from 'gulp-changed';
import csso from 'gulp-csso';
import ext_replace from 'gulp-ext-replace';
import htmlmin from 'gulp-html-minifier-terser';
import gulpAmpValidator from 'gulp-amphtml-validator';
import through2 from 'through2';
import amphtmlValidator from 'amphtml-validator';
import AmpOptimizer from '@ampproject/toolbox-optimizer';
const ampOptimizer = AmpOptimizer.create();
function build(cb) {
return src('./_site/**/*.html')
.pipe(
through2.obj(async (file, _, cb) => {
if (file.isBuffer()) {
const date = new Date();
console.log(`[\x1b[90m${date.toLocaleTimeString('it-IT')}\x1b[0m] Running AMP Optimizer on ${file.path}`);
const optimizedHtml = await ampOptimizer.transformHtml(
file.contents.toString()
);
file.contents = Buffer.from(optimizedHtml);
}
cb(null, file);
})
)
.pipe(htmlmin({ collapseWhitespace: false }))
.pipe(dest('./_site/'));
}
function test() {
return src('./_site/**/*.html')
// Validate the input and attach the validation result to the "amp" property
// of the file object.
.pipe(gulpAmpValidator.validate())
// Print the validation results to the console.
.pipe(gulpAmpValidator.format())
// Exit the process with error code (1) if an AMP validation error
// occurred.
.pipe(gulpAmpValidator.failAfterWarningOrError());
}
function validate() {
return src('./_site/**/*.html')
.pipe(
through2.obj(async (file, _, cb) => {
if (file.isBuffer()) {
const validator = await amphtmlValidator.getInstance();
const contents_in_string = `${file.contents.toString()}`;
if (contents_in_string.indexOf('<title>Redirecting…</title>') === -1) {
const result = validator.validateString(contents_in_string);
if (result.status !== 'PASS') console.error(`\n${result.status}: ${file.relative}`);
// (result.status === 'PASS' ? console.log : console.error)(result.status);
for (let ii = 0; ii < result.errors.length; ii++) {
const error = result.errors[ii];
let msg =
'line ' + error.line + ', col ' + error.col + ': ' + error.message;
if (error.specUrl !== null) {
msg += ' (see ' + error.specUrl + ')';
}
(error.severity === 'ERROR' ? console.error : console.warn)(msg);
}
};
}
cb(null, file);
})
);
}
function minifyCSS() {
return src(['./_includes/css/*.css', '!**/*.min.css'])
.pipe(changed('./_includes/css/'))
.pipe(csso())
.pipe(ext_replace('.min.css'))
.pipe(dest('./_includes/css/'));
}
export default function () {
watch(['./_includes/css/*.css', '!**/*.min.css'], minifyCSS);
}
export { build, test, minifyCSS, validate };