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 3 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
11 changes: 10 additions & 1 deletion 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 @@ -117,6 +117,26 @@ 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('is able to understand multiple env flags', async () => {
const { exitCode, stderr, stdout } = await run(__dirname, ['--env', 'isDev', '--env', 'verboseStats', '--env', 'envMessage']);

Expand Down
16 changes: 16 additions & 0 deletions test/build/config/type/function-with-env/webpack.config.js
Expand Up @@ -9,6 +9,22 @@ 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',
},
};
}
return {
entry: './a.js',
mode: 'development',
Expand Down