Skip to content

Commit bc12f1a

Browse files
authored
fix: parsing of empty --env flags (#2643)
1 parent d2ab57d commit bc12f1a

File tree

6 files changed

+85
-7
lines changed

6 files changed

+85
-7
lines changed

packages/webpack-cli/lib/webpack-cli.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,11 @@ class WebpackCLI {
373373
{
374374
name: 'env',
375375
type: (value, previous = {}) => {
376+
// for https://github.com/webpack/webpack-cli/issues/2642
377+
if (value.endsWith('=')) {
378+
value.concat('""');
379+
}
380+
376381
// This ensures we're only splitting by the first `=`
377382
const [allKeys, val] = value.split(/=(.+)/, 2);
378383
const splitKeys = allKeys.split(/\.(?!$)/);
@@ -389,7 +394,11 @@ class WebpackCLI {
389394
}
390395

391396
if (index === splitKeys.length - 1) {
392-
prevRef[someKey] = val || true;
397+
if (typeof val === 'string') {
398+
prevRef[someKey] = val;
399+
} else {
400+
prevRef[someKey] = true;
401+
}
393402
}
394403

395404
prevRef = prevRef[someKey];
@@ -1212,13 +1221,13 @@ class WebpackCLI {
12121221
const defaultCommandToRun = getCommandName(buildCommandOptions.name);
12131222
const hasOperand = typeof operands[0] !== 'undefined';
12141223
const operand = hasOperand ? operands[0] : defaultCommandToRun;
1215-
1224+
const isHelpOption = typeof options.help !== 'undefined';
12161225
const isHelpCommandSyntax = isCommand(operand, helpCommandOptions);
12171226

1218-
if (options.help || isHelpCommandSyntax) {
1227+
if (isHelpOption || isHelpCommandSyntax) {
12191228
let isVerbose = false;
12201229

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

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

1246-
if (options.version || isCommand(operand, versionCommandOptions)) {
1255+
const isVersionOption = typeof options.version !== 'undefined';
1256+
const isVersionCommandSyntax = isCommand(operand, versionCommandOptions);
1257+
1258+
if (isVersionOption || isVersionCommandSyntax) {
12471259
const optionsForVersion = []
1248-
.concat(options.version ? [operand] : [])
1260+
.concat(isVersionOption ? [operand] : [])
12491261
.concat(operands.slice(1))
12501262
.concat(unknown);
12511263

test/build/config/type/function-with-env/function-with-env.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,36 @@ describe('function configuration', () => {
117117
expect(existsSync(resolve(__dirname, './dist/true.js'))).toBeTruthy();
118118
});
119119

120+
it('Supports empty string', async () => {
121+
const { exitCode, stderr, stdout } = await run(__dirname, ['--env', `foo=''`]);
122+
123+
expect(exitCode).toBe(0);
124+
expect(stderr).toBeFalsy();
125+
expect(stdout).toBeTruthy();
126+
// Should generate the appropriate files
127+
expect(existsSync(resolve(__dirname, './dist/empty-string.js'))).toBeTruthy();
128+
});
129+
130+
it('Supports empty string with multiple "="', async () => {
131+
const { exitCode, stderr, stdout } = await run(__dirname, ['--env', `foo=bar=''`]);
132+
133+
expect(exitCode).toBe(0);
134+
expect(stderr).toBeFalsy();
135+
expect(stdout).toBeTruthy();
136+
// Should generate the appropriate files
137+
expect(existsSync(resolve(__dirname, './dist/new-empty-string.js'))).toBeTruthy();
138+
});
139+
140+
it('Supports env variable with "=" at the end', async () => {
141+
const { exitCode, stderr, stdout } = await run(__dirname, ['--env', `foo=`]);
142+
143+
expect(exitCode).toBe(0);
144+
expect(stderr).toBeFalsy();
145+
expect(stdout).toBeTruthy();
146+
// Should generate the appropriate files
147+
expect(existsSync(resolve(__dirname, './dist/equal-at-the-end.js'))).toBeTruthy();
148+
});
149+
120150
it('is able to understand multiple env flags', async () => {
121151
const { exitCode, stderr, stdout } = await run(__dirname, ['--env', 'isDev', '--env', 'verboseStats', '--env', 'envMessage']);
122152

test/build/config/type/function-with-env/webpack.config.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,30 @@ module.exports = (env) => {
99
},
1010
};
1111
}
12+
if (env.foo === `''`) {
13+
return {
14+
entry: './a.js',
15+
output: {
16+
filename: 'empty-string.js',
17+
},
18+
};
19+
}
20+
if (env.foo === `bar=''`) {
21+
return {
22+
entry: './a.js',
23+
output: {
24+
filename: 'new-empty-string.js',
25+
},
26+
};
27+
}
28+
if (env['foo=']) {
29+
return {
30+
entry: './a.js',
31+
output: {
32+
filename: 'equal-at-the-end.js',
33+
},
34+
};
35+
}
1236
return {
1337
entry: './a.js',
1438
mode: 'development',

test/help/__snapshots__/help.test.js.snap.webpack4

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ exports[`help should log error for invalid command using the "--help" option 1`]
2828

2929
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'"`;
3030

31+
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'"`;
32+
3133
exports[`help should log error for invalid flag with the "--help" option 1`] = `
3234
"[webpack-cli] Incorrect use of help
3335
[webpack-cli] Please use: 'webpack help [command] [option]' | 'webpack [command] --help'

test/help/__snapshots__/help.test.js.snap.webpack5

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ exports[`help should log error for invalid command using the "--help" option 1`]
2828

2929
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'"`;
3030

31+
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'"`;
32+
3133
exports[`help should log error for invalid flag with the "--help" option 1`] = `
3234
"[webpack-cli] Incorrect use of help
3335
[webpack-cli] Please use: 'webpack help [command] [option]' | 'webpack [command] --help'

test/help/help.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,4 +402,12 @@ describe('help', () => {
402402
expect(stderr).toMatchSnapshot();
403403
expect(stdout).toBeFalsy();
404404
});
405+
406+
it('should log error for invalid flag with the "--help" option #2', async () => {
407+
const { exitCode, stderr, stdout } = await run(__dirname, ['--help=']);
408+
409+
expect(exitCode).toBe(2);
410+
expect(stderr).toMatchSnapshot();
411+
expect(stdout).toBeFalsy();
412+
});
405413
});

0 commit comments

Comments
 (0)