-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
executable file
·119 lines (101 loc) · 2.95 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
const browserSync = require('browser-sync');
const del = require('del');
const gulp = require('gulp');
const ampValidator = require('gulp-amphtml-validator');
const autoprefixer = require('gulp-autoprefixer');
const cssnano = require('gulp-cssnano');
const plumber = require('gulp-plumber');
const pug = require('gulp-pug');
const sass = require('gulp-sass');
const merge = require('merge-stream');
const { join, parse } = require('path');
const through = require('through2');
/*
* Helpers
*/
const browserSyncInstance = browserSync.create();
const inlineCss = () => {
const htmlFiles = {};
const cssFiles = {};
const onFile = function(file, _, callback) {
const { ext, dir, name } = parse(file.relative);
if (['.css', '.html'].includes(ext)) {
if (ext === '.html') {
htmlFiles[join(dir, name)] = file;
callback();
} else {
cssFiles[join(dir, name)] = file;
callback();
}
} else {
callback(new Error(`Invalid extension: ${ext}.`));
}
};
const onEnd = function(callback) {
Object.entries(htmlFiles).forEach(([key, htmlFile]) => {
if (key in cssFiles) {
const cssFileContents = cssFiles[key].contents.toString();
const customStyleTag = `<style amp-custom>${cssFileContents}</style>`;
const customCssRegex = /<style amp-custom>(.*)<\/style>/;
const newHtmlFileContents = htmlFile.contents
.toString()
.replace(customCssRegex, customStyleTag);
htmlFile.contents = new Buffer.from(newHtmlFileContents);
}
this.push(htmlFile);
});
callback();
};
return through.obj(onFile, onEnd);
};
/*
* Tasks
*/
gulp.task('clean', () => del(['dist/**', '!dist'], { force: true }));
gulp.task('build:assets', () =>
gulp.src('src/assets/**/*', { base: 'src' }).pipe(gulp.dest('dist')),
);
gulp.task('build:pages', () => {
const htmlStream = gulp
.src('src/templates/pages/**/*.pug', { base: 'src/templates/pages' })
.pipe(plumber())
.pipe(pug())
.pipe(plumber.stop());
const stylesStream = gulp
.src('src/styles/**/*.scss', { base: 'src/styles' })
.pipe(plumber())
.pipe(sass())
.pipe(plumber.stop())
.pipe(autoprefixer())
.pipe(cssnano());
return merge(htmlStream, stylesStream)
.pipe(plumber())
.pipe(inlineCss())
.pipe(plumber.stop())
.pipe(ampValidator.validate())
.pipe(ampValidator.format())
.pipe(gulp.dest('dist'));
});
gulp.task('build', gulp.series('clean', 'build:assets', 'build:pages'));
gulp.task(
'watch',
gulp.series('build', () => {
browserSyncInstance.init({
open: false,
port: 8080,
server: {
baseDir: 'dist',
serveStaticOptions: {
extensions: ['html'],
},
},
});
gulp
.watch(
['src/templates/**/*.pug', 'src/styles/**/*.scss'],
gulp.series('build'),
)
.on('change', browserSyncInstance.reload);
}),
);
gulp.task('default', gulp.series('watch'));