-
-
Notifications
You must be signed in to change notification settings - Fork 429
/
webpack.js
100 lines (87 loc) · 2.73 KB
/
webpack.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
'use strict';
var gulp = require('gulp');
var gutil = require('gulp-util');
var webpack = require('webpack');
var WebpackNotifierPlugin = require('webpack-notifier');
var failPlugin = require('webpack-fail-plugin');
var webpackConfig = require('../webpack.config.js');
var packageJson = require('../package.json');
function buildProduction(done) {
// modify some webpack config options
var myProdConfig = webpackConfig;
myProdConfig.output.filename = '[name].[hash].js';
myProdConfig.plugins = myProdConfig.plugins.concat(
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify('production')
}
}),
new webpack.optimize.CommonsChunkPlugin({ name: 'vendor', filename: 'vendor.[hash].js' }),
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: true
}
}),
failPlugin
);
// run webpack
webpack(myProdConfig, function (err, stats) {
if (err) { throw new gutil.PluginError('webpack:build', err); }
gutil.log('[webpack:build]', stats.toString({
colors: true
}));
if (done) { done(); }
});
}
function createDevCompiler() {
// modify some webpack config options
var myDevConfig = webpackConfig;
myDevConfig.devtool = 'inline-source-map';
myDevConfig.plugins = myDevConfig.plugins.concat(
new webpack.optimize.CommonsChunkPlugin({ name: 'vendor', filename: 'vendor.js' }),
new WebpackNotifierPlugin({ title: 'Webpack build', excludeWarnings: true })
);
// create a single instance of the compiler to allow caching
return webpack(myDevConfig);
}
function build() {
return new Promise(function (resolve, reject) {
buildProduction(function (err) {
if (err) {
reject(err);
} else {
resolve('webpack built');
}
});
});
}
function watch() {
var firstBuildDone = false;
return new Promise(function (resolve, reject) {
var devCompiler = createDevCompiler();
devCompiler.watch({ // watch options:
aggregateTimeout: 300 // wait so long for more changes
}, function (err, stats) {
if (err) {
if (!firstBuildDone) {
firstBuildDone = true;
reject(err);
}
throw new gutil.PluginError('webpack:build-dev', err);
} else {
if (!firstBuildDone) {
firstBuildDone = true;
resolve('webpack built');
}
}
gutil.log('[webpack:build-dev]', stats.toString({
chunks: false,
colors: true
}));
});
});
}
module.exports = {
build: function () { return build(); },
watch: function () { return watch(); }
};