-
Notifications
You must be signed in to change notification settings - Fork 191
/
gulpfile.js
224 lines (201 loc) · 6.38 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
/* Dependencies */
const gulp = require('gulp');
const plumber = require('gulp-plumber');
const autoprefixer = require('gulp-autoprefixer');
const fileinclude = require('gulp-file-include');
const sass = require('gulp-sass');
const filter = require('gulp-filter')
const minimist = require('minimist');
const del = require('del');
const gulpAmpValidator = require('gulp-amphtml-validator');
const bs = require('browser-sync').create();
const reload = bs.reload;
const nodemon = require('gulp-nodemon');
const replace = require('gulp-replace');
const noop = require('gulp-noop');
const through2 = require('through2');
const mergeMediaQuery = require('gulp-merge-media-queries');
const AmpOptimizer = require('@ampproject/toolbox-optimizer');
const ampOptimizer = AmpOptimizer.create();
// Build type is configurable such that some options can be changed e.g. whether
// to minimise CSS. Usage 'gulp <task> --env development'.
const knownOptions = {
string: 'env',
default: { env: process.env.NODE_ENV || 'dist' }
};
const options = minimist(process.argv.slice(2), knownOptions);
const paths = {
css: {
src: 'src/sass/**/*.scss',
dest: 'src/css/'
},
html: {
src: 'src/html/pages/*.html',
dest: 'dist/'
},
images: {
src: 'src/img/**/*.{gif,jpg,png,svg}',
dest: 'dist/img'
},
favicon: {
src: 'src/favicon/*',
dest: 'dist/'
},
rootConfig: {
src: 'src/rootConfigFiles/*',
dest: 'dist/'
},
server: {
src: 'src/server/**/*',
dest: 'dist/server'
}
};
/**
* Builds the styles, bases on SASS files taken from src. The resulting CSS is
* used as partials that are included in the final AMP HTML.
* When SASS sees a non-ASCII character in a file, it starts the CSS file it builds with "@charset "UTF-8";".
* That's great in CSS files, but not accepted within <style> tags.
* So unless the SASS team takes on https://github.com/sass/sass/issues/2288, we need to remove it.
*/
gulp.task('styles', function buildStyles() {
const cssEncodingDirective = '@charset "UTF-8";';
return gulp.src(paths.css.src)
.pipe(plumber())
.pipe(sass(options.env === 'dist' ? { outputStyle: 'compressed' } : {}))
.pipe(options.env === 'dev' ? replace(cssEncodingDirective, '') : noop())
.pipe(autoprefixer('last 10 versions'))
.pipe(mergeMediaQuery({log: true}))
.pipe(gulp.dest(paths.css.dest));
});
/**
* Copies the images to the distribution.
*/
gulp.task('images', function buildImages() {
return gulp.src(paths.images.src)
.pipe(gulp.dest(paths.images.dest));
});
/**
* Copies the favicon to the distribution.
*/
gulp.task('favicon', function buildImages() {
return gulp.src(paths.favicon.src)
.pipe(gulp.dest(paths.favicon.dest));
});
/**
* Copies the root config files to the distribution.
*/
gulp.task('rootConfig', function buildImages() {
return gulp.src(paths.rootConfig.src)
.pipe(gulp.dest(paths.rootConfig.dest));
});
/**
* Copies the server and helper classes to the distribution.
*/
gulp.task('server', function buildImages() {
return gulp.src(paths.server.src)
.pipe(gulp.dest(paths.server.dest));
});
/**
* Builds the HTML files. Only files from 'pages' are built.
* We don't want to build partials!
* Use the AMP Optimizer to add any scripts required by AMP components,
* and to perform the optimizations done by AMP caches right here in our HTML,
* greatly speeding up our AMP pages when served from our origin.
*/
gulp.task('html', gulp.series('styles', function buildHtml() {
const pageFilter = filter(['**/pages/*.html']);
return gulp.src(paths.html.src)
.pipe(pageFilter)
.pipe(fileinclude({
prefix: '%%',
basepath: '@file'
}))
.pipe(through2.obj(async (file, _, cb) => {
if (file.isBuffer()) {
const optimizedHtml = await ampOptimizer.transformHtml(file.contents.toString())
file.contents = Buffer.from(optimizedHtml)
}
cb(null, file);
}))
.pipe(gulp.dest(paths.html.dest));
}));
/**
* Checks resulting output AMP HTML for validity.
*/
gulp.task('validate', function validate() {
return gulp.src(paths.html.dest + '/**/*.html')
.pipe(gulpAmpValidator.validate())
.pipe(gulpAmpValidator.format())
.pipe(gulpAmpValidator.failAfterError());
});
/**
* Removes all files from the distribution directory, and also the CSS build
* directory.
*/
gulp.task('clean', function clean() {
return del([
paths.html.dest + '/**/*',
paths.css.dest + '/**/*'
]);
});
/**
* Builds the output from sources.
*/
gulp.task('build', gulp.series('images', 'favicon', 'rootConfig', 'html', 'server', 'validate'));
/**
* First rebuilds the output then triggers a reload of the browser.
*/
gulp.task('rebuild', gulp.series('build', function rebuild(done) {
bs.reload();
done();
}));
/**
* Sets up the live browser sync.
*/
/*
gulp.task('serve', function sync(done) {
bs.init({
server: {
baseDir: 'dist/'
}
});
done();
});
*/
gulp.task('browser-sync', function sync(done) {
bs.init(null, {
proxy: "http://localhost:8080", // port of node server
});
done();
});
gulp.task('nodemon', function (cb) {
var callbackCalled = false;
return nodemon({script: './dist/server/server.js'}).on('start', function () {
if (!callbackCalled) {
callbackCalled = true;
cb();
}
});
});
/**
* Sets up live-reloading: Changes to HTML or CSS trigger a rebuild, changes to
* images, favicon, root config files and server only result in images, favicon, root config files, server and helper classes being copied again to dist.
*/
gulp.task('watch', function watch(done) {
gulp.watch(paths.images.src, gulp.series('images'));
gulp.watch(paths.favicon.src, gulp.series('favicon'));
gulp.watch(paths.rootConfig.src, gulp.series('rootConfig'));
gulp.watch(paths.server.src, gulp.series('server'));
gulp.watch('src/html/**/*.html', gulp.series('rebuild'));
gulp.watch(paths.css.src, gulp.series('rebuild'));
done();
});
/**
* Prepares a clean build.
*/
gulp.task('prepare', gulp.series('clean', 'build'));
/**
* Default task is to perform a clean build then set up browser sync for live
* reloading.
*/
gulp.task('default', gulp.series('build', 'nodemon', 'browser-sync', 'watch'));