From 8cf1053068b34f0e4d9d636e47747743d3e5961c Mon Sep 17 00:00:00 2001 From: Yuta Hiroto Date: Sat, 1 Jun 2019 16:30:30 +0200 Subject: [PATCH] fix: allow passing promise function of webpack.config.js (#1947) --- lib/utils/processOptions.js | 14 +++++----- test/cli.test.js | 16 +++++++++++ test/fixtures/promise-config/foo.js | 3 +++ .../fixtures/promise-config/webpack.config.js | 12 +++++++++ test/helpers/test-bin.js | 27 +++---------------- 5 files changed, 42 insertions(+), 30 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..d9b7b72b41 100644 --- a/test/cli.test.js +++ b/test/cli.test.js @@ -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'); 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..25e3af020a 100644 --- a/test/helpers/test-bin.js +++ b/test/helpers/test-bin.js @@ -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; @@ -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;