Skip to content
This repository was archived by the owner on Feb 7, 2022. It is now read-only.

Commit 72f3733

Browse files
committed
Properly handle config function return promise of config array
1 parent 66da53e commit 72f3733

File tree

3 files changed

+64
-43
lines changed

3 files changed

+64
-43
lines changed

index.js

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,36 @@ function notSilent(options) {
2121
}
2222

2323
function startFarm(config, configPath, options, runWorker, callback) {
24-
config = Array.isArray(config) ? config : [config];
25-
options = options || {};
26-
27-
// When in watch mode and a callback is provided start IPC server to invoke callback
28-
// once all webpack configurations have been compiled
29-
if (options.watch) {
30-
startWatchIPCServer(callback, Object.keys(config));
31-
}
24+
return Promise.resolve(config).then(function(config) {
25+
config = Array.isArray(config) ? config : [config];
26+
options = options || {};
27+
28+
// When in watch mode and a callback is provided start IPC server to invoke callback
29+
// once all webpack configurations have been compiled
30+
if (options.watch) {
31+
startWatchIPCServer(callback, Object.keys(config));
32+
}
3233

33-
if(notSilent(options)) {
34-
console.log(chalk.blue('[WEBPACK]') + ' Building ' + chalk.yellow(config.length) + ' ' + pluralize('target', config.length));
35-
}
34+
if(notSilent(options)) {
35+
console.log(chalk.blue('[WEBPACK]') + ' Building ' + chalk.yellow(config.length) + ' ' + pluralize('target', config.length));
36+
}
3637

37-
var builds = config.map(function (c, i) {
38-
return runWorker(configPath, options, i, config.length);
39-
});
40-
if(options.bail) {
41-
return Promise.all(builds);
42-
} else {
43-
return Promise.settle(builds).then(function(results) {
44-
return Promise.all(results.map(function (result) {
45-
if(result.isFulfilled()) {
46-
return result.value();
47-
}
48-
return Promise.reject(result.reason());
49-
}));
38+
var builds = config.map(function (c, i) {
39+
return runWorker(configPath, options, i, config.length);
5040
});
51-
}
41+
if(options.bail) {
42+
return Promise.all(builds);
43+
} else {
44+
return Promise.settle(builds).then(function(results) {
45+
return Promise.all(results.map(function (result) {
46+
if(result.isFulfilled()) {
47+
return result.value();
48+
}
49+
return Promise.reject(result.reason());
50+
}));
51+
});
52+
}
53+
})
5254
}
5355

5456
/**

src/__tests__/webpackWorker.spec.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ describe('webpackWorker', () => {
162162
jest.fn()
163163
);
164164
if (options.multi && options.expectedConfigs < 3) {
165-
const config = promiseMock.resolve.mock.calls[0][0];
165+
const config = promiseMock.resolve.mock.calls[0][0][options.configIndex];
166166
expect(config).toEqual({ webpack: 'config'});
167167
} else {
168168
expect(promiseMock.reject).toHaveBeenLastCalledWith(errorMessage);
@@ -181,6 +181,21 @@ describe('webpackWorker', () => {
181181
it('should fail if expectedConfigLength dont match with config.length', () => {
182182
multiConfigTest({ multi: true, configIndex: 1, expectedConfigs: 3 });
183183
});
184+
185+
it('should be able to handle config function return promise of array of config object', () => {
186+
jest.doMock('promiseReturnConfigArray', () => Promise.resolve( [{ webpack: 'config'}, { webpack: 'config2'}]), { virtual: true });
187+
const configIndex = 1
188+
const expectedConfigLength = 2
189+
webpackWorker(
190+
'promiseReturnConfigArray',
191+
{ json: true },
192+
configIndex,
193+
expectedConfigLength,
194+
jest.fn()
195+
)
196+
const config = promiseMock.resolve.mock.calls[1][0];
197+
expect(config).toEqual([{ webpack: 'config'}, { webpack: 'config2'}]);
198+
})
184199
})
185200
});
186201

src/webpackWorker.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,24 +70,28 @@ module.exports = function(configuratorFileName, options, index, expectedConfigLe
7070
process.argv = options.argv;
7171
}
7272
chalk.enabled = options.colors;
73-
var config = loadConfigurationFile(configuratorFileName),
74-
watch = !!options.watch,
75-
silent = !!options.json;
76-
if(expectedConfigLength !== 1 && !Array.isArray(config)
77-
|| Array.isArray(config) && config.length !== expectedConfigLength) {
78-
if(config.length !== expectedConfigLength) {
79-
var errorMessage = '[WEBPACK] There is a difference between the amount of the'
80-
+ ' provided configs. Maybe you where expecting command line'
81-
+ ' arguments to be passed to your webpack.config.js. If so,'
82-
+ " you'll need to separate them with a -- from the parallel-webpack options.";
83-
console.error(errorMessage);
84-
return Promise.reject(errorMessage);
73+
var config = loadConfigurationFile(configuratorFileName)
74+
75+
Promise.resolve(config).then(function(config) {
76+
var watch = !!options.watch,
77+
silent = !!options.json;
78+
if(expectedConfigLength !== 1 && !Array.isArray(config)
79+
|| Array.isArray(config) && config.length !== expectedConfigLength) {
80+
if(config.length !== expectedConfigLength) {
81+
var errorMessage = '[WEBPACK] There is a difference between the amount of the'
82+
+ ' provided configs. Maybe you where expecting command line'
83+
+ ' arguments to be passed to your webpack.config.js. If so,'
84+
+ " you'll need to separate them with a -- from the parallel-webpack options.";
85+
console.error(errorMessage);
86+
return Promise.reject(errorMessage);
87+
}
88+
}
89+
var webpackConfig;
90+
if(Array.isArray(config)) {
91+
webpackConfig = config[index];
92+
} else {
93+
webpackConfig = config
8594
}
86-
}
87-
if(Array.isArray(config)) {
88-
config = config[index];
89-
}
90-
Promise.resolve(config).then(function(webpackConfig) {
9195
var watcher,
9296
webpack = getWebpack(),
9397
hasCompletedOneCompile = false,

0 commit comments

Comments
 (0)