Skip to content

Commit

Permalink
fix: parsing of empty --env flags (#2643)
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 authored Apr 19, 2021
1 parent d2ab57d commit bc12f1a
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 7 deletions.
26 changes: 19 additions & 7 deletions packages/webpack-cli/lib/webpack-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,11 @@ class WebpackCLI {
{
name: 'env',
type: (value, previous = {}) => {
// for https://github.com/webpack/webpack-cli/issues/2642
if (value.endsWith('=')) {
value.concat('""');
}

// This ensures we're only splitting by the first `=`
const [allKeys, val] = value.split(/=(.+)/, 2);
const splitKeys = allKeys.split(/\.(?!$)/);
Expand All @@ -389,7 +394,11 @@ class WebpackCLI {
}

if (index === splitKeys.length - 1) {
prevRef[someKey] = val || true;
if (typeof val === 'string') {
prevRef[someKey] = val;
} else {
prevRef[someKey] = true;
}
}

prevRef = prevRef[someKey];
Expand Down Expand Up @@ -1212,13 +1221,13 @@ class WebpackCLI {
const defaultCommandToRun = getCommandName(buildCommandOptions.name);
const hasOperand = typeof operands[0] !== 'undefined';
const operand = hasOperand ? operands[0] : defaultCommandToRun;

const isHelpOption = typeof options.help !== 'undefined';
const isHelpCommandSyntax = isCommand(operand, helpCommandOptions);

if (options.help || isHelpCommandSyntax) {
if (isHelpOption || isHelpCommandSyntax) {
let isVerbose = false;

if (options.help) {
if (isHelpOption) {
if (typeof options.help === 'string') {
if (options.help !== 'verbose') {
this.logger.error("Unknown value for '--help' option, please use '--help=verbose'");
Expand All @@ -1232,7 +1241,7 @@ class WebpackCLI {
this.program.forHelp = true;

const optionsForHelp = []
.concat(options.help && hasOperand ? [operand] : [])
.concat(isHelpOption && hasOperand ? [operand] : [])
// Syntax `webpack help [command]`
.concat(operands.slice(1))
// Syntax `webpack help [option]`
Expand All @@ -1243,9 +1252,12 @@ class WebpackCLI {
await outputHelp(optionsForHelp, isVerbose, isHelpCommandSyntax, program);
}

if (options.version || isCommand(operand, versionCommandOptions)) {
const isVersionOption = typeof options.version !== 'undefined';
const isVersionCommandSyntax = isCommand(operand, versionCommandOptions);

if (isVersionOption || isVersionCommandSyntax) {
const optionsForVersion = []
.concat(options.version ? [operand] : [])
.concat(isVersionOption ? [operand] : [])
.concat(operands.slice(1))
.concat(unknown);

Expand Down
30 changes: 30 additions & 0 deletions test/build/config/type/function-with-env/function-with-env.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,36 @@ describe('function configuration', () => {
expect(existsSync(resolve(__dirname, './dist/true.js'))).toBeTruthy();
});

it('Supports empty string', async () => {
const { exitCode, stderr, stdout } = await run(__dirname, ['--env', `foo=''`]);

expect(exitCode).toBe(0);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
// Should generate the appropriate files
expect(existsSync(resolve(__dirname, './dist/empty-string.js'))).toBeTruthy();
});

it('Supports empty string with multiple "="', async () => {
const { exitCode, stderr, stdout } = await run(__dirname, ['--env', `foo=bar=''`]);

expect(exitCode).toBe(0);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
// Should generate the appropriate files
expect(existsSync(resolve(__dirname, './dist/new-empty-string.js'))).toBeTruthy();
});

it('Supports env variable with "=" at the end', async () => {
const { exitCode, stderr, stdout } = await run(__dirname, ['--env', `foo=`]);

expect(exitCode).toBe(0);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
// Should generate the appropriate files
expect(existsSync(resolve(__dirname, './dist/equal-at-the-end.js'))).toBeTruthy();
});

it('is able to understand multiple env flags', async () => {
const { exitCode, stderr, stdout } = await run(__dirname, ['--env', 'isDev', '--env', 'verboseStats', '--env', 'envMessage']);

Expand Down
24 changes: 24 additions & 0 deletions test/build/config/type/function-with-env/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,30 @@ module.exports = (env) => {
},
};
}
if (env.foo === `''`) {
return {
entry: './a.js',
output: {
filename: 'empty-string.js',
},
};
}
if (env.foo === `bar=''`) {
return {
entry: './a.js',
output: {
filename: 'new-empty-string.js',
},
};
}
if (env['foo=']) {
return {
entry: './a.js',
output: {
filename: 'equal-at-the-end.js',
},
};
}
return {
entry: './a.js',
mode: 'development',
Expand Down
2 changes: 2 additions & 0 deletions test/help/__snapshots__/help.test.js.snap.webpack4
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ exports[`help should log error for invalid command using the "--help" option 1`]

exports[`help should log error for invalid flag with the "--help" option #2 1`] = `"[webpack-cli] Unknown value for '--help' option, please use '--help=verbose'"`;

exports[`help should log error for invalid flag with the "--help" option #2 2`] = `"[webpack-cli] Unknown value for '--help' option, please use '--help=verbose'"`;

exports[`help should log error for invalid flag with the "--help" option 1`] = `
"[webpack-cli] Incorrect use of help
[webpack-cli] Please use: 'webpack help [command] [option]' | 'webpack [command] --help'
Expand Down
2 changes: 2 additions & 0 deletions test/help/__snapshots__/help.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ exports[`help should log error for invalid command using the "--help" option 1`]

exports[`help should log error for invalid flag with the "--help" option #2 1`] = `"[webpack-cli] Unknown value for '--help' option, please use '--help=verbose'"`;

exports[`help should log error for invalid flag with the "--help" option #2 2`] = `"[webpack-cli] Unknown value for '--help' option, please use '--help=verbose'"`;

exports[`help should log error for invalid flag with the "--help" option 1`] = `
"[webpack-cli] Incorrect use of help
[webpack-cli] Please use: 'webpack help [command] [option]' | 'webpack [command] --help'
Expand Down
8 changes: 8 additions & 0 deletions test/help/help.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,4 +402,12 @@ describe('help', () => {
expect(stderr).toMatchSnapshot();
expect(stdout).toBeFalsy();
});

it('should log error for invalid flag with the "--help" option #2', async () => {
const { exitCode, stderr, stdout } = await run(__dirname, ['--help=']);

expect(exitCode).toBe(2);
expect(stderr).toMatchSnapshot();
expect(stdout).toBeFalsy();
});
});

0 comments on commit bc12f1a

Please sign in to comment.