Skip to content

Commit

Permalink
refactor: code
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Apr 19, 2021
1 parent bb14837 commit 00ed5a9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/webpack-cli/lib/webpack-cli.js
Expand Up @@ -374,10 +374,12 @@ class WebpackCLI {
name: 'env',
type: (value, previous = {}) => {
// for https://github.com/webpack/webpack-cli/issues/2642
const regExpForSplitting = (value.match(/=/g) || []).length === 1 ? /=/ : /=(.+)/;
if (value.endsWith('=')) {
value.concat(`''`);
}

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

let prevRef = previous;
Expand Down
10 changes: 10 additions & 0 deletions test/build/config/type/function-with-env/function-with-env.test.js
Expand Up @@ -127,6 +127,16 @@ describe('function configuration', () => {
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
8 changes: 8 additions & 0 deletions test/build/config/type/function-with-env/webpack.config.js
Expand Up @@ -17,6 +17,14 @@ module.exports = (env) => {
},
};
}
if (env.foo === `bar=''`) {
return {
entry: './a.js',
output: {
filename: 'new-empty-string.js',
},
};
}
return {
entry: './a.js',
mode: 'development',
Expand Down

0 comments on commit 00ed5a9

Please sign in to comment.