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

fix: pass all argv to configurations when serve command used #2345

Merged
merged 4 commits into from
Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ jobs:
yarn build:ci
yarn run lerna bootstrap

- name: Run Smoketests
run: yarn test:smoketests
# - name: Run Smoketests
# run: yarn test:smoketests

- name: Test and Generate Coverage
run: |
Expand Down
2 changes: 1 addition & 1 deletion packages/serve/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class ServeCommand {
processor(devServerOptions);
}

webpackOptions.env = { WEBPACK_SERVE: true, ...options.env };
webpackOptions.argv = { ...options, env: { WEBPACK_SERVE: true, ...options.env } };

const compiler = await cli.createCompiler(webpackOptions);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('resolveConfig', function () {

it('should handle different env formats', async () => {
const result = await resolveConfig({
env: { test: true, name: 'Hisoka' },
argv: { env: { test: true, name: 'Hisoka' } },
config: [resolve(__dirname, './env.webpack.config.cjs')],
});
const expectedOptions = { mode: 'staging', name: 'Hisoka' };
Expand Down
14 changes: 8 additions & 6 deletions packages/webpack-cli/lib/webpack-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ class WebpackCLI {
return { options, path: configPath };
};

const evaluateConfig = async (loadedConfig, args) => {
const evaluateConfig = async (loadedConfig, argv) => {
const isMultiCompiler = Array.isArray(loadedConfig.options);
const config = isMultiCompiler ? loadedConfig.options : [loadedConfig.options];

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

return rawConfig;
Expand Down Expand Up @@ -824,7 +824,7 @@ class WebpackCLI {

const loadedConfig = await loadConfig(configPath);

return evaluateConfig(loadedConfig, options);
return evaluateConfig(loadedConfig, options.argv || {});
}),
);

Expand Down Expand Up @@ -867,7 +867,7 @@ class WebpackCLI {

if (foundDefaultConfigFile) {
const loadedConfig = await loadConfig(foundDefaultConfigFile.path);
const evaluatedConfig = await evaluateConfig(loadedConfig, options);
const evaluatedConfig = await evaluateConfig(loadedConfig, options.argv || {});

config.options = evaluatedConfig.options;

Expand Down Expand Up @@ -1275,7 +1275,9 @@ class WebpackCLI {
: undefined;

// TODO webpack@4 doesn't support `{ children: [{ colors: true }, { colors: true }] }` for stats
if (compiler.compilers && !compiler.compilers.find(oneOfCompiler => oneOfCompiler.webpack)) {
const statsForWebpack4 = webpack.Stats && webpack.Stats.presetToOptions;

if (compiler.compilers && statsForWebpack4) {
statsOptions.colors = statsOptions.children.some((child) => child.colors);
}

Expand Down Expand Up @@ -1311,7 +1313,7 @@ class WebpackCLI {
}
};

options.env = { WEBPACK_BUNDLE: true, ...options.env };
options.argv = { ...options, env: { WEBPACK_BUNDLE: true, ...options.env } };

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('function configuration', () => {
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
expect(stdout).toContain("{ argv: { mode: 'development', env: { WEBPACK_BUNDLE: true } } }");
expect(stdout).toContain("mode: 'development'");
expect(existsSync(resolve(__dirname, './dist/dev.js')));
});
});
11 changes: 11 additions & 0 deletions test/serve/basic/function-with-argv.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const WebpackCLITestPlugin = require('../../utils/webpack-cli-test-plugin');

module.exports = (env, argv) => {
console.log(argv);

return {
mode: 'development',
devtool: false,
plugins: [new WebpackCLITestPlugin(['mode'], false, 'hooks.compilation.taps')],
};
};
11 changes: 11 additions & 0 deletions test/serve/basic/function-with-env.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const WebpackCLITestPlugin = require('../../utils/webpack-cli-test-plugin');

module.exports = (env) => {
console.log(env);

return {
mode: 'development',
devtool: false,
plugins: [new WebpackCLITestPlugin(['mode'], false, 'hooks.compilation.taps')],
};
};
46 changes: 46 additions & 0 deletions test/serve/basic/serve-basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,52 @@ describe('basic serve usage', () => {
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
});

it('should work with the "--config" option', async () => {
const { stderr, stdout } = await runServe(__dirname, ['serve', '--config', 'webpack.config.js', '--port', port]);

expect(stderr).toBeFalsy();
expect(stdout).toContain('development');
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
});

it('should work with the "--config" and "--env" options', async () => {
const { stderr, stdout } = await runServe(__dirname, [
'serve',
'--config',
'function-with-env.config.js',
'--env',
'foo=bar',
'--port',
port,
]);

expect(stderr).toBeFalsy();
expect(stdout).toContain('WEBPACK_SERVE: true');
expect(stdout).toContain("foo: 'bar'");
expect(stdout).toContain('development');
expect(stdout.match(/HotModuleReplacementPlugin/g)).toBeNull();
});

it('should work with the "--config" and "--env" options and expose dev server options', async () => {
const { stderr, stdout } = await runServe(__dirname, [
'serve',
'--config',
'function-with-argv.config.js',
'--env',
'foo=bar',
'--hot',
'--port',
port,
]);

expect(stderr).toBeFalsy();
expect(stdout).toContain('hot: true');
expect(stdout).toContain('WEBPACK_SERVE: true');
expect(stdout).toContain("foo: 'bar'");
expect(stdout).toContain('development');
expect(stdout.match(/HotModuleReplacementPlugin/g)).toHaveLength(1);
});

it('should work in multi compiler mode', async () => {
const { stderr, stdout } = await runServe(__dirname, ['serve', '--config', 'multi.config.js', '--port', port]);

Expand Down
15 changes: 15 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2245,6 +2245,21 @@
p-each-series "^2.1.0"
p-lazy "^3.0.0"

"@webpack-cli/utils@^1.2.1":
version "1.2.1"
resolved "https://registry.yarnpkg.com/@webpack-cli/utils/-/utils-1.2.1.tgz#b3a847fc803e0ff661833b147b02f9f886a54d87"
integrity sha512-Q28u+uBYqmRGRhRNhBfi5jnlUBIEtvSsIhSIS81BCvc/nADNdNlYrqKn3FLJ2KgE7ZmEhKf0cvarcwLvy1BCOQ==
dependencies:
colorette "^1.2.1"
execa "^5.0.0"
findup-sync "^4.0.0"
global-modules "^2.0.0"
got "^11.8.0"
jscodeshift "^0.11.0"
p-each-series "^2.1.0"
yeoman-environment "^2.10.3"
yeoman-generator "^4.12.0"

"@xtuc/ieee754@^1.2.0":
version "1.2.0"
resolved "https://registry.yarnpkg.com/@xtuc/ieee754/-/ieee754-1.2.0.tgz#eef014a3145ae477a1cbc00cd1e552336dceb790"
Expand Down