-
Notifications
You must be signed in to change notification settings - Fork 13
/
gulpfile.js
260 lines (222 loc) · 6.72 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* eslint-env node */
'use strict';
const del = require('del');
const gulp = require('gulp');
const fs = require('fs-extra');
const semver = require('semver');
const csso = require('gulp-csso');
const copy = require('gulp-copy');
const jsdoc = require('gulp-jsdoc3');
const terser = require('gulp-terser');
const eslint = require('gulp-eslint');
const connect = require('gulp-connect');
const htmlmin = require('gulp-htmlmin');
const replace = require('gulp-replace');
const workbox = require('workbox-build');
const firebase = require('firebase-tools');
const runSequence = require('run-sequence');
const autoprefixer = require('gulp-autoprefixer');
const inlinesource = require('gulp-inline-source');
const SRC_DIR = 'src';
const DEST_DIR = 'build';
const TEMP_DIR = '.tmp';
const TERSER_OPTS = {
compress: {
drop_console: true,
},
output: {
beautify: false,
max_line_len: 120,
indent_level: 2,
},
};
/** ***************************************************************************
* Bump Version Number
*****************************************************************************/
/**
* Bumps the version number in the package.json file.
*
* @param {string} release - Type of release patch|minor|major.
* @return {Promise}.
*/
function bumpVersion(release) {
return fs.readJson('package.json')
.then((data) => {
const currentVersion = data.version;
const nextVersion = semver.inc(currentVersion, release);
data.version = nextVersion;
return fs.writeJson('package.json', data, {spaces: 2});
});
}
gulp.task('bump:patch', () => {
return bumpVersion('patch');
});
/** ***************************************************************************
* Clean
*****************************************************************************/
gulp.task('clean:temp', () => {
const filesToDelete = ['.tmp/**'];
return del(filesToDelete);
});
gulp.task('clean:build', () => {
const filesToDelete = ['build/**'];
return del(filesToDelete);
});
gulp.task('clean:docs', () => {
const filesToDelete = ['docs/**'];
return del(filesToDelete);
});
gulp.task('clean', (cb) => {
return runSequence(['clean:build', 'clean:temp', 'clean:docs'], cb);
});
/** ***************************************************************************
* Linting & Doc Generation
*****************************************************************************/
gulp.task('lint', () => {
const filesToLint = [
'gulpfile.js',
'src/index.html',
'src/scripts/*',
'src/inline-scripts/*',
];
const config = {
useEslintrc: true,
};
return gulp.src(filesToLint)
.pipe(eslint(config))
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
gulp.task('jsdoc', ['clean:docs', 'lint'], () => {
const filesToDoc = [
'README.md',
'src/scripts/*.js',
];
const config = fs.readJsonSync('.jsdocrc.json');
return gulp.src(filesToDoc, {read: false})
.pipe(jsdoc(config));
});
/** ***************************************************************************
* Generate Service Worker
*****************************************************************************/
gulp.task('generate-service-worker', () => {
return workbox.generateSW({
globDirectory: DEST_DIR,
globPatterns: [
'**/*.{html,js,png,ico,mp3,json}',
],
swDest: `${DEST_DIR}/service-worker.js`,
clientsClaim: true,
skipWaiting: true,
offlineGoogleAnalytics: true,
runtimeCaching: [
{
urlPattern: /^https:\/\/fonts\.gstatic\.com/,
handler: 'cacheFirst',
options: {
cacheName: 'googleapis',
expiration: {maxEntries: 30},
},
},
],
}).then(({warnings}) => {
for (const warning of warnings) {
console.log(warning);
}
});
});
/** ***************************************************************************
* Build
*****************************************************************************/
gulp.task('css-build', () => {
const autoprefixOpts = {
browsers: ['last 2 versions'],
};
const cssoOpts = {
sourceMap: true,
};
return gulp.src(`${SRC_DIR}/styles/styles.css`)
.pipe(autoprefixer(autoprefixOpts))
.pipe(csso(cssoOpts))
.pipe(gulp.dest(`${TEMP_DIR}/styles/`));
});
gulp.task('js-inline', () => {
return gulp.src(`${SRC_DIR}/inline-scripts/*.js`)
.pipe(terser(TERSER_OPTS))
.pipe(gulp.dest(`${TEMP_DIR}/inline-scripts`));
});
gulp.task('js-script', () => {
return gulp.src(`${SRC_DIR}/scripts/*.js`)
.pipe(terser(TERSER_OPTS))
.pipe(gulp.dest(`${TEMP_DIR}/scripts`));
});
gulp.task('js-build', (cb) => {
return runSequence(['js-inline', 'js-script'], cb);
});
gulp.task('html-copy', () => {
const filesToCopy = [
`${SRC_DIR}/index.html`,
];
return gulp.src(filesToCopy)
.pipe(copy(TEMP_DIR, {prefix: 1}));
});
gulp.task('html-build', ['html-copy', 'css-build', 'js-build'], () => {
const inlineOpts = {
compress: false,
pretty: false,
};
const htmlMinOpts = {
collapseWhitespace: true,
maxLineLength: 80,
minifyCSS: true,
minifyJS: false,
removeComments: true,
};
const buildDate = new Date().toISOString();
const packageJSON = fs.readJsonSync('package.json');
return gulp.src(`${TEMP_DIR}/index.html`)
.pipe(replace('[[BUILD_DATE]]', buildDate))
.pipe(replace('[[VERSION]]', packageJSON.version))
.pipe(inlinesource(inlineOpts))
.pipe(htmlmin(htmlMinOpts))
.pipe(gulp.dest(TEMP_DIR));
});
gulp.task('copy-static', () => {
const filesToCopy = [
`${TEMP_DIR}/index.html`,
`${TEMP_DIR}/maps/**/*`,
`${TEMP_DIR}/scripts/**/*`,
`${SRC_DIR}/icons/**/*`,
`${SRC_DIR}/images/**/*`,
`${SRC_DIR}/sounds/**/*`,
`${SRC_DIR}/manifest.json`,
`${SRC_DIR}/robots.txt`,
`${SRC_DIR}/humans.txt`,
];
return gulp.src(filesToCopy)
.pipe(copy(DEST_DIR, {prefix: 1}));
});
gulp.task('build:no-sw', ['clean'], (cb) => {
return runSequence('html-build', 'copy-static', cb);
});
gulp.task('build', (cb) => {
return runSequence('build:no-sw', 'generate-service-worker', cb);
});
/** ***************************************************************************
* Development - serving
*****************************************************************************/
gulp.task('serve', () => {
return connect.server({root: 'src'});
});
gulp.task('serve:prod', ['build:no-sw'], () => {
return connect.server({root: 'build'});
});
/** ***************************************************************************
* Deploy
*****************************************************************************/
gulp.task('deploy-firebase', () => {
return firebase.deploy();
});
gulp.task('deploy', (cb) => {
return runSequence('bump:patch', 'build', 'deploy-firebase', cb);
});