forked from snitin315/webpack-bundle-analyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
89 lines (74 loc) · 2.19 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
'use strict';
const gulp = require('gulp');
const NODE_SRC = './src/**/*.js';
const NODE_DEST = './lib';
const cli = require('commander')
.usage('<task> [options]')
.option('-e, --env <environment>', 'Can be `prod` or `dev`. Default is `dev`', /^(dev|prod)$/u, 'dev')
.option('-a, --analyze', 'Analyze client bundle. If set, `env` will be set to `prod`.')
.parse(process.argv);
const task = cli.args[0] || 'watch';
if (task === 'build' || cli.analyze) {
cli.env = 'prod';
}
gulp.task('clean', gulp.parallel(cleanNodeScripts, cleanViewerScripts));
gulp.task('build', gulp.series('clean', compileNodeScripts, compileViewerScripts));
gulp.task('watch', gulp.series('build', watch));
gulp.task('default', gulp.task('watch'));
class TaskError extends Error {
constructor(message) {
super(message);
this.name = 'TaskError';
// Internal Gulp flag that says "don't display error stack trace"
this.showStack = false;
}
}
function watch() {
gulp
.watch(NODE_SRC, gulp.series(cleanNodeScripts, compileNodeScripts))
// TODO: replace with `emitErrors: false` option after https://github.com/gulpjs/glob-watcher/pull/34 will be merged
.on('error', () => {});
}
function cleanViewerScripts() {
const del = require('del');
return del('public');
}
function cleanNodeScripts() {
const del = require('del');
return del(NODE_DEST);
}
function compileNodeScripts() {
const babel = require('gulp-babel');
return gulp
.src(NODE_SRC)
.pipe(babel())
.pipe(gulp.dest(NODE_DEST));
}
function compileViewerScripts() {
const webpack = require('webpack');
const config = require('./webpack.config')({
env: cli.env,
analyze: cli.analyze
});
return new Promise((resolve, reject) => {
webpack(config, (err, stats) => {
if (cli.env === 'dev') {
if (err) {
console.error(err);
} else {
console.log(stats.toString({colors: true}));
}
resolve();
} else {
if (err) return reject(err);
if (stats.hasErrors()) {
reject(
new TaskError('Webpack compilation error')
);
}
console.log(stats.toString({colors: true}));
resolve();
}
});
});
}