forked from trivago/parallel-webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
169 lines (153 loc) · 5.47 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
var workerFarm = require('worker-farm'),
Ajv = require('ajv'),
Promise = require('bluebird'),
chalk = require('chalk'),
assign = require('lodash.assign'),
pluralize = require('pluralize'),
schema = require('./schema.json'),
loadConfigurationFile = require('./src/loadConfigurationFile').default,
startWatchIPCServer = require('./src/watchModeIPC').startWatchIPCServer;
var ajv = new Ajv({
allErrors: true,
coerceTypes: true,
removeAdditional: 'all',
useDefaults: true
});
var validate = ajv.compile(schema);
function notSilent(options) {
return !options.json;
}
function startFarm(config, configPath, options, runWorker, callback) {
return Promise.resolve(config).then(function(config) {
config = Array.isArray(config) ? config : [config];
options = options || {};
// When in watch mode and a callback is provided start IPC server to invoke callback
// once all webpack configurations have been compiled
if (options.watch) {
startWatchIPCServer(callback, Object.keys(config));
}
if(notSilent(options)) {
console.log(chalk.blue('[WEBPACK]') + ' Building ' + chalk.yellow(config.length) + ' ' + pluralize('target', config.length));
}
var builds = config.map(function (c, i) {
return runWorker(configPath, options, i, config.length);
});
if(options.bail) {
return Promise.all(builds);
} else {
return Promise.settle(builds).then(function(results) {
return Promise.all(results.map(function (result) {
if(result.isFulfilled()) {
return result.value();
}
return Promise.reject(result.reason());
}));
});
}
})
}
/**
* Runs the specified webpack configuration in parallel.
* @param {String} configPath The path to the webpack.config.js
* @param {Object} options
* @param {Boolean} [options.watch=false] If `true`, Webpack will run in
* `watch-mode`.
* @param {Number} [options.maxCallsPerWorker=Infinity] The maximum amount of calls
* per parallel worker
* @param {Number} [options.maxConcurrentWorkers=require('os').cpus().length] The
* maximum number of parallel workers
* @param {Number} [options.maxConcurrentCallsPerWorker=10] The maximum number of
* concurrent call per prallel worker
* @param {Number} [options.maxConcurrentCalls=Infinity] The maximum number of
* concurrent calls
* @param {Number} [options.maxRetries=0] The maximum amount of retries
* on build error
* @param {Function} [callback] A callback to be invoked once the build has
* been completed
* @return {Promise} A Promise that is resolved once all builds have been
* created
*/
function run(configPath, options, callback) {
var config,
argvBackup = process.argv,
farmOptions = assign({}, options);
options = options || {};
if(options.colors === undefined) {
options.colors = chalk.supportsColor;
}
if(!options.argv) {
options.argv = [];
}
options.argv.unshift(process.execPath, 'parallel-webpack');
try {
process.argv = options.argv;
config = loadConfigurationFile(configPath);
process.argv = argvBackup;
} catch(e) {
process.argv = argvBackup;
return Promise.reject(new Error(
chalk.red('[WEBPACK]') + ' Could not load configuration file ' + chalk.underline(configPath) + "\n"
+ e
));
}
if (!validate(farmOptions)) {
return Promise.reject(new Error(
'Options validation failed:\n' +
validate.errors.map(function(error) {
return 'Property: "options' + error.dataPath + '" ' + error.message;
}).join('\n')
));
}
var workers = workerFarm(farmOptions, require.resolve('./src/webpackWorker'));
var shutdownCallback = function() {
if (notSilent(options)) {
console.log(chalk.red('[WEBPACK]') + ' Forcefully shutting down');
}
workerFarm.end(workers);
};
function keepAliveAfterFinishCallback(cb){
if(options.keepAliveAfterFinish){
setTimeout(cb, options.keepAliveAfterFinish);
} else {
cb();
}
}
function finalCallback(){
workerFarm.end(workers);
process.removeListener("SIGINT", shutdownCallback);
}
process.on('SIGINT', shutdownCallback);
var startTime = Date.now();
var farmPromise = startFarm(
config,
configPath,
options,
Promise.promisify(workers),
callback
).error(function(err) {
if(notSilent(options)) {
console.log('%s Build failed after %s seconds', chalk.red('[WEBPACK]'), chalk.blue((Date.now() - startTime) / 1000));
}
return Promise.reject(err);
}).then(function (results) {
if(notSilent(options)) {
console.log('%s Finished build after %s seconds', chalk.blue('[WEBPACK]'), chalk.blue((Date.now() - startTime) / 1000));
}
results = results.filter(function(result) {
return result;
});
if(results.length) {
return results;
}
}).finally(function() {
keepAliveAfterFinishCallback(finalCallback);
});
if (!options.watch) {
farmPromise.asCallback(callback);
}
return farmPromise;
}
module.exports = {
createVariants: require('./src/createVariants'),
run: run
};