diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 04847d08649..7424fe6c6a0 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -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: | diff --git a/packages/serve/src/index.ts b/packages/serve/src/index.ts index 2c9e0186bb3..c70b9ced2c2 100644 --- a/packages/serve/src/index.ts +++ b/packages/serve/src/index.ts @@ -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); diff --git a/packages/webpack-cli/__tests__/resolveConfig/resolveConfig.test.js b/packages/webpack-cli/__tests__/resolveConfig/resolveConfig.test.js index 674e357444c..4522bfc3f2c 100644 --- a/packages/webpack-cli/__tests__/resolveConfig/resolveConfig.test.js +++ b/packages/webpack-cli/__tests__/resolveConfig/resolveConfig.test.js @@ -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' }; diff --git a/packages/webpack-cli/lib/webpack-cli.js b/packages/webpack-cli/lib/webpack-cli.js index e18f4a5bdb7..640c9808e33 100644 --- a/packages/webpack-cli/lib/webpack-cli.js +++ b/packages/webpack-cli/lib/webpack-cli.js @@ -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]; @@ -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; @@ -824,7 +824,7 @@ class WebpackCLI { const loadedConfig = await loadConfig(configPath); - return evaluateConfig(loadedConfig, options); + return evaluateConfig(loadedConfig, options.argv || {}); }), ); @@ -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; @@ -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); } @@ -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); diff --git a/test/config/type/function-with-argv/function-with-argv.test.js b/test/config/type/function-with-argv/function-with-argv.test.js index c4ee065e975..c1d212164d5 100644 --- a/test/config/type/function-with-argv/function-with-argv.test.js +++ b/test/config/type/function-with-argv/function-with-argv.test.js @@ -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'))); }); }); diff --git a/test/serve/basic/function-with-argv.config.js b/test/serve/basic/function-with-argv.config.js new file mode 100644 index 00000000000..d5e597ff3ec --- /dev/null +++ b/test/serve/basic/function-with-argv.config.js @@ -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')], + }; +}; diff --git a/test/serve/basic/function-with-env.config.js b/test/serve/basic/function-with-env.config.js new file mode 100644 index 00000000000..7aae1e7f0fe --- /dev/null +++ b/test/serve/basic/function-with-env.config.js @@ -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')], + }; +}; diff --git a/test/serve/basic/serve-basic.test.js b/test/serve/basic/serve-basic.test.js index 0c5b1c55236..ff5a20998c2 100644 --- a/test/serve/basic/serve-basic.test.js +++ b/test/serve/basic/serve-basic.test.js @@ -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]); diff --git a/yarn.lock b/yarn.lock index 1eb88b51162..41acf788531 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"