1 |
|
2 |
|
3 |
|
4 |
|
5 | var Promise = require('bluebird'),
|
6 | chalk = require('chalk');
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | function getWebpack() {
|
14 | try {
|
15 | return require(process.cwd() + '/node_modules/webpack');
|
16 | } catch(e) {
|
17 | return require('webpack');
|
18 | }
|
19 | }
|
20 |
|
21 | function getAppName(webpackConfig) {
|
22 | return webpackConfig.name || webpackConfig.output.filename;
|
23 | }
|
24 |
|
25 | function 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 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | module.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 | };
|