Skip to content

Commit

Permalink
fix: allow passing promise function of webpack.config.js (#1947)
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroppy authored and evilebottnawi committed Jun 1, 2019
1 parent 03c2751 commit 8cf1053
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 30 deletions.
14 changes: 8 additions & 6 deletions lib/utils/processOptions.js
Original file line number Diff line number Diff line change
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
16 changes: 16 additions & 0 deletions test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,22 @@ describe('CLI', () => {
.catch(done);
});

it('should accept the promise 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((err) => {
// for windows
expect(err.stdout.includes('Compiled successfully.')).toEqual(true);
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

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

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

module.exports = () => {
return new Promise((resolve) => {
resolve({
mode: 'development',
entry: join(__dirname, 'foo.js'),
});
});
};
27 changes: 3 additions & 24 deletions test/helpers/test-bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ 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 = '';
let stderr = '';

if (!configPath) {
configPath = basicConfigPath;
Expand All @@ -30,26 +28,7 @@ function runWebackDevServer(testArgs, configPath) {

const args = [webpackDevServerPath, '--config', configPath].concat(testArgs);

return new Promise((resolve, reject) => {
const child = execa('node', args, { cwd, env });

child.on('error', (error) => reject(error));

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

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

child.on('close', (code) => {
if (code !== 0) {
return reject(stderr);
}
resolve({ stdout, stderr, code });
});
});
return execa('node', args, { cwd, env, timeout: 10000 });
}

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

0 comments on commit 8cf1053

Please sign in to comment.