forked from f-list/exported
-
Notifications
You must be signed in to change notification settings - Fork 20
/
webpack.js
35 lines (30 loc) · 1.24 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
const webpack = require('webpack');
const path = require('path');
const mode = process.argv[2];
let config = require(path.resolve(process.argv[3] || 'webpack.config'));
if(typeof config === 'function') config = config(mode);
config = Array.isArray(config) ? config : [config];
for(const item of config) item.mode = mode === 'watch' ? 'development' : mode;
let lastHash = null;
function compilerCallback(err, stats) {
if(mode !== 'watch' || err) compiler.purgeInputFileSystem();
if(err) {
lastHash = null;
console.error(err.stack || err);
if(err.details) console.error(err.details);
process.exit(1);
}
if(stats.hash !== lastHash) {
lastHash = stats.hash;
const statsString = stats.toString({colors: require("supports-color"), context: config[0].context, cached: false, cachedAssets: false, exclude: ['node_modules']});
if(statsString) process.stdout.write(statsString + '\n');
}
if(mode !== 'watch' && stats.hasErrors()) {
process.exitCode = 2;
console.error('FULL ERROR', stats.stats[0].compilation.errors[0]);
}
}
console.log(config);
const compiler = webpack(config);
if(mode === 'watch') compiler.watch({}, compilerCallback);
else compiler.run(compilerCallback);