1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | const chalk = require('chalk');
|
12 | const webpack = require('webpack');
|
13 |
|
14 | const run = ({ config, watchConfig }, log) => {
|
15 | let lastHash;
|
16 | const compiler = webpack(config);
|
17 |
|
18 | const done = (fatal, stats) => {
|
19 | const hasErrors = stats && stats.hasErrors();
|
20 |
|
21 | process.exitCode = Number(!!fatal || (hasErrors && !watchConfig));
|
22 |
|
23 | if (fatal) {
|
24 | log.error(fatal);
|
25 | return;
|
26 | }
|
27 |
|
28 | if (lastHash === stats.hash) {
|
29 | log.info(chalk`{dim ˢᵉʳᵛᵉ} Duplicate build detected {dim (${lastHash})}\n`);
|
30 | return;
|
31 | }
|
32 |
|
33 | lastHash = stats.hash;
|
34 |
|
35 | const statsDefaults = { colors: chalk.supportsColor.hasBasic, exclude: ['node_modules'] };
|
36 | const { options = {} } =
|
37 | []
|
38 | .concat(compiler.compilers || compiler)
|
39 | .reduce((a, c) => c.options.stats && c.options.stats) || {};
|
40 | const statsOptions =
|
41 | !options.stats || typeof options.stats === 'object'
|
42 | ? Object.assign({}, statsDefaults, options.stats)
|
43 | : options.stats;
|
44 | const result = stats.toString(statsOptions);
|
45 |
|
46 |
|
47 | log.info(result.split('\n').join('\n '), '\n');
|
48 | };
|
49 |
|
50 | if (watchConfig) {
|
51 | log.info('Watching Files');
|
52 | compiler.watch(watchConfig.watchOptions || {}, done);
|
53 | } else {
|
54 | compiler.hooks.done.tap('webpack-serve', () => log.info('Build Finished'));
|
55 | compiler.run(done);
|
56 | }
|
57 | };
|
58 |
|
59 | module.exports = { run };
|