-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
110 lines (98 loc) · 3.26 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
"use strict";
const gulp = require('gulp-help')(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 gulpAmpValidator = require('gulp-amphtml-validator');
const replace = require('gulp-replace');
const uncss = require('gulp-uncss');
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'
}
};
gulp.task('bower', function() {
return bower();
});
gulp.task('compile:css', ['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 !importent which not supported by amphtml
.pipe(replace(/!important/g, ''))
// remove @-ms-viewport which not supported by amphtml
.pipe(replace(/@-ms-viewport\s*{\n(.*?)\n}/g, ''))
.pipe(gulp.dest(paths.tmp.dir));
});
// inline-css inserts the cleaned + minified CSS into HTML
gulp.task('build:inline-css', ['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', ['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', ['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', ['build:minify-html'], function () {
return gulp.src(paths.dist.html)
// make all link create new tab
.pipe(replace(/<a\s+href="http/g, '<a target="_blank" href="http'))
.pipe(gulp.dest(paths.dist.dir));
});
gulp.task('validate:amphtml', ['build:minify-html'], function() {
return gulp.src(paths.dist.html)
// Some page no need to support amp, just ignore it
.pipe(gulpIgnore.exclude(['**/404.html']))
// Valide 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.failAfterError());
});
gulp.task('build', 'build all resources', [
'bower',
'compile:css',
'build:inline-css',
'build:remove-html',
'build:minify-html',
'build:fix-html-link',
'validate:amphtml'
]);
gulp.task('default', 'build all resources', [
'build',
]);