From d3154431e559f736d8beaf6ca98b12a59b80e9bd Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Sat, 1 Aug 2020 17:29:44 +0530 Subject: [PATCH] feat: support multiple env params (#1715) * feat: support multiple env params * tests: add tests for multiple env * tests: add tests for multiple env * tests: add tests for multiple env * tests: add tests for multiple env --- .../webpack-cli/lib/groups/ConfigGroup.js | 9 +++++++- packages/webpack-cli/lib/utils/cli-flags.js | 1 + test/config/type/function-with-env/a.js | 4 ++++ .../function-with-env.test.js | 21 ++++++++++++------- .../type/function-with-env/webpack.config.js | 6 +++++- 5 files changed, 32 insertions(+), 9 deletions(-) diff --git a/packages/webpack-cli/lib/groups/ConfigGroup.js b/packages/webpack-cli/lib/groups/ConfigGroup.js index 21471bc6785..c4ad3ecf2c5 100644 --- a/packages/webpack-cli/lib/groups/ConfigGroup.js +++ b/packages/webpack-cli/lib/groups/ConfigGroup.js @@ -96,7 +96,14 @@ class ConfigGroup extends GroupHelper { const configOptions = moduleObj.content; if (typeof configOptions === 'function') { // when config is a function, pass the env from args to the config function - const newOptions = configOptions(this.args.env); + let formattedEnv; + if (Array.isArray(this.args.env)) { + formattedEnv = this.args.env.reduce((envObject, envOption) => { + envObject[envOption] = true; + return envObject; + }, {}); + } + const newOptions = configOptions(formattedEnv); // When config function returns a promise, resolve it, if not it's resolved by default newOptionsObject['options'] = await Promise.resolve(newOptions); } else { diff --git a/packages/webpack-cli/lib/utils/cli-flags.js b/packages/webpack-cli/lib/utils/cli-flags.js index ade0bb1bf6e..4b7a18570f3 100644 --- a/packages/webpack-cli/lib/utils/cli-flags.js +++ b/packages/webpack-cli/lib/utils/cli-flags.js @@ -257,6 +257,7 @@ module.exports = { name: 'env', usage: '--env', type: String, + multiple: true, group: CONFIG_GROUP, description: 'Environment passed to the configuration when it is a function', }, diff --git a/test/config/type/function-with-env/a.js b/test/config/type/function-with-env/a.js index ebe8dc7e539..542cfb7c49e 100644 --- a/test/config/type/function-with-env/a.js +++ b/test/config/type/function-with-env/a.js @@ -1 +1,5 @@ console.log('chuntaro'); +// eslint-disable-next-line no-undef +if (envMessage) { + console.log('env message present'); +} diff --git a/test/config/type/function-with-env/function-with-env.test.js b/test/config/type/function-with-env/function-with-env.test.js index d4721d7417c..fae211de366 100644 --- a/test/config/type/function-with-env/function-with-env.test.js +++ b/test/config/type/function-with-env/function-with-env.test.js @@ -1,5 +1,5 @@ 'use strict'; -const { stat } = require('fs'); +const { existsSync, readFile } = require('fs'); const { resolve } = require('path'); const { run } = require('../../../utils/test-utils'); @@ -9,19 +9,26 @@ describe('function configuration', () => { expect(stderr).toBeFalsy(); expect(stdout).toBeTruthy(); // Should generate the appropriate files - stat(resolve(__dirname, './bin/prod.js'), (err, stats) => { - expect(err).toBe(null); - expect(stats.isFile()).toBe(true); - }); + expect(existsSync(resolve(__dirname, './bin/prod.js'))).toBeTruthy(); }); it('is able to understand a configuration file as a function', () => { const { stderr, stdout } = run(__dirname, ['--env', 'isDev']); expect(stderr).toBeFalsy(); expect(stdout).toBeTruthy(); // Should generate the appropriate files - stat(resolve(__dirname, './bin/dev.js'), (err, stats) => { + expect(existsSync(resolve(__dirname, './bin/dev.js'))).toBeTruthy(); + }); + it('is able to understand multiple env flags', (done) => { + const { stderr, stdout } = run(__dirname, ['--env', 'isDev', '--env', 'verboseStats', '--env', 'envMessage']); + expect(stderr).toBeFalsy(); + expect(stdout).toBeTruthy(); + // check that the verbose env is respected + expect(stdout).toContain('LOG from webpack'); + // check if the values from DefinePlugin make it to the compiled code + readFile(resolve(__dirname, './bin/dev.js'), 'utf-8', (err, data) => { expect(err).toBe(null); - expect(stats.isFile()).toBe(true); + expect(data).toContain('env message present'); + done(); }); }); }); diff --git a/test/config/type/function-with-env/webpack.config.js b/test/config/type/function-with-env/webpack.config.js index c2275d1b48f..35efedbbb0d 100644 --- a/test/config/type/function-with-env/webpack.config.js +++ b/test/config/type/function-with-env/webpack.config.js @@ -1,5 +1,7 @@ +const { DefinePlugin } = require('webpack'); + module.exports = (env) => { - if (env === 'isProd') { + if (env.isProd) { return { entry: './a.js', output: { @@ -10,6 +12,8 @@ module.exports = (env) => { return { entry: './a.js', mode: 'development', + stats: env.verboseStats ? 'verbose' : 'normal', + plugins: [new DefinePlugin({ envMessage: env.envMessage ? JSON.stringify('env message present') : false })], output: { filename: 'dev.js', },