-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
113 lines (99 loc) · 3.09 KB
/
gulpfile.js
File metadata and controls
113 lines (99 loc) · 3.09 KB
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
"use strict";
const gulp = require('gulp');
const bower = require('gulp-bower');
const purify = require('gulp-purifycss');
const concatCss = require('gulp-concat-css');
const cleanCSS = require('gulp-clean-css');
const gulpIgnore = require('gulp-ignore');
const htmlreplace = require('gulp-html-replace');
const gulpRemoveHtml = require('gulp-remove-html');
const htmlmin = require('gulp-html-minifier');
const replace = require('gulp-replace');
const { exec } = require('child_process'); // ✅ 用於 AMP 驗證
const fs = require('fs');
const paths = {
src: {
dir: './resources/public/weekly',
html: './resources/public/weekly/**/*.html'
},
dist: {
dir: './build/dist',
html: './build/dist/**/*.html'
},
tmp: {
dir: './build/tmp',
css: './build/tmp/app.css'
}
};
// --- Tasks ---
gulp.task('bower', function () {
return bower();
});
gulp.task('compile:css', gulp.series('bower', function () {
return gulp.src([
'./bower_components/bootstrap/dist/css/bootstrap.min.css',
'./resources/public/weekly/css/screen.css'
])
.pipe(concatCss('app.css'))
.pipe(purify([paths.src.html]))
// remove unsupported AMP syntax
.pipe(replace(/!important/g, ''))
.pipe(replace(/@-ms-viewport\s*{\n(.*?)\n}/g, ''))
.pipe(gulp.dest(paths.tmp.dir));
}));
gulp.task('build:inline-css', gulp.series('compile:css', function () {
return gulp.src(paths.src.html)
.pipe(htmlreplace({
'cssInline': {
'src': gulp.src(paths.tmp.css).pipe(cleanCSS()),
'tpl': '<style amp-custom>%s</style>'
}
}))
.pipe(gulp.dest(paths.dist.dir));
}));
gulp.task('build:remove-html', gulp.series('build:inline-css', function () {
return gulp.src(paths.dist.html)
.pipe(gulpRemoveHtml({ keyword: 'build:removeHtml' }))
.pipe(gulp.dest(paths.dist.dir));
}));
gulp.task('build:minify-html', gulp.series('build:remove-html', function () {
return gulp.src(paths.dist.html)
.pipe(htmlmin({
collapseWhitespace: true,
minifyJS: true,
minifyCSS: true,
removeComments: true
}))
.pipe(gulp.dest(paths.dist.dir));
}));
gulp.task('build:fix-html-link', gulp.series('build:minify-html', function () {
return gulp.src(paths.dist.html)
.pipe(replace(/<a\s+href="http/g, '<a target="_blank" href="http'))
.pipe(gulp.dest(paths.dist.dir));
}));
// ✅ New AMP Validation Task using official CLI
gulp.task('validate:amphtml', gulp.series('build:minify-html', function (done) {
console.log('🔍 Running AMP validation...');
exec(`npx amphtml-validator ${paths.dist.html}`, (err, stdout, stderr) => {
if (stdout) console.log(stdout);
if (stderr) console.error(stderr);
if (err) {
console.error('❌ AMP validation failed.');
done(new Error('AMP validation failed.'));
} else {
console.log('✅ AMP validation passed.');
done();
}
});
}));
// --- Build sequence ---
gulp.task('build', gulp.series(
'bower',
'compile:css',
'build:inline-css',
'build:remove-html',
'build:minify-html',
'build:fix-html-link',
'validate:amphtml'
));
gulp.task('default', gulp.series('build'));