diff --git a/packages/webpack-cli/lib/webpack-cli.js b/packages/webpack-cli/lib/webpack-cli.js index b8b0db06b69..2a7f54f98a2 100644 --- a/packages/webpack-cli/lib/webpack-cli.js +++ b/packages/webpack-cli/lib/webpack-cli.js @@ -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; diff --git a/test/build/config/type/function-with-env/function-with-env.test.js b/test/build/config/type/function-with-env/function-with-env.test.js index 2c07fb01697..cdb80700457 100644 --- a/test/build/config/type/function-with-env/function-with-env.test.js +++ b/test/build/config/type/function-with-env/function-with-env.test.js @@ -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']); diff --git a/test/build/config/type/function-with-env/webpack.config.js b/test/build/config/type/function-with-env/webpack.config.js index 8fe8deb18e8..9114623d545 100644 --- a/test/build/config/type/function-with-env/webpack.config.js +++ b/test/build/config/type/function-with-env/webpack.config.js @@ -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',