UNPKG

5.31 kBJavaScriptView Raw
1/**
2 * Created by pgotthardt on 07/12/15.
3 */
4
5var Promise = require('bluebird'),
6 chalk = require('chalk');
7
8/**
9 * Choose the most correct version of webpack, prefer locally installed version,
10 * fallback to the own dependency if there's none.
11 * @returns {*}
12 */
13function getWebpack() {
14 try {
15 return require(process.cwd() + '/node_modules/webpack');
16 } catch(e) {
17 return require('webpack');
18 }
19}
20
21function getAppName(webpackConfig) {
22 return webpackConfig.name || webpackConfig.output.filename;
23}
24
25function getOutputOptions(webpackConfig, options) {
26 var outputOptions = Object.create(webpackConfig.stats || {});
27 if(typeof options.modulesSort !== 'undefined') {
28 outputOptions.modulesSort = options.modulesSort;
29 }
30 if(typeof options.chunksSort !== 'undefined') {
31 outputOptions.chunksSort = options.chunksSort;
32 }
33 if(typeof options.assetsSort !== 'undefined') {
34 outputOptions.assetsSort = options.assetsSort;
35 }
36 if(typeof options.exclude !== 'undefined') {
37 outputOptions.exclude = options.exclude;
38 }
39 if(typeof options.colors !== 'undefined') {
40 outputOptions.colors = options.colors;
41 }
42 return outputOptions;
43}
44
45/**
46 * Create a single webpack build using the specified configuration.
47 * Calls the done callback once it has finished its work.
48 *
49 * @param {string} configuratorFileName The app configuration filename
50 * @param {Object} options The build options
51 * @param {boolean} options.watch If `true`, then the webpack watcher is being run; if `false`, runs only ones
52 * @param {boolean} options.json If `true`, then the webpack watcher will only report the result as JSON but not produce any other output
53 * @param {Number} index The configuration index
54 * @param {Function} done The callback that should be invoked once this worker has finished the build.
55 */
56module.exports = function(configuratorFileName, options, index, expectedConfigLength, done) {
57 if(options.argv) {
58 process.argv = options.argv;
59 }
60 chalk.enabled = options.colors;
61 var config = require(configuratorFileName),
62 watch = !!options.watch,
63 silent = !!options.json;
64 if(expectedConfigLength !== 1 && !Array.isArray(config)
65 || Array.isArray(config) && config.length !== expectedConfigLength) {
66 if(config.length !== expectedConfigLength) {
67 var errorMessage = '[WEBPACK] There is a difference between the amount of the'
68 + ' provided configs. Maybe you where expecting command line'
69 + ' arguments to be passed to your webpack.config.js. If so,'
70 + " you'll need to separate them with a -- from the parallel-webpack options.";
71 console.error(errorMessage);
72 return Promise.reject(errorMessage);
73 }
74 }
75 if(Array.isArray(config)) {
76 config = config[index];
77 }
78 Promise.resolve(config).then(function(webpackConfig) {
79 var webpack = getWebpack(),
80 outputOptions = getOutputOptions(webpackConfig, options),
81 finishedCallback = function(err, stats) {
82 if(err) {
83 console.error('%s fatal error occured', chalk.red('[WEBPACK]'));
84 console.error(err);
85 done(err);
86 }
87 if(stats.compilation.errors && stats.compilation.errors.length) {
88 var message = chalk.red('[WEBPACK]') + ' Errors building ' + chalk.yellow(getAppName(webpackConfig)) + "\n"
89 + stats.compilation.errors.map(function(error) {
90 return error.message;
91 }).join("\n");
92 if(watch) {
93 console.log(message);
94 } else {
95 return done({
96 message: message,
97 stats: JSON.stringify(stats.toJson(outputOptions), null, 2)
98 });
99 }
100 }
101 if(!silent) {
102 if(options.stats) {
103 console.log(stats.toString(outputOptions));
104 }
105 console.log('%s Finished building %s within %s seconds', chalk.blue('[WEBPACK]'), chalk.yellow(getAppName(webpackConfig)), chalk.blue((stats.endTime - stats.startTime) / 1000));
106 }
107 if(!watch) {
108 done(null, options.stats ? JSON.stringify(stats.toJson(outputOptions), null, 2) : '');
109 }
110 };
111 if(!silent) {
112 console.log('%s Started %s %s', chalk.blue('[WEBPACK]'), watch ? 'watching' : 'building', chalk.yellow(getAppName(webpackConfig)));
113 }
114 var compiler = webpack(webpackConfig),
115 watcher;
116 if(watch || webpack.watch) {
117 watcher = compiler.watch({}, finishedCallback);
118 } else {
119 compiler.run(finishedCallback);
120 }
121
122 process.on('SIGINT', function() {
123 if(watcher) watcher.close(done);
124 done({
125 message: chalk.red('[WEBPACK]') + ' Forcefully shut down ' + chalk.yellow(getAppName(webpackConfig))
126 });
127 process.exit(0);
128 });
129 });
130};