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

feat: support multiple env params #1715

Merged
merged 6 commits into from Aug 1, 2020
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
9 changes: 8 additions & 1 deletion packages/webpack-cli/lib/groups/ConfigGroup.js
Expand Up @@ -96,7 +96,14 @@ class ConfigGroup extends GroupHelper {
const configOptions = moduleObj.content;
if (typeof configOptions === 'function') {
// when config is a function, pass the env from args to the config function
const newOptions = configOptions(this.args.env);
let formattedEnv;
if (Array.isArray(this.args.env)) {
formattedEnv = this.args.env.reduce((envObject, envOption) => {
envObject[envOption] = true;
return envObject;
}, {});
}
const newOptions = configOptions(formattedEnv);
// When config function returns a promise, resolve it, if not it's resolved by default
newOptionsObject['options'] = await Promise.resolve(newOptions);
} else {
Expand Down
1 change: 1 addition & 0 deletions packages/webpack-cli/lib/utils/cli-flags.js
Expand Up @@ -257,6 +257,7 @@ module.exports = {
name: 'env',
usage: '--env',
type: String,
multiple: true,
group: CONFIG_GROUP,
description: 'Environment passed to the configuration when it is a function',
},
Expand Down
12 changes: 3 additions & 9 deletions test/config/type/function-with-env/function-with-env.test.js
@@ -1,5 +1,5 @@
'use strict';
const { stat } = require('fs');
const { existsSync } = require('fs');
const { resolve } = require('path');
const { run } = require('../../../utils/test-utils');

Expand All @@ -9,19 +9,13 @@ describe('function configuration', () => {
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
// Should generate the appropriate files
stat(resolve(__dirname, './bin/prod.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
});
expect(existsSync(resolve(__dirname, './bin/prod.js'))).toBeTruthy();
});
it('is able to understand a configuration file as a function', () => {
const { stderr, stdout } = run(__dirname, ['--env', 'isDev']);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
// Should generate the appropriate files
stat(resolve(__dirname, './bin/dev.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
});
expect(existsSync(resolve(__dirname, './bin/dev.js'))).toBeTruthy();
});
});
2 changes: 1 addition & 1 deletion test/config/type/function-with-env/webpack.config.js
@@ -1,5 +1,5 @@
module.exports = (env) => {
if (env === 'isProd') {
if (env.isProd) {
return {
entry: './a.js',
output: {
Expand Down