Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(options): allow passing promise function of webpack.config.js #1947

Merged
merged 2 commits into from Jun 1, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
16 changes: 16 additions & 0 deletions test/cli.test.js
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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

windows 😠

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'),
});
});
};
27 changes: 3 additions & 24 deletions test/helpers/test-bin.js
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;