|
| 1 | +import webpack from 'webpack'; |
| 2 | +import _debug from 'debug'; |
| 3 | +import fs from 'fs-extra'; |
| 4 | +import config from '../webpack.config.prod.js'; |
| 5 | + |
| 6 | +const debug = _debug('app:build:webpack-compiler'); |
| 7 | + |
| 8 | +const DEFAULT_STATS_FORMAT = { |
| 9 | + chunks : false, |
| 10 | + chunkModules : false, |
| 11 | + colors : true |
| 12 | + } |
| 13 | + |
| 14 | +function webpackCompiler (webpackConfig, statsFormat = DEFAULT_STATS_FORMAT) { |
| 15 | + return new Promise((resolve, reject) => { |
| 16 | + const compiler = webpack(webpackConfig); |
| 17 | + |
| 18 | + compiler.run((err, stats) => { |
| 19 | + const jsonStats = stats.toJson(); |
| 20 | + |
| 21 | + debug('Webpack compile completed.'); |
| 22 | + debug(stats.toString(statsFormat)); |
| 23 | + |
| 24 | + if (err) { |
| 25 | + debug('Webpack compiler encountered a fatal error.', err); |
| 26 | + return reject(err); |
| 27 | + } else if (jsonStats.errors.length > 0) { |
| 28 | + debug('Webpack compiler encountered errors.'); |
| 29 | + debug(jsonStats.errors.join('\n')); |
| 30 | + return reject(new Error('Webpack compiler encountered errors')); |
| 31 | + } else if (jsonStats.warnings.length > 0) { |
| 32 | + debug('Webpack compiler encountered warnings.'); |
| 33 | + debug(jsonStats.warnings.join('\n')); |
| 34 | + } else { |
| 35 | + debug('No errors or warnings encountered.'); |
| 36 | + } |
| 37 | + resolve(jsonStats); |
| 38 | + }); |
| 39 | + }); |
| 40 | +} |
| 41 | + |
| 42 | +;(async function () { |
| 43 | + try { |
| 44 | + debug('Run compiler'); |
| 45 | + const stats = await webpackCompiler(config); |
| 46 | + if (stats.warnings.length && config.compiler_fail_on_warning) { |
| 47 | + debug('Config set to fail on warning, exiting with status code "1".'); |
| 48 | + process.exit(1); |
| 49 | + } |
| 50 | + } catch (e) { |
| 51 | + debug('Compiler encountered an error.', e); |
| 52 | + process.exit(1); |
| 53 | + } |
| 54 | +})(); |
0 commit comments