|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { getWebpackConfig } = require('../lib/cmd/pack'); |
| 4 | +const log = require('./log').setup({ file: __filename }); |
| 5 | +const webpack = require('webpack'); |
| 6 | + |
| 7 | +function builder(yargs) { |
| 8 | + return yargs |
| 9 | + .usage('Usage: $0') |
| 10 | + .example('$0', 'Compile the entrypoints configured in Webpack.'); |
| 11 | +} |
| 12 | + |
| 13 | +/** |
| 14 | + * Run a one-off Webpack build. |
| 15 | + * |
| 16 | + * @param {webpack.Compiler} webpackCompiler - A configured Webpack compiler |
| 17 | + * instance. |
| 18 | + * @returns {Promise} - A Promise that resolves when the compilation is |
| 19 | + * complete. |
| 20 | + */ |
| 21 | +function handleAssetBuild(webpackCompiler) { |
| 22 | + return new Promise((resolve, reject) => { |
| 23 | + webpackCompiler.run((err, stats) => { |
| 24 | + if (err) { |
| 25 | + return reject(err); |
| 26 | + } |
| 27 | + |
| 28 | + if (stats.hasErrors()) { |
| 29 | + const msg = stats.toJson('errors-only'); |
| 30 | + |
| 31 | + return reject(msg); |
| 32 | + } |
| 33 | + |
| 34 | + resolve(webpackCompiler); |
| 35 | + }); |
| 36 | + }).then(compiler => { |
| 37 | + compiler.close(error => { |
| 38 | + if (error) { |
| 39 | + throw error; |
| 40 | + } |
| 41 | + }); |
| 42 | + }).catch(error => { |
| 43 | + log('error', 'Webpack compilation failed', { |
| 44 | + error |
| 45 | + }); |
| 46 | + }); |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Run a Webpack build. |
| 51 | + * |
| 52 | + * @param {webpack.Compiler} webpackCompiler - A configured Webpack compiler |
| 53 | + * instance. |
| 54 | + * @returns {Promise} - A Promise that resolves when the live compilation is |
| 55 | + * terminated. |
| 56 | + */ |
| 57 | +function handleAssetWatch(webpackCompiler) { |
| 58 | + return new Promise((resolve, reject) => { |
| 59 | + const watchingInstance = webpackCompiler.watch( |
| 60 | + { |
| 61 | + ignored: /node_modules/ |
| 62 | + }, |
| 63 | + (err, stats) => { |
| 64 | + if (err) { |
| 65 | + return reject(err); |
| 66 | + } |
| 67 | + |
| 68 | + if (stats.hasErrors()) { |
| 69 | + const msg = stats.toJson('errors-only'); |
| 70 | + |
| 71 | + return reject(msg); |
| 72 | + } |
| 73 | + } |
| 74 | + ); |
| 75 | + |
| 76 | + resolve(watchingInstance); |
| 77 | + }).then(watching => { |
| 78 | + process.on('exit', () => { |
| 79 | + watching.close(error => { |
| 80 | + if (error) { |
| 81 | + throw error; |
| 82 | + } |
| 83 | + }); |
| 84 | + }); |
| 85 | + }).catch(error => { |
| 86 | + log('error', 'Webpack compilation failed', { |
| 87 | + message: error.message, |
| 88 | + stack: error.stack |
| 89 | + }); |
| 90 | + }); |
| 91 | +} |
| 92 | + |
| 93 | +function handler(argv) { |
| 94 | + const config = getWebpackConfig(argv).toConfig(); |
| 95 | + const compiler = webpack(config); |
| 96 | + const builder = argv.watch ? handleAssetWatch.bind(null, compiler) : handleAssetBuild.bind(null, compiler); |
| 97 | + |
| 98 | + return builder(); |
| 99 | +} |
| 100 | + |
| 101 | +exports.aliases = ['p']; |
| 102 | +exports.builder = builder; |
| 103 | +exports.command = 'pack'; |
| 104 | +exports.describe = 'Compile Webpack assets'; |
| 105 | +exports.handler = handler; |
0 commit comments