UNPKG

1.81 kBJavaScriptView Raw
1/*
2 Copyright © 2018 Andrew Powell
3
4 This Source Code Form is subject to the terms of the Mozilla Public
5 License, v. 2.0. If a copy of the MPL was not distributed with this
6 file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
8 The above copyright notice and this permission notice shall be
9 included in all copies or substantial portions of this Source Code Form.
10*/
11const chalk = require('chalk');
12const webpack = require('webpack');
13
14const 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 // indent the result slightly to visually set it apart from other output
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
59module.exports = { run };