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: 🎸 add support for env flag #1598

Merged
merged 5 commits into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/webpack-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Options
-c, --config string Provide path to a webpack configuration file
-m, --merge string Merge a configuration file using webpack-merge
--progress Print compilation progress during build
--env Environment passed to the configuration when it is a function
--silent Disable any output that webpack makes
--help Outputs list of supported flags
-o, --output string Output location of the file generated by webpack
Expand Down
7 changes: 3 additions & 4 deletions packages/webpack-cli/lib/groups/ConfigGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,9 @@ class ConfigGroup extends GroupHelper {
}
const configPath = moduleObj.path;
const configOptions = moduleObj.content;
if (configOptions.length > 0) {
newOptionsObject['options'] = configOptions;
} else if (typeof configOptions === 'function') {
const newOptions = configOptions();
if (typeof configOptions === 'function') {
// when config is a function, pass the env from args to the config function
const newOptions = configOptions(this.args.env);
newOptionsObject['options'] = newOptions;
} else {
if (Array.isArray(configOptions) && !configOptions.length) {
Expand Down
7 changes: 7 additions & 0 deletions packages/webpack-cli/lib/utils/cli-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,13 @@ module.exports = {
group: DISPLAY_GROUP,
description: 'It tells webpack to output all the information',
},
{
name: 'env',
usage: '--env',
type: String,
group: CONFIG_GROUP,
description: 'Environment passed to the configuration when it is a function',
},
/* {
name: "analyze",
type: Boolean,
Expand Down
1 change: 1 addition & 0 deletions test/config/type/function-with-env/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('chuntaro');
27 changes: 27 additions & 0 deletions test/config/type/function-with-env/function-with-env.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';
const { stat } = require('fs');
const { resolve } = require('path');
const { run } = require('../../../utils/test-utils');

describe('function configuration', () => {
it('is able to understand a configuration file as a function', () => {
const { stderr, stdout } = run(__dirname, ['--env', 'isProd']);
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);
});
});
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);
});
});
});
17 changes: 17 additions & 0 deletions test/config/type/function-with-env/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = (env) => {
if (env === 'isProd') {
return {
entry: './a.js',
output: {
filename: 'prod.js',
},
};
}
return {
entry: './a.js',
mode: 'development',
output: {
filename: 'dev.js',
},
};
};