Skip to content

Commit

Permalink
feat: support multiple env params (#1715)
Browse files Browse the repository at this point in the history
* feat: support multiple env params

* tests: add tests for multiple env

* tests: add tests for multiple env

* tests: add tests for multiple env

* tests: add tests for multiple env
  • Loading branch information
anshumanv committed Aug 1, 2020
1 parent 319e2c4 commit d315443
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 9 deletions.
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
4 changes: 4 additions & 0 deletions test/config/type/function-with-env/a.js
@@ -1 +1,5 @@
console.log('chuntaro');
// eslint-disable-next-line no-undef
if (envMessage) {
console.log('env message present');
}
21 changes: 14 additions & 7 deletions test/config/type/function-with-env/function-with-env.test.js
@@ -1,5 +1,5 @@
'use strict';
const { stat } = require('fs');
const { existsSync, readFile } = require('fs');
const { resolve } = require('path');
const { run } = require('../../../utils/test-utils');

Expand All @@ -9,19 +9,26 @@ 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(existsSync(resolve(__dirname, './bin/dev.js'))).toBeTruthy();
});
it('is able to understand multiple env flags', (done) => {
const { stderr, stdout } = run(__dirname, ['--env', 'isDev', '--env', 'verboseStats', '--env', 'envMessage']);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
// check that the verbose env is respected
expect(stdout).toContain('LOG from webpack');
// check if the values from DefinePlugin make it to the compiled code
readFile(resolve(__dirname, './bin/dev.js'), 'utf-8', (err, data) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
expect(data).toContain('env message present');
done();
});
});
});
6 changes: 5 additions & 1 deletion test/config/type/function-with-env/webpack.config.js
@@ -1,5 +1,7 @@
const { DefinePlugin } = require('webpack');

module.exports = (env) => {
if (env === 'isProd') {
if (env.isProd) {
return {
entry: './a.js',
output: {
Expand All @@ -10,6 +12,8 @@ module.exports = (env) => {
return {
entry: './a.js',
mode: 'development',
stats: env.verboseStats ? 'verbose' : 'normal',
plugins: [new DefinePlugin({ envMessage: env.envMessage ? JSON.stringify('env message present') : false })],
output: {
filename: 'dev.js',
},
Expand Down

0 comments on commit d315443

Please sign in to comment.