diff --git a/package.json b/package.json index be4d41658cf..27753e6f735 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "pretest": "yarn build && yarn lint && yarn prepsuite", "test": "jest --reporters=default", "test:smoketests": "nyc node smoketests", - "test:coverage": "nyc --no-hook-require jest --forceExit", + "test:coverage": "nyc jest --forceExit", "test:cli": "jest test --reporters=default --forceExit", "test:packages": "jest packages/ --reporters=default --forceExit", "test:ci": "yarn test:cli && yarn test:packages", diff --git a/packages/webpack-cli/lib/webpack-cli.js b/packages/webpack-cli/lib/webpack-cli.js index c171608e498..79f7070534c 100644 --- a/packages/webpack-cli/lib/webpack-cli.js +++ b/packages/webpack-cli/lib/webpack-cli.js @@ -1160,7 +1160,11 @@ class WebpackCLI { Module.prototype._compile = previousModuleCompile; } - if (error.code === 'ERR_REQUIRE_ESM' && pathToFileURL && dynamicImportLoader) { + if ( + (error.code === 'ERR_REQUIRE_ESM' || process.env.WEBPACK_CLI_FORCE_LOAD_ESM_CONFIG) && + pathToFileURL && + dynamicImportLoader + ) { const urlForConfig = pathToFileURL(configPath); options = await dynamicImportLoader(urlForConfig); diff --git a/test/config-format/mjs/mjs.test.js b/test/config-format/mjs/mjs.test.js index af96ace1940..4fa4e1cd12b 100644 --- a/test/config-format/mjs/mjs.test.js +++ b/test/config-format/mjs/mjs.test.js @@ -2,14 +2,14 @@ const { run } = require('../../utils/test-utils'); describe('webpack cli', () => { it('should support mjs config format', () => { - const { exitCode, stderr, stdout } = run(__dirname, ['-c', 'webpack.config.mjs']); + const { exitCode, stderr, stdout } = run(__dirname, ['-c', 'webpack.config.mjs'], { + env: { WEBPACK_CLI_FORCE_LOAD_ESM_CONFIG: true }, + }); if (/Unexpected token/.test(stderr)) { expect(exitCode).toBe(2); expect(stdout).toBeFalsy(); } else { - console.log(stderr); - expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); expect(stdout).toBeTruthy(); diff --git a/test/config-lookup/custom-name/custom-name.test.js b/test/config-lookup/custom-name/custom-name.test.js index 557bc914556..62c5fca458a 100644 --- a/test/config-lookup/custom-name/custom-name.test.js +++ b/test/config-lookup/custom-name/custom-name.test.js @@ -5,7 +5,7 @@ const { run } = require('../../utils/test-utils'); describe('custom config file', () => { it('should work with cjs format', () => { - const { exitCode, stderr, stdout } = run(__dirname, ['--config', resolve(__dirname, 'config.webpack.js')], false); + const { exitCode, stderr, stdout } = run(__dirname, ['--config', resolve(__dirname, 'config.webpack.js')]); expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); @@ -13,7 +13,9 @@ describe('custom config file', () => { }); it('should work with esm format', () => { - const { exitCode, stderr, stdout } = run(__dirname, ['--config', resolve(__dirname, 'config.webpack.mjs')], false); + const { exitCode, stderr, stdout } = run(__dirname, ['--config', resolve(__dirname, 'config.webpack.mjs')], { + env: { WEBPACK_CLI_FORCE_LOAD_ESM_CONFIG: true }, + }); if (/Unexpected token/.test(stderr)) { expect(exitCode).toBe(2); diff --git a/test/config/defaults/mjs-config/default-mjs-config.test.js b/test/config/defaults/mjs-config/default-mjs-config.test.js index 5ca205d320f..030ba6bd817 100644 --- a/test/config/defaults/mjs-config/default-mjs-config.test.js +++ b/test/config/defaults/mjs-config/default-mjs-config.test.js @@ -4,7 +4,7 @@ const { run, isWebpack5 } = require('../../../utils/test-utils'); describe('Default Config:', () => { it('Should be able to pick mjs config by default', () => { - const { exitCode, stderr, stdout } = run(__dirname, []); + const { exitCode, stderr, stdout } = run(__dirname, [], { env: { WEBPACK_CLI_FORCE_LOAD_ESM_CONFIG: true } }); if (/Unexpected token/.test(stderr)) { expect(exitCode).toEqual(2); diff --git a/test/config/error-mjs/config-error.test.js b/test/config/error-mjs/config-error.test.js index db6424d4e83..928760bdb03 100644 --- a/test/config/error-mjs/config-error.test.js +++ b/test/config/error-mjs/config-error.test.js @@ -4,7 +4,9 @@ const { run } = require('../../utils/test-utils'); describe('config error', () => { it('should throw error with invalid configuration', () => { - const { exitCode, stderr, stdout } = run(__dirname, ['-c', resolve(__dirname, 'webpack.config.mjs')]); + const { exitCode, stderr, stdout } = run(__dirname, ['-c', resolve(__dirname, 'webpack.config.mjs')], { + env: { WEBPACK_CLI_FORCE_LOAD_ESM_CONFIG: true }, + }); expect(exitCode).toBe(2); @@ -17,7 +19,9 @@ describe('config error', () => { }); it('should throw syntax error and exit with non-zero exit code', () => { - const { exitCode, stderr, stdout } = run(__dirname, ['-c', resolve(__dirname, 'syntax-error.mjs')]); + const { exitCode, stderr, stdout } = run(__dirname, ['-c', resolve(__dirname, 'syntax-error.mjs')], { + env: { WEBPACK_CLI_FORCE_LOAD_ESM_CONFIG: true }, + }); expect(exitCode).toBe(2); expect(stderr).toContain('SyntaxError: Unexpected token');