Skip to content

Commit

Permalink
feat(webpack-cli): add --config-name flag
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Aug 18, 2020
1 parent d3e2936 commit 7aedac7
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 0 deletions.
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
9 changes: 9 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,14 @@ 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) {
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(1);
} 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',
},
];

0 comments on commit 7aedac7

Please sign in to comment.