-
Notifications
You must be signed in to change notification settings - Fork 2
/
gulpfile.lib.js
384 lines (347 loc) · 8.84 KB
/
gulpfile.lib.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
const gulp = require('gulp');
const babel = require('gulp-babel');
const cache = require('gulp-cached');
const gulpIf = require('gulp-if');
const nodemon = require('gulp-nodemon');
const rev = require('gulp-rev');
const sass = require('gulp-sass');
const sourcemaps = require('gulp-sourcemaps');
const ts = require('gulp-typescript');
const zip = require('gulp-zip');
const webpackStream = require('webpack-stream');
const through = require('through2');
const rsync = require('gulp-rsync');
const revReplace = require('gulp-rev-replace');
const fs = require('fs');
const del = require('del');
const path = require('path');
const webpack = require('webpack');
const WebpackDevServer = require('webpack-dev-server');
const getWebpackConfig = require('./build/webpack');
const tsProject = ts.createProject('tsconfig.json');
function handleError(error) {
console.error(error);
this.emit('end');
}
function createCheckServerJSTask({
name,
globs,
destination,
serverWatchGlobs = undefined,
}) {
if (serverWatchGlobs) {
// eslint-disable-next-line no-param-reassign
serverWatchGlobs[name] = globs;
}
gulp.task(name, () => {
return gulp
.src(globs)
.pipe(tsProject())
.on('error', handleError)
.pipe(gulp.dest(destination));
});
}
function createBuildServerJSTask({
name,
globs,
destination,
serverWatchGlobs = undefined,
}) {
if (serverWatchGlobs) {
// eslint-disable-next-line no-param-reassign
serverWatchGlobs[name] = globs;
}
gulp.task(name, () => {
return gulp
.src(globs)
.pipe(cache(name))
.pipe(sourcemaps.init())
.pipe(babel())
.on('error', handleError)
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(destination));
});
}
function createBuildClientJSWebpackTask(
name,
{
sourceFile,
destFile,
destDir,
cssDestDir,
cssDestFile,
publicPath,
production = false,
watch = false,
hasCss = true,
isMobile = false,
},
) {
const destFileParts = destFile.split('.');
let outputFile = destFile;
// In production mode, add a suffix for caching
// (see https://webpack.js.org/guides/caching)
if (production && !isMobile) {
destFileParts.splice(destFileParts.length - 1, 0, '[contenthash:8]');
outputFile = destFileParts.join('.');
}
const webpackConfig = getWebpackConfig(
{
hasCss,
useSourceMaps: !production,
useCssSourceMaps: !production && !isMobile,
minimize: production,
production,
// For mobile, code-splitting is unneeded because we put
// all Javascript on the client anyways
splitChunks: !isMobile,
emitLoadable: !isMobile,
},
{
output: { filename: outputFile, publicPath },
watch,
},
);
gulp.task(name, taskDone => {
let cssEmitted = !hasCss || false;
let jsEmitted = false;
let taskDoneCalled = false;
gulp
.src(sourceFile)
.pipe(webpackStream(webpackConfig))
.pipe(gulpIf(/\.css/, gulp.dest(cssDestDir)))
.pipe(gulpIf(/\.js/, gulp.dest(destDir)))
.pipe(
through.obj((file, enc, cb) => {
// We want to mark the task as finished as soon as the first set of
// files are bundled, so we don't block later tasks
if (
file.path.match(/\.css(\.\w{8})?(\.asset)?$/) ||
file.path.endsWith(cssDestFile)
) {
cssEmitted = true;
}
if (
file.path.match(/\.js(\.\w{8})?(\.asset)?$/) ||
file.path.endsWith(destFile)
) {
jsEmitted = true;
}
if (cssEmitted && jsEmitted && !taskDoneCalled) {
taskDoneCalled = true;
taskDone();
}
cb(null, file);
}),
);
});
}
/** Creates a task to run the Webpack dev server */
function createWebpackDevServerTask(name, { sourceFile, publicPath }) {
const webpackConfig = getWebpackConfig(
{
hasCss: true,
minimize: false,
useSourceMaps: true,
useCssSourceMaps: true,
production: false,
splitChunks: true,
emitLoadable: true,
},
{
entry: {
main: path.join(__dirname, sourceFile),
},
output: {
filename: '[name].js',
publicPath,
},
},
);
gulp.task(name, cb => {
// Based off of code from https://github.com/webpack/webpack-dev-server/blob/db5ce44/examples/api/simple/server.js
const compiler = webpack(webpackConfig);
const server = new WebpackDevServer(compiler, webpackConfig.devServer);
server.listen(7001, '127.0.0.1', error => {
console.log('Starting server on http://localhost:7001');
if (error) {
console.error(error);
}
cb();
});
});
}
function createWatchTask(name, watchGlobs) {
console.log(
'If watching fails on Linux, consider running `yarn adjust-linux-watch-limit`',
);
gulp.task(name, cb => {
Object.keys(watchGlobs).forEach(taskName => {
const glob = watchGlobs[taskName];
gulp.watch(glob, gulp.series(taskName));
});
cb();
});
}
function createRevRenameTask(name, glob, dest, manifestDest, production) {
gulp.task(name, () => {
if (production) {
return gulp
.src(glob)
.pipe(rev())
.pipe(gulp.dest(dest))
.pipe(rev.manifest()) // Used for rev-replace-references
.pipe(gulp.dest(manifestDest));
}
return gulp.src('.');
});
}
function createBuildViewsTask(
name,
{
glob,
dest,
revManifestPath = '',
production = false,
watchGlobs = undefined,
},
) {
if (watchGlobs) {
// eslint-disable-next-line no-param-reassign
watchGlobs[name] = glob;
}
gulp.task(name, () => {
let ret = gulp.src(glob);
if (production && revManifestPath) {
ret = ret.pipe(
revReplace({
replaceInExtensions: ['.html', '.ejs'],
manifest: gulp.src(revManifestPath),
}),
);
if (watchGlobs) {
// eslint-disable-next-line no-param-reassign
watchGlobs[name] = `{${watchGlobs[name]},${revManifestPath}}`;
}
} else {
ret = ret.pipe(cache(name));
}
return ret.pipe(gulp.dest(dest));
});
}
function createBuildSassTask(
name,
glob,
dests,
production = false,
watchGlobs = undefined,
) {
if (watchGlobs) {
// eslint-disable-next-line no-param-reassign
watchGlobs[name] = glob;
}
gulp.task(name, () => {
let stream = gulp.src(glob);
if (!production) {
stream = stream.pipe(sourcemaps.init());
}
stream = stream.pipe(sass().on('error', sass.logError));
if (!production) {
stream = stream.pipe(sourcemaps.write());
}
for (const dest of dests) {
stream = stream.pipe(gulp.dest(dest));
}
return stream;
});
}
function createCopyGulpTask(taskName, globs, destDir, watchGlobsVar) {
if (watchGlobsVar) {
// eslint-disable-next-line no-param-reassign
watchGlobsVar[taskName] = globs;
}
gulp.task(taskName, () => {
return gulp
.src(globs)
.pipe(cache(taskName))
.pipe(gulp.dest(destDir));
});
}
function createCreateDirectoriesGulpTask(taskName, directories) {
gulp.task(taskName, async () => {
directories.forEach(dir => {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
});
});
}
function createRunServerTask(name, script, watchDirs, env = {}) {
gulp.task(name, cb => {
nodemon({
script,
ext: 'js json ejs css jpg png',
env,
delay: 200, // Delay is in milliseconds
watch: watchDirs,
stdout: false,
verbose: true,
}).on('readable', function onStarted() {
this.stdout.pipe(process.stdout);
this.stderr.pipe(process.stderr);
cb();
});
});
}
function createSyncTask(name, srcGlobs, rsyncOptions) {
gulp.task(name, () => gulp.src(srcGlobs).pipe(rsync(rsyncOptions)));
}
/**
* Get a gulp task to delete all items at a certain path
* @param {string|string[]} globs
* @return Promise.<void>
*/
function createDeleteTask(name, globs) {
gulp.task(name, () => {
// eslint-disable-next-line no-param-reassign
globs = globs instanceof Array ? globs : [globs];
return del(globs);
});
}
/**
* Zips a bunch of files into
*/
function createZipTask(
name,
globs,
outputFile,
outputDir,
watchGlobs = undefined,
) {
if (watchGlobs) {
// eslint-disable-next-line no-param-reassign
watchGlobs[name] = globs;
}
gulp.task(name, () =>
gulp
.src(globs)
.pipe(zip(outputFile))
.pipe(gulp.dest(outputDir)),
);
}
module.exports = {
createCheckServerJSTask,
createBuildServerJSTask,
createBuildClientJSWebpackTask,
createWebpackDevServerTask,
createWatchTask,
createRevRenameTask,
createBuildViewsTask,
createBuildSassTask,
createCopyGulpTask,
createCreateDirectoriesGulpTask,
createRunServerTask,
createSyncTask,
createDeleteTask,
createZipTask,
};