Skip to content

Commit

Permalink
fix(options): allow passing promise function of webpack.config.js
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroppy committed Jun 1, 2019
1 parent a0b9c70 commit 7bd8212
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 9 deletions.
14 changes: 8 additions & 6 deletions lib/utils/processOptions.js
Expand Up @@ -6,12 +6,14 @@ const defaultPort = require('./defaultPort');
function processOptions(config, argv, callback) {
// processOptions {Promise}
if (typeof config.then === 'function') {
config.then(processOptions).catch((err) => {
// eslint-disable-next-line no-console
console.error(err.stack || err);
// eslint-disable-next-line no-process-exit
process.exit();
});
config
.then((conf) => processOptions(conf, argv, callback))
.catch((err) => {
// eslint-disable-next-line no-console
console.error(err.stack || err);
// eslint-disable-next-line no-process-exit
process.exit(1);
});

return;
}
Expand Down
12 changes: 12 additions & 0 deletions test/cli.test.js
Expand Up @@ -100,6 +100,18 @@ describe('CLI', () => {
.catch(done);
});

it('should accept the function of webpack.config.js', (done) => {
testBin(
false,
resolve(__dirname, './fixtures/promise-config/webpack.config.js')
)
.then((output) => {
expect(output.code).toEqual(0);
done();
})
.catch(done);
});

it('should exit the process when SIGINT is detected', (done) => {
const cliPath = resolve(__dirname, '../bin/webpack-dev-server.js');
const examplePath = resolve(__dirname, '../examples/cli/public');
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/promise-config/foo.js
@@ -0,0 +1,3 @@
'use strict';

console.log('i am foo!');
12 changes: 12 additions & 0 deletions test/fixtures/promise-config/webpack.config.js
@@ -0,0 +1,12 @@
'use strict';

const { join } = require('path');

module.exports = () => {
return new Promise((resolve) => {
resolve({
mode: 'development',
entry: join(__dirname, 'foo.js'),
});
});
};
18 changes: 15 additions & 3 deletions test/helpers/test-bin.js
Expand Up @@ -12,7 +12,7 @@ const basicConfigPath = path.resolve(
'../fixtures/cli/webpack.config.js'
);

function runWebackDevServer(testArgs, configPath) {
function testBin(testArgs, configPath) {
const cwd = process.cwd();
const env = process.env.NODE_ENV;
let stdout = '';
Expand All @@ -36,13 +36,25 @@ function runWebackDevServer(testArgs, configPath) {
child.on('error', (error) => reject(error));

child.stdout.on('data', (data) => {
stdout += data.toString();
const str = data.toString();

stdout += str;

// if webpack.config.js is a promise function, it won't be able to call `close`
if (str.includes('Compiled successfully.')) {
child.kill();
}
});

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

child.on('end', () => {
console.log('end');
});

// cannot catch when function is promise
child.on('close', (code) => {
if (code !== 0) {
return reject(stderr);
Expand All @@ -52,4 +64,4 @@ function runWebackDevServer(testArgs, configPath) {
});
}

module.exports = runWebackDevServer;
module.exports = testBin;

0 comments on commit 7bd8212

Please sign in to comment.