Skip to content

Commit

Permalink
feat: allow nested env properties
Browse files Browse the repository at this point in the history
  • Loading branch information
anshumanv committed Oct 15, 2020
1 parent 2d1d363 commit 4cf57f5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
16 changes: 14 additions & 2 deletions packages/webpack-cli/lib/utils/arg-parser.js
Expand Up @@ -95,8 +95,20 @@ const argParser = (options, args, argsOnly = false, name = '') => {
// so you can do `--env platform=staging --env production`
// { platform: "staging", production: true }
const multiArg = (value, previous = {}) => {
const [key, val] = value.split('=');
return { ...previous, [key]: val || true };
const [allKeys, val] = value.split('=');
const splitKeys = allKeys.split('.');
let prevRef = previous;
splitKeys.forEach((someKey, index) => {
if (!prevRef[someKey]) prevRef[someKey] = {};
if (index === splitKeys.length - 1) {
prevRef[someKey] = val || true;
} else {
prevRef[someKey] = typeof prevRef[someKey] === 'string' ? {} : { ...prevRef[someKey] };
}
console.log({ prevRef, previous, someKey });
prevRef = prevRef[someKey];
});
return previous;
};
parserInstance.option(flagsWithType, option.description, multiArg, option.defaultValue).action(() => {});
} else {
Expand Down
11 changes: 9 additions & 2 deletions test/config/type/function-with-env/function-with-env.test.js
Expand Up @@ -19,11 +19,18 @@ describe('function configuration', () => {
expect(existsSync(resolve(__dirname, './bin/dev.js'))).toBeTruthy();
});
it('Supports passing string in env', () => {
const { stderr, stdout } = run(__dirname, ['--env', 'environment=production', '-c', 'webpack.env.config.js']);
const { stderr, stdout } = run(__dirname, [
'--env',
'environment=production',
'--env',
'app.title=Luffy',
'-c',
'webpack.env.config.js',
]);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
// Should generate the appropriate files
expect(existsSync(resolve(__dirname, './bin/prod.js'))).toBeTruthy();
expect(existsSync(resolve(__dirname, './bin/Luffy.js'))).toBeTruthy();
});
it('is able to understand multiple env flags', (done) => {
const { stderr, stdout } = run(__dirname, ['--env', 'isDev', '--env', 'verboseStats', '--env', 'envMessage']);
Expand Down
7 changes: 5 additions & 2 deletions test/config/type/function-with-env/webpack.env.config.js
@@ -1,10 +1,13 @@
module.exports = (env) => {
const { environment } = env;
const {
environment,
app: { title },
} = env;
if (environment === 'production') {
return {
entry: './a.js',
output: {
filename: 'prod.js',
filename: `${title}.js`,
},
};
}
Expand Down

0 comments on commit 4cf57f5

Please sign in to comment.