Skip to content

Commit 4da773d

Browse files
committed
fix: improve parsing of --env flag
1 parent d2ab57d commit 4da773d

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,11 @@ class WebpackCLI {
373373
{
374374
name: 'env',
375375
type: (value, previous = {}) => {
376+
// for https://github.com/webpack/webpack-cli/issues/2642
377+
const regExpForSplitting = (value.match(/=/g) || []).length === 1 ? /=/ : /=(.+)/;
378+
376379
// This ensures we're only splitting by the first `=`
377-
const [allKeys, val] = value.split(/=(.+)/, 2);
380+
const [allKeys, val] = value.split(regExpForSplitting, 2);
378381
const splitKeys = allKeys.split(/\.(?!$)/);
379382

380383
let prevRef = previous;
@@ -389,7 +392,11 @@ class WebpackCLI {
389392
}
390393

391394
if (index === splitKeys.length - 1) {
392-
prevRef[someKey] = val || true;
395+
if (typeof val === 'string') {
396+
prevRef[someKey] = val;
397+
} else {
398+
prevRef[someKey] = true;
399+
}
393400
}
394401

395402
prevRef = prevRef[someKey];

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ 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+
120130
it('is able to understand multiple env flags', async () => {
121131
const { exitCode, stderr, stdout } = await run(__dirname, ['--env', 'isDev', '--env', 'verboseStats', '--env', 'envMessage']);
122132

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ 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+
}
1220
return {
1321
entry: './a.js',
1422
mode: 'development',

0 commit comments

Comments
 (0)