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 1 commit
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: 9 additions & 2 deletions packages/webpack-cli/lib/webpack-cli.js
Expand Up @@ -373,8 +373,11 @@ class WebpackCLI {
{
name: 'env',
type: (value, previous = {}) => {
// for https://github.com/webpack/webpack-cli/issues/2642
const regExpForSplitting = (value.match(/=/g) || []).length === 1 ? /=/ : /=(.+)/;
snitin315 marked this conversation as resolved.
Show resolved Hide resolved

// This ensures we're only splitting by the first `=`
const [allKeys, val] = value.split(/=(.+)/, 2);
const [allKeys, val] = value.split(regExpForSplitting, 2);
const splitKeys = allKeys.split(/\.(?!$)/);

let prevRef = previous;
Expand All @@ -389,7 +392,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,16 @@ 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('is able to understand multiple env flags', async () => {
const { exitCode, stderr, stdout } = await run(__dirname, ['--env', 'isDev', '--env', 'verboseStats', '--env', 'envMessage']);

Expand Down
8 changes: 8 additions & 0 deletions test/build/config/type/function-with-env/webpack.config.js
Expand Up @@ -9,6 +9,14 @@ module.exports = (env) => {
},
};
}
if (env.foo === `''`) {
return {
entry: './a.js',
output: {
filename: 'empty-string.js',
},
};
}
return {
entry: './a.js',
mode: 'development',
Expand Down