From 3c77b07212d827184e55d5b7a0c281bdca93706a Mon Sep 17 00:00:00 2001 From: Ryan Harvey Date: Thu, 20 Jul 2017 14:21:50 -0500 Subject: [PATCH 1/2] add CLI option for selecting a config from a multi-config --- bin/config-optimist.js | 1 + bin/config-yargs.js | 6 ++++++ bin/convert-argv.js | 14 ++++++++++++++ test/binCases/config-name/found-many/index.js | 1 + test/binCases/config-name/found-many/index2.js | 1 + test/binCases/config-name/found-many/index3.js | 0 test/binCases/config-name/found-many/test.js | 11 +++++++++++ test/binCases/config-name/found-many/test.opts | 5 +++++ .../config-name/found-many/webpack.config.js | 16 ++++++++++++++++ test/binCases/config-name/found-one/index.js | 1 + test/binCases/config-name/found-one/index2.js | 1 + test/binCases/config-name/found-one/test.js | 10 ++++++++++ test/binCases/config-name/found-one/test.opts | 5 +++++ .../config-name/found-one/webpack.config.js | 12 ++++++++++++ test/binCases/config-name/not-found/test.js | 9 +++++++++ test/binCases/config-name/not-found/test.opts | 2 ++ .../config-name/not-found/webpack.config.js | 3 +++ 17 files changed, 98 insertions(+) create mode 100644 test/binCases/config-name/found-many/index.js create mode 100644 test/binCases/config-name/found-many/index2.js create mode 100644 test/binCases/config-name/found-many/index3.js create mode 100644 test/binCases/config-name/found-many/test.js create mode 100644 test/binCases/config-name/found-many/test.opts create mode 100644 test/binCases/config-name/found-many/webpack.config.js create mode 100644 test/binCases/config-name/found-one/index.js create mode 100644 test/binCases/config-name/found-one/index2.js create mode 100644 test/binCases/config-name/found-one/test.js create mode 100644 test/binCases/config-name/found-one/test.opts create mode 100644 test/binCases/config-name/found-one/webpack.config.js create mode 100644 test/binCases/config-name/not-found/test.js create mode 100644 test/binCases/config-name/not-found/test.opts create mode 100644 test/binCases/config-name/not-found/webpack.config.js diff --git a/bin/config-optimist.js b/bin/config-optimist.js index 5ea07ee14e2..6ec4d62c873 100644 --- a/bin/config-optimist.js +++ b/bin/config-optimist.js @@ -2,6 +2,7 @@ module.exports = function(optimist) { optimist .boolean("help").alias("help", "h").alias("help", "?").describe("help") .string("config").describe("config", "Path to the config file") + .string("config-name").describe("config-name", "Name of the config to use") .string("env").describe("env", "Environment passed to the config, when it is a function") .string("context").describe("context", "The root directory for resolving entry point and stats") .string("entry").describe("entry", "The entry point") diff --git a/bin/config-yargs.js b/bin/config-yargs.js index 79ab734a317..993d6c9fcf4 100644 --- a/bin/config-yargs.js +++ b/bin/config-yargs.js @@ -20,6 +20,12 @@ module.exports = function(yargs) { defaultDescription: "webpack.config.js or webpackfile.js", requiresArg: true }, + "config-name": { + type: "string", + describe: "Name of the config to use", + group: CONFIG_GROUP, + requiresArg: true + }, "env": { describe: "Environment passed to the config, when it is a function", group: CONFIG_GROUP diff --git a/bin/convert-argv.js b/bin/convert-argv.js index f7e8d27d0db..fa7ca1a0b9e 100644 --- a/bin/convert-argv.js +++ b/bin/convert-argv.js @@ -130,6 +130,20 @@ module.exports = function(yargs, argv, convertOptions) { return processConfiguredOptions(options.default); } + // filter multi-config by name + if(Array.isArray(options) && argv["config-name"]) { + var namedOptions = options.filter(function(opt) { + return opt.name === argv["config-name"]; + }); + if(namedOptions.length === 0) { + console.error("Configuration with name '" + argv["config-name"] + "' was not found."); + process.exit(-1); // eslint-disable-line + } else if(namedOptions.length === 1) { + return processConfiguredOptions(namedOptions[0]); + } + options = namedOptions; + } + if(Array.isArray(options)) { options.forEach(processOptions); } else { diff --git a/test/binCases/config-name/found-many/index.js b/test/binCases/config-name/found-many/index.js new file mode 100644 index 00000000000..e7134e7006d --- /dev/null +++ b/test/binCases/config-name/found-many/index.js @@ -0,0 +1 @@ +module.exports = "foo"; diff --git a/test/binCases/config-name/found-many/index2.js b/test/binCases/config-name/found-many/index2.js new file mode 100644 index 00000000000..a7f281456cc --- /dev/null +++ b/test/binCases/config-name/found-many/index2.js @@ -0,0 +1 @@ +module.exports = "bar"; diff --git a/test/binCases/config-name/found-many/index3.js b/test/binCases/config-name/found-many/index3.js new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/binCases/config-name/found-many/test.js b/test/binCases/config-name/found-many/test.js new file mode 100644 index 00000000000..2dfc3454a88 --- /dev/null +++ b/test/binCases/config-name/found-many/test.js @@ -0,0 +1,11 @@ +"use strict"; + +module.exports = function testAssertions(code, stdout, stderr) { + code.should.be.exactly(0); + + stdout.should.be.ok(); + stdout[7].should.containEql("./index2.js"); + stdout[13].should.containEql("./index3.js"); + stderr.should.be.empty(); +}; + diff --git a/test/binCases/config-name/found-many/test.opts b/test/binCases/config-name/found-many/test.opts new file mode 100644 index 00000000000..b9b00798672 --- /dev/null +++ b/test/binCases/config-name/found-many/test.opts @@ -0,0 +1,5 @@ +--config ./webpack.config.js +--config-name bar +--output-filename [name].js +--output-chunk-filename [id].chunk.js +--target async-node diff --git a/test/binCases/config-name/found-many/webpack.config.js b/test/binCases/config-name/found-many/webpack.config.js new file mode 100644 index 00000000000..2f6d0872765 --- /dev/null +++ b/test/binCases/config-name/found-many/webpack.config.js @@ -0,0 +1,16 @@ +var path = require("path"); + +module.exports = [ + { + name: "foo", + entry: path.resolve(__dirname, "./index") + }, + { + name: "bar", + entry: path.resolve(__dirname, "./index2") + }, + { + name: "bar", + entry: path.resolve(__dirname, "./index3.js") + } +]; diff --git a/test/binCases/config-name/found-one/index.js b/test/binCases/config-name/found-one/index.js new file mode 100644 index 00000000000..e7134e7006d --- /dev/null +++ b/test/binCases/config-name/found-one/index.js @@ -0,0 +1 @@ +module.exports = "foo"; diff --git a/test/binCases/config-name/found-one/index2.js b/test/binCases/config-name/found-one/index2.js new file mode 100644 index 00000000000..a7f281456cc --- /dev/null +++ b/test/binCases/config-name/found-one/index2.js @@ -0,0 +1 @@ +module.exports = "bar"; diff --git a/test/binCases/config-name/found-one/test.js b/test/binCases/config-name/found-one/test.js new file mode 100644 index 00000000000..78982f4c1d9 --- /dev/null +++ b/test/binCases/config-name/found-one/test.js @@ -0,0 +1,10 @@ +"use strict"; + +module.exports = function testAssertions(code, stdout, stderr) { + code.should.be.exactly(0); + + stdout.should.be.ok(); + stdout[5].should.containEql("./index2.js"); + stderr.should.be.empty(); +}; + diff --git a/test/binCases/config-name/found-one/test.opts b/test/binCases/config-name/found-one/test.opts new file mode 100644 index 00000000000..b9b00798672 --- /dev/null +++ b/test/binCases/config-name/found-one/test.opts @@ -0,0 +1,5 @@ +--config ./webpack.config.js +--config-name bar +--output-filename [name].js +--output-chunk-filename [id].chunk.js +--target async-node diff --git a/test/binCases/config-name/found-one/webpack.config.js b/test/binCases/config-name/found-one/webpack.config.js new file mode 100644 index 00000000000..cfeca70c7a6 --- /dev/null +++ b/test/binCases/config-name/found-one/webpack.config.js @@ -0,0 +1,12 @@ +var path = require("path"); + +module.exports = [ + { + name: "foo", + entry: path.resolve(__dirname, "./index") + }, + { + name: "bar", + entry: path.resolve(__dirname, "./index2") + } +]; diff --git a/test/binCases/config-name/not-found/test.js b/test/binCases/config-name/not-found/test.js new file mode 100644 index 00000000000..fbbcdd0854b --- /dev/null +++ b/test/binCases/config-name/not-found/test.js @@ -0,0 +1,9 @@ +"use strict"; + +module.exports = function testAssertions(code, stdout, stderr) { + code.should.be.exactly(255); + + stdout.should.be.empty(); + stderr[0].should.containEql("Configuration with name \'foo\' was not found."); +}; + diff --git a/test/binCases/config-name/not-found/test.opts b/test/binCases/config-name/not-found/test.opts new file mode 100644 index 00000000000..dfe77dcedc7 --- /dev/null +++ b/test/binCases/config-name/not-found/test.opts @@ -0,0 +1,2 @@ +--config ./webpack.config.js +--config-name foo diff --git a/test/binCases/config-name/not-found/webpack.config.js b/test/binCases/config-name/not-found/webpack.config.js new file mode 100644 index 00000000000..7c22117219d --- /dev/null +++ b/test/binCases/config-name/not-found/webpack.config.js @@ -0,0 +1,3 @@ +module.exports = [{ + name: "bar" +}]; From cd907e6a5f978333a763338a6801495efd38a5b0 Mon Sep 17 00:00:00 2001 From: Ryan Harvey Date: Thu, 20 Jul 2017 14:38:25 -0500 Subject: [PATCH 2/2] make test work on all platforms --- test/binCases/config-name/not-found/test.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/binCases/config-name/not-found/test.js b/test/binCases/config-name/not-found/test.js index fbbcdd0854b..d8cb53b704d 100644 --- a/test/binCases/config-name/not-found/test.js +++ b/test/binCases/config-name/not-found/test.js @@ -1,8 +1,7 @@ "use strict"; module.exports = function testAssertions(code, stdout, stderr) { - code.should.be.exactly(255); - + code.should.not.eql(0); stdout.should.be.empty(); stderr[0].should.containEql("Configuration with name \'foo\' was not found."); };