Skip to content

Commit 5070b9b

Browse files
fix: pass all argv to configurations when serve command used (#2345)
1 parent debb857 commit 5070b9b

File tree

9 files changed

+96
-10
lines changed

9 files changed

+96
-10
lines changed

.github/workflows/nodejs.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ jobs:
9292
yarn build:ci
9393
yarn run lerna bootstrap
9494
95-
- name: Run Smoketests
96-
run: yarn test:smoketests
95+
# - name: Run Smoketests
96+
# run: yarn test:smoketests
9797

9898
- name: Test and Generate Coverage
9999
run: |

packages/serve/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class ServeCommand {
7373
processor(devServerOptions);
7474
}
7575

76-
webpackOptions.env = { WEBPACK_SERVE: true, ...options.env };
76+
webpackOptions.argv = { ...options, env: { WEBPACK_SERVE: true, ...options.env } };
7777

7878
const compiler = await cli.createCompiler(webpackOptions);
7979

packages/webpack-cli/__tests__/resolveConfig/resolveConfig.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ describe('resolveConfig', function () {
6060

6161
it('should handle different env formats', async () => {
6262
const result = await resolveConfig({
63-
env: { test: true, name: 'Hisoka' },
63+
argv: { env: { test: true, name: 'Hisoka' } },
6464
config: [resolve(__dirname, './env.webpack.config.cjs')],
6565
});
6666
const expectedOptions = { mode: 'staging', name: 'Hisoka' };

packages/webpack-cli/lib/webpack-cli.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ class WebpackCLI {
778778
return { options, path: configPath };
779779
};
780780

781-
const evaluateConfig = async (loadedConfig, args) => {
781+
const evaluateConfig = async (loadedConfig, argv) => {
782782
const isMultiCompiler = Array.isArray(loadedConfig.options);
783783
const config = isMultiCompiler ? loadedConfig.options : [loadedConfig.options];
784784

@@ -791,7 +791,7 @@ class WebpackCLI {
791791
// `Promise` may return `Function`
792792
if (typeof rawConfig === 'function') {
793793
// when config is a function, pass the env from args to the config function
794-
rawConfig = await rawConfig(args.env, args);
794+
rawConfig = await rawConfig(argv.env, argv);
795795
}
796796

797797
return rawConfig;
@@ -824,7 +824,7 @@ class WebpackCLI {
824824

825825
const loadedConfig = await loadConfig(configPath);
826826

827-
return evaluateConfig(loadedConfig, options);
827+
return evaluateConfig(loadedConfig, options.argv || {});
828828
}),
829829
);
830830

@@ -867,7 +867,7 @@ class WebpackCLI {
867867

868868
if (foundDefaultConfigFile) {
869869
const loadedConfig = await loadConfig(foundDefaultConfigFile.path);
870-
const evaluatedConfig = await evaluateConfig(loadedConfig, options);
870+
const evaluatedConfig = await evaluateConfig(loadedConfig, options.argv || {});
871871

872872
config.options = evaluatedConfig.options;
873873

@@ -1275,7 +1275,9 @@ class WebpackCLI {
12751275
: undefined;
12761276

12771277
// TODO webpack@4 doesn't support `{ children: [{ colors: true }, { colors: true }] }` for stats
1278-
if (compiler.compilers && !compiler.compilers.find(oneOfCompiler => oneOfCompiler.webpack)) {
1278+
const statsForWebpack4 = webpack.Stats && webpack.Stats.presetToOptions;
1279+
1280+
if (compiler.compilers && statsForWebpack4) {
12791281
statsOptions.colors = statsOptions.children.some((child) => child.colors);
12801282
}
12811283

@@ -1311,7 +1313,7 @@ class WebpackCLI {
13111313
}
13121314
};
13131315

1314-
options.env = { WEBPACK_BUNDLE: true, ...options.env };
1316+
options.argv = { ...options, env: { WEBPACK_BUNDLE: true, ...options.env } };
13151317

13161318
compiler = await this.createCompiler(options, callback);
13171319

test/config/type/function-with-argv/function-with-argv.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ describe('function configuration', () => {
1111
expect(stderr).toBeFalsy();
1212
expect(stdout).toBeTruthy();
1313
expect(stdout).toContain("{ argv: { mode: 'development', env: { WEBPACK_BUNDLE: true } } }");
14+
expect(stdout).toContain("mode: 'development'");
1415
expect(existsSync(resolve(__dirname, './dist/dev.js')));
1516
});
1617
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const WebpackCLITestPlugin = require('../../utils/webpack-cli-test-plugin');
2+
3+
module.exports = (env, argv) => {
4+
console.log(argv);
5+
6+
return {
7+
mode: 'development',
8+
devtool: false,
9+
plugins: [new WebpackCLITestPlugin(['mode'], false, 'hooks.compilation.taps')],
10+
};
11+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const WebpackCLITestPlugin = require('../../utils/webpack-cli-test-plugin');
2+
3+
module.exports = (env) => {
4+
console.log(env);
5+
6+
return {
7+
mode: 'development',
8+
devtool: false,
9+
plugins: [new WebpackCLITestPlugin(['mode'], false, 'hooks.compilation.taps')],
10+
};
11+
};

test/serve/basic/serve-basic.test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,52 @@ describe('basic serve usage', () => {
3535
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
3636
});
3737

38+
it('should work with the "--config" option', async () => {
39+
const { stderr, stdout } = await runServe(__dirname, ['serve', '--config', 'webpack.config.js', '--port', port]);
40+
41+
expect(stderr).toBeFalsy();
42+
expect(stdout).toContain('development');
43+
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
44+
});
45+
46+
it('should work with the "--config" and "--env" options', async () => {
47+
const { stderr, stdout } = await runServe(__dirname, [
48+
'serve',
49+
'--config',
50+
'function-with-env.config.js',
51+
'--env',
52+
'foo=bar',
53+
'--port',
54+
port,
55+
]);
56+
57+
expect(stderr).toBeFalsy();
58+
expect(stdout).toContain('WEBPACK_SERVE: true');
59+
expect(stdout).toContain("foo: 'bar'");
60+
expect(stdout).toContain('development');
61+
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
62+
});
63+
64+
it('should work with the "--config" and "--env" options and expose dev server options', async () => {
65+
const { stderr, stdout } = await runServe(__dirname, [
66+
'serve',
67+
'--config',
68+
'function-with-argv.config.js',
69+
'--env',
70+
'foo=bar',
71+
'--hot',
72+
'--port',
73+
port,
74+
]);
75+
76+
expect(stderr).toBeFalsy();
77+
expect(stdout).toContain('hot: true');
78+
expect(stdout).toContain('WEBPACK_SERVE: true');
79+
expect(stdout).toContain("foo: 'bar'");
80+
expect(stdout).toContain('development');
81+
expect(stdout.match(/HotModuleReplacementPlugin/g)).toHaveLength(1);
82+
});
83+
3884
it('should work in multi compiler mode', async () => {
3985
const { stderr, stdout } = await runServe(__dirname, ['serve', '--config', 'multi.config.js', '--port', port]);
4086

yarn.lock

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2245,6 +2245,21 @@
22452245
p-each-series "^2.1.0"
22462246
p-lazy "^3.0.0"
22472247

2248+
"@webpack-cli/utils@^1.2.1":
2249+
version "1.2.1"
2250+
resolved "https://registry.yarnpkg.com/@webpack-cli/utils/-/utils-1.2.1.tgz#b3a847fc803e0ff661833b147b02f9f886a54d87"
2251+
integrity sha512-Q28u+uBYqmRGRhRNhBfi5jnlUBIEtvSsIhSIS81BCvc/nADNdNlYrqKn3FLJ2KgE7ZmEhKf0cvarcwLvy1BCOQ==
2252+
dependencies:
2253+
colorette "^1.2.1"
2254+
execa "^5.0.0"
2255+
findup-sync "^4.0.0"
2256+
global-modules "^2.0.0"
2257+
got "^11.8.0"
2258+
jscodeshift "^0.11.0"
2259+
p-each-series "^2.1.0"
2260+
yeoman-environment "^2.10.3"
2261+
yeoman-generator "^4.12.0"
2262+
22482263
"@xtuc/ieee754@^1.2.0":
22492264
version "1.2.0"
22502265
resolved "https://registry.yarnpkg.com/@xtuc/ieee754/-/ieee754-1.2.0.tgz#eef014a3145ae477a1cbc00cd1e552336dceb790"

0 commit comments

Comments
 (0)