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 4 commits
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
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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need disable because envMessage is not present in scope but instead coming from env

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.buildChunkGraph.visitModules');
// 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();
});
});
});
7 changes: 6 additions & 1 deletion test/config/type/function-with-env/webpack.config.js
@@ -1,5 +1,8 @@
const { DefinePlugin } = require('webpack');

module.exports = (env) => {
if (env === 'isProd') {
console.log({ env });
if (env.isProd) {
return {
entry: './a.js',
output: {
Expand All @@ -10,6 +13,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 })],
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need JSON.stringify, can't pass string directly 😞

output: {
filename: 'dev.js',
},
Expand Down