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: log error if --config-name is used without multiple configs #1874

Merged
merged 1 commit into from Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 14 additions & 8 deletions packages/webpack-cli/lib/groups/ConfigGroup.js
Expand Up @@ -163,21 +163,27 @@ const finalize = async (moduleObj, args) => {
const newOptions = configOptions(formattedEnv, args);
// When config function returns a promise, resolve it, if not it's resolved by default
newOptionsObject['options'] = await Promise.resolve(newOptions);
} else if (Array.isArray(configOptions) && configName) {
// In case of exporting multiple configurations, If you pass a name to --config-name flag,
// webpack will only build that specific configuration.
const namedOptions = configOptions.filter((opt) => configName.includes(opt.name));
if (namedOptions.length === 0) {
logger.error(`Configuration with name "${configName}" was not found.`);
process.exit(2);
} else if (configName) {
if (Array.isArray(configOptions) && configOptions.length > 1) {
// In case of exporting multiple configurations, If you pass a name to --config-name flag,
// webpack will only build that specific configuration.
const namedOptions = configOptions.filter((opt) => configName.includes(opt.name));
if (namedOptions.length === 0) {
logger.error(`Configuration with name "${configName}" was not found.`);
process.exit(2);
} else {
newOptionsObject['options'] = namedOptions;
}
} else {
newOptionsObject['options'] = namedOptions;
logger.error('Multiple configurations not found. Please use "--config-name" with multiple configurations.');
process.exit(2);
}
} else {
if (Array.isArray(configOptions) && !configOptions.length) {
newOptionsObject['options'] = {};
return newOptionsObject;
}

newOptionsObject['options'] = configOptions;
}

Expand Down
6 changes: 6 additions & 0 deletions test/config-name/config-name.test.js
Expand Up @@ -43,4 +43,10 @@ describe('--config-name flag', () => {
expect(stderr).toContain('Configuration with name "test" was not found.');
expect(stdout).toBeFalsy();
});

it('should log error if multiple configurations are not found', () => {
const { stderr, stdout } = run(__dirname, ['--config-name', 'test', '-c', 'single-config.js'], false);
expect(stderr).toContain('Multiple configurations not found. Please use "--config-name" with multiple configurations');
expect(stdout).toBeFalsy();
});
});
8 changes: 8 additions & 0 deletions test/config-name/single-config.js
@@ -0,0 +1,8 @@
module.exports = {
output: {
filename: './dist-single.js',
},
name: 'first',
entry: './src/first.js',
mode: 'development',
};