Skip to content

Commit 4c6eb6f

Browse files
authored
Merge pull request webpack#5331 from rrharvey/feature/cli-config-name
Add CLI option for selecting a named config from a multi-config
2 parents 048b300 + cd907e6 commit 4c6eb6f

17 files changed

Lines changed: 97 additions & 0 deletions

File tree

bin/config-optimist.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module.exports = function(optimist) {
22
optimist
33
.boolean("help").alias("help", "h").alias("help", "?").describe("help")
44
.string("config").describe("config", "Path to the config file")
5+
.string("config-name").describe("config-name", "Name of the config to use")
56
.string("env").describe("env", "Environment passed to the config, when it is a function")
67
.string("context").describe("context", "The root directory for resolving entry point and stats")
78
.string("entry").describe("entry", "The entry point")

bin/config-yargs.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ module.exports = function(yargs) {
2020
defaultDescription: "webpack.config.js or webpackfile.js",
2121
requiresArg: true
2222
},
23+
"config-name": {
24+
type: "string",
25+
describe: "Name of the config to use",
26+
group: CONFIG_GROUP,
27+
requiresArg: true
28+
},
2329
"env": {
2430
describe: "Environment passed to the config, when it is a function",
2531
group: CONFIG_GROUP

bin/convert-argv.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,20 @@ module.exports = function(yargs, argv, convertOptions) {
130130
return processConfiguredOptions(options.default);
131131
}
132132

133+
// filter multi-config by name
134+
if(Array.isArray(options) && argv["config-name"]) {
135+
var namedOptions = options.filter(function(opt) {
136+
return opt.name === argv["config-name"];
137+
});
138+
if(namedOptions.length === 0) {
139+
console.error("Configuration with name '" + argv["config-name"] + "' was not found.");
140+
process.exit(-1); // eslint-disable-line
141+
} else if(namedOptions.length === 1) {
142+
return processConfiguredOptions(namedOptions[0]);
143+
}
144+
options = namedOptions;
145+
}
146+
133147
if(Array.isArray(options)) {
134148
options.forEach(processOptions);
135149
} else {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "foo";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "bar";

test/binCases/config-name/found-many/index3.js

Whitespace-only changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"use strict";
2+
3+
module.exports = function testAssertions(code, stdout, stderr) {
4+
code.should.be.exactly(0);
5+
6+
stdout.should.be.ok();
7+
stdout[7].should.containEql("./index2.js");
8+
stdout[13].should.containEql("./index3.js");
9+
stderr.should.be.empty();
10+
};
11+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
--config ./webpack.config.js
2+
--config-name bar
3+
--output-filename [name].js
4+
--output-chunk-filename [id].chunk.js
5+
--target async-node
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var path = require("path");
2+
3+
module.exports = [
4+
{
5+
name: "foo",
6+
entry: path.resolve(__dirname, "./index")
7+
},
8+
{
9+
name: "bar",
10+
entry: path.resolve(__dirname, "./index2")
11+
},
12+
{
13+
name: "bar",
14+
entry: path.resolve(__dirname, "./index3.js")
15+
}
16+
];
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "foo";

0 commit comments

Comments
 (0)