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

feat: add --config-name flag #1753

Merged
merged 2 commits into from Aug 19, 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
1 change: 1 addition & 0 deletions packages/webpack-cli/README.md
Expand Up @@ -38,6 +38,7 @@ yarn add webpack-cli --dev
```
--entry string[] The entry point(s) of your application.
-c, --config string Provide path to a webpack configuration file
--config-name string[] Name of the configuration to use
-m, --merge string Merge a configuration file using webpack-merge
--progress Print compilation progress during build
--color Enables colors on console
Expand Down
11 changes: 11 additions & 0 deletions packages/webpack-cli/lib/groups/ConfigGroup.js
Expand Up @@ -5,6 +5,7 @@ const { extensions, jsVariants } = require('interpret');
const GroupHelper = require('../utils/GroupHelper');
const rechoir = require('rechoir');
const MergeError = require('../utils/errors/MergeError');
const logger = require('../utils/logger');

// Order defines the priority, in increasing order
// example - config file lookup will be in order of .webpack/webpack.config.development.js -> webpack.config.development.js -> webpack.config.js
Expand Down Expand Up @@ -117,6 +118,16 @@ class ConfigGroup extends GroupHelper {
const newOptions = configOptions(formattedEnv, argv);
// 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) && this.args.configName) {
Copy link
Member

Choose a reason for hiding this comment

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

Add a comment when this block is executed, can give example, looks good otherwise

// 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) => this.args.configName.includes(opt.name));
if (namedOptions.length === 0) {
logger.error(`Configuration with name "${this.args.configName}" was not found.`);
process.exit(2);
} else {
newOptionsObject['options'] = namedOptions;
}
} else {
if (Array.isArray(configOptions) && !configOptions.length) {
newOptionsObject['options'] = {};
Expand Down
8 changes: 8 additions & 0 deletions packages/webpack-cli/lib/utils/cli-flags.js
Expand Up @@ -124,6 +124,14 @@ module.exports = {
description: 'Provide path to a webpack configuration file e.g. ./webpack.config.js',
link: 'https://webpack.js.org/configuration/',
},
{
name: 'config-name',
usage: '--config-name <name of config>',
type: String,
multiple: true,
group: CONFIG_GROUP,
description: 'Name of the configuration to use',
},
{
name: 'color',
usage: '--color',
Expand Down
46 changes: 46 additions & 0 deletions test/config-name/config-name.test.js
@@ -0,0 +1,46 @@
'use strict';

const { run } = require('../utils/test-utils');
const { stat } = require('fs');
const { resolve } = require('path');

describe('--config-name flag', () => {
it('should select only the config whose name is passed with --config-name', (done) => {
const { stderr, stdout } = run(__dirname, ['--config-name', 'first'], false);
expect(stderr).toBeFalsy();
expect(stdout).toContain('Child first');
expect(stdout).not.toContain('Child second');
expect(stdout).not.toContain('Child third');

stat(resolve(__dirname, './dist/dist-first.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
done();
});
});

it('should work with multiple values for --config-name', (done) => {
const { stderr, stdout } = run(__dirname, ['--config-name', 'first', '--config-name', 'third'], false);
expect(stderr).toBeFalsy();
expect(stdout).toContain('Child first');
expect(stdout).not.toContain('Child second');
expect(stdout).toContain('Child third');

stat(resolve(__dirname, './dist/dist-first.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
done();
});
stat(resolve(__dirname, './dist/dist-third.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
done();
});
});

it('should log error if invalid config name is provided', () => {
const { stderr, stdout } = run(__dirname, ['--config-name', 'test'], false);
expect(stderr).toContain('Configuration with name "test" was not found.');
expect(stdout).toBeFalsy();
});
});
1 change: 1 addition & 0 deletions test/config-name/src/first.js
@@ -0,0 +1 @@
console.log('first config');
1 change: 1 addition & 0 deletions test/config-name/src/second.js
@@ -0,0 +1 @@
console.log('second config');
1 change: 1 addition & 0 deletions test/config-name/src/third.js
@@ -0,0 +1 @@
console.log('third config');
26 changes: 26 additions & 0 deletions test/config-name/webpack.config.js
@@ -0,0 +1,26 @@
module.exports = [
{
output: {
filename: './dist-first.js',
},
name: 'first',
entry: './src/first.js',
mode: 'development',
},
{
output: {
filename: './dist-second.js',
},
name: 'second',
entry: './src/second.js',
mode: 'production',
},
{
output: {
filename: './dist-third.js',
},
name: 'third',
entry: './src/third.js',
mode: 'none',
},
];