Skip to content

Commit 479a130

Browse files
committed
fix(options): allow passing promise function of webpack.config.js
1 parent a0b9c70 commit 479a130

File tree

5 files changed

+46
-10
lines changed

5 files changed

+46
-10
lines changed

lib/utils/processOptions.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ const defaultPort = require('./defaultPort');
66
function processOptions(config, argv, callback) {
77
// processOptions {Promise}
88
if (typeof config.then === 'function') {
9-
config.then(processOptions).catch((err) => {
10-
// eslint-disable-next-line no-console
11-
console.error(err.stack || err);
12-
// eslint-disable-next-line no-process-exit
13-
process.exit();
14-
});
9+
config
10+
.then((conf) => processOptions(conf, argv, callback))
11+
.catch((err) => {
12+
// eslint-disable-next-line no-console
13+
console.error(err.stack || err);
14+
// eslint-disable-next-line no-process-exit
15+
process.exit(1);
16+
});
1517

1618
return;
1719
}

test/cli.test.js

+12
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,18 @@ describe('CLI', () => {
100100
.catch(done);
101101
});
102102

103+
it('should accept the promise function of webpack.config.js', (done) => {
104+
testBin(
105+
false,
106+
resolve(__dirname, 'fixtures/promise-config/webpack.config.js')
107+
)
108+
.then((output) => {
109+
expect(output.code).toEqual(0);
110+
done();
111+
})
112+
.catch(done);
113+
});
114+
103115
it('should exit the process when SIGINT is detected', (done) => {
104116
const cliPath = resolve(__dirname, '../bin/webpack-dev-server.js');
105117
const examplePath = resolve(__dirname, '../examples/cli/public');

test/fixtures/promise-config/foo.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
console.log('i am foo!');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
const { join } = require('path');
4+
5+
module.exports = () => {
6+
return new Promise((resolve) => {
7+
resolve({
8+
mode: 'development',
9+
entry: join(__dirname, 'foo.js'),
10+
});
11+
});
12+
};

test/helpers/test-bin.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const basicConfigPath = path.resolve(
1212
'../fixtures/cli/webpack.config.js'
1313
);
1414

15-
function runWebackDevServer(testArgs, configPath) {
15+
function testBin(testArgs, configPath) {
1616
const cwd = process.cwd();
1717
const env = process.env.NODE_ENV;
1818
let stdout = '';
@@ -36,14 +36,21 @@ function runWebackDevServer(testArgs, configPath) {
3636
child.on('error', (error) => reject(error));
3737

3838
child.stdout.on('data', (data) => {
39-
stdout += data.toString();
39+
const str = data.toString();
40+
41+
stdout += str;
42+
43+
// if webpack.config.js is a promise function, it won't be able to call `close`
44+
if (str.includes('Compiled successfully.')) {
45+
child.kill('SIGINT');
46+
}
4047
});
4148

4249
child.stderr.on('data', (data) => {
4350
stderr += data.toString();
4451
});
4552

46-
child.on('close', (code) => {
53+
child.on('exit', (code) => {
4754
if (code !== 0) {
4855
return reject(stderr);
4956
}
@@ -52,4 +59,4 @@ function runWebackDevServer(testArgs, configPath) {
5259
});
5360
}
5461

55-
module.exports = runWebackDevServer;
62+
module.exports = testBin;

0 commit comments

Comments
 (0)