Skip to content

Commit

Permalink
feat: allow passing strings in env flag
Browse files Browse the repository at this point in the history
  • Loading branch information
anshumanv committed Oct 13, 2020
1 parent 258219a commit 2d1d363
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
10 changes: 1 addition & 9 deletions packages/webpack-cli/lib/groups/ConfigGroup.js
Expand Up @@ -153,15 +153,7 @@ const finalize = async (moduleObj, args) => {
const configOptions = moduleObj.content;

if (typeof configOptions === 'function') {
// when config is a function, pass the env from args to the config function
let formattedEnv;
if (Array.isArray(env)) {
formattedEnv = env.reduce((envObject, envOption) => {
envObject[envOption] = true;
return envObject;
}, {});
}
const newOptions = configOptions(formattedEnv, args);
const newOptions = configOptions(env, args);
// When config function returns a promise, resolve it, if not it's resolved by default
newOptionsObject['options'] = await Promise.resolve(newOptions);
} else if (configName) {
Expand Down
9 changes: 9 additions & 0 deletions packages/webpack-cli/lib/utils/arg-parser.js
Expand Up @@ -90,6 +90,15 @@ const argParser = (options, args, argsOnly = false, name = '') => {
// a multiple argument parsing function
const multiArg = (value, previous = []) => previous.concat([value]);
parserInstance.option(flagsWithType, option.description, multiArg, option.defaultValue).action(() => {});
} else if (option.multipleType) {
// for options which accept multiple types like env
// so you can do `--env platform=staging --env production`
// { platform: "staging", production: true }
const multiArg = (value, previous = {}) => {
const [key, val] = value.split('=');
return { ...previous, [key]: val || true };
};
parserInstance.option(flagsWithType, option.description, multiArg, option.defaultValue).action(() => {});
} else {
// Prevent default behavior for standalone options
parserInstance.option(flagsWithType, option.description, option.defaultValue).action(() => {});
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack-cli/lib/utils/cli-flags.js
Expand Up @@ -205,7 +205,7 @@ const core = [
name: 'env',
usage: '--env',
type: String,
multiple: true,
multipleType: true,
description: 'Environment passed to the configuration when it is a function',
},
{
Expand Down
7 changes: 7 additions & 0 deletions test/config/type/function-with-env/function-with-env.test.js
Expand Up @@ -18,6 +18,13 @@ describe('function configuration', () => {
// Should generate the appropriate files
expect(existsSync(resolve(__dirname, './bin/dev.js'))).toBeTruthy();
});
it('Supports passing string in env', () => {
const { stderr, stdout } = run(__dirname, ['--env', 'environment=production', '-c', 'webpack.env.config.js']);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
// Should generate the appropriate files
expect(existsSync(resolve(__dirname, './bin/prod.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();
Expand Down
12 changes: 12 additions & 0 deletions test/config/type/function-with-env/webpack.env.config.js
@@ -0,0 +1,12 @@
module.exports = (env) => {
const { environment } = env;
if (environment === 'production') {
return {
entry: './a.js',
output: {
filename: 'prod.js',
},
};
}
return {};
};

0 comments on commit 2d1d363

Please sign in to comment.