From 979f7334c295ccd527b57661c250118599eb654f Mon Sep 17 00:00:00 2001 From: Yuta Hiroto Date: Sat, 1 Jun 2019 01:56:25 +0200 Subject: [PATCH] fix(options): allow passing promise function of webpack.config.js --- lib/utils/processOptions.js | 14 ++++++++------ test/cli.test.js | 12 ++++++++++++ test/fixtures/promise-config/foo.js | 3 +++ test/fixtures/promise-config/webpack.config.js | 12 ++++++++++++ test/helpers/test-bin.js | 13 ++++++++++--- 5 files changed, 45 insertions(+), 9 deletions(-) create mode 100644 test/fixtures/promise-config/foo.js create mode 100644 test/fixtures/promise-config/webpack.config.js diff --git a/lib/utils/processOptions.js b/lib/utils/processOptions.js index ee7138ac29..23da69b430 100644 --- a/lib/utils/processOptions.js +++ b/lib/utils/processOptions.js @@ -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; } diff --git a/test/cli.test.js b/test/cli.test.js index 966e3e584d..7aec9caeaf 100644 --- a/test/cli.test.js +++ b/test/cli.test.js @@ -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'); diff --git a/test/fixtures/promise-config/foo.js b/test/fixtures/promise-config/foo.js new file mode 100644 index 0000000000..f88d8b5040 --- /dev/null +++ b/test/fixtures/promise-config/foo.js @@ -0,0 +1,3 @@ +'use strict'; + +console.log('i am foo!'); diff --git a/test/fixtures/promise-config/webpack.config.js b/test/fixtures/promise-config/webpack.config.js new file mode 100644 index 0000000000..9265f94362 --- /dev/null +++ b/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'), + }); + }); +}; diff --git a/test/helpers/test-bin.js b/test/helpers/test-bin.js index 2c7b5e98ae..83b506ee71 100644 --- a/test/helpers/test-bin.js +++ b/test/helpers/test-bin.js @@ -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 = ''; @@ -36,7 +36,14 @@ 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) => { @@ -52,4 +59,4 @@ function runWebackDevServer(testArgs, configPath) { }); } -module.exports = runWebackDevServer; +module.exports = testBin;