Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve parsing of --env flag #2643

Merged
merged 4 commits into from Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 19 additions & 7 deletions packages/webpack-cli/lib/webpack-cli.js
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('""');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something weird with coverage here

}

// 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;
snitin315 marked this conversation as resolved.
Show resolved Hide resolved
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
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
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
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
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
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();
});
});