Skip to content

Commit aebdbbc

Browse files
feat: support ES module configuration format (#2381)
1 parent d004ded commit aebdbbc

File tree

25 files changed

+191
-85
lines changed

25 files changed

+191
-85
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"pretest": "yarn build && yarn lint && yarn prepsuite",
3838
"test": "jest --reporters=default",
3939
"test:smoketests": "nyc node smoketests",
40-
"test:coverage": "nyc jest --forceExit",
40+
"test:coverage": "nyc --require ts-node/register jest --forceExit",
4141
"test:cli": "jest test --reporters=default --forceExit",
4242
"test:packages": "jest packages/ --reporters=default --forceExit",
4343
"test:ci": "yarn test:cli && yarn test:packages",
@@ -64,6 +64,7 @@
6464
"@typescript-eslint/eslint-plugin": "^2.34.0",
6565
"@typescript-eslint/parser": "^2.34.0",
6666
"@webpack-cli/migrate": "^1.1.2",
67+
"coffeescript": "^2.5.1",
6768
"colorette": "^1.2.1",
6869
"commitlint": "^11.0.0",
6970
"commitlint-config-cz": "^0.13.2",
@@ -88,6 +89,7 @@
8889
"rimraf": "^3.0.2",
8990
"strip-ansi": "^6.0.0",
9091
"ts-jest": "^26.4.3",
92+
"ts-node": "^9.1.1",
9193
"typescript": "^4.1.3",
9294
"webpack": "^5.18.0",
9395
"webpack-bundle-analyzer": "^4.3.0",

packages/webpack-cli/bin/cli.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#!/usr/bin/env node
22

33
'use strict';
4+
5+
const Module = require('module');
6+
7+
const originalModuleCompile = Module.prototype._compile;
8+
49
require('v8-compile-cache');
510

611
const importLocal = require('import-local');
@@ -15,7 +20,7 @@ if (importLocal(__filename)) {
1520
process.title = 'webpack';
1621

1722
if (utils.packageExists('webpack')) {
18-
runCLI(process.argv);
23+
runCLI(process.argv, originalModuleCompile);
1924
} else {
2025
const { promptInstallation, logger, colors } = utils;
2126

@@ -25,7 +30,7 @@ if (utils.packageExists('webpack')) {
2530
.then(() => {
2631
logger.success(`${colors.bold('webpack')} was installed successfully.`);
2732

28-
runCLI(process.argv);
33+
runCLI(process.argv, originalModuleCompile);
2934
})
3035
.catch(() => {
3136
logger.error(`Action Interrupted, Please try once again or install ${colors.bold('webpack')} manually.`);

packages/webpack-cli/lib/bootstrap.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
const WebpackCLI = require('./webpack-cli');
22
const utils = require('./utils');
33

4-
const runCLI = async (args) => {
4+
const runCLI = async (args, originalModuleCompile) => {
55
try {
66
// Create a new instance of the CLI object
77
const cli = new WebpackCLI();
88

9+
cli._originalModuleCompile = originalModuleCompile;
10+
911
await cli.run(args);
1012
} catch (error) {
1113
utils.logger.error(error);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function dynamicImportLoader() {
2+
let importESM;
3+
4+
try {
5+
importESM = new Function('id', 'return import(id);');
6+
} catch (e) {
7+
importESM = null;
8+
}
9+
10+
return importESM;
11+
}
12+
13+
module.exports = dynamicImportLoader;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ module.exports = {
1919
return require('./capitalize-first-letter');
2020
},
2121

22+
get dynamicImportLoader() {
23+
return require('./dynamic-import-loader');
24+
},
25+
2226
get getPackageManager() {
2327
return require('./get-package-manager');
2428
},

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

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const fs = require('fs');
22
const path = require('path');
3+
const { pathToFileURL } = require('url');
4+
const Module = require('module');
35

46
const { program } = require('commander');
57
const utils = require('./utils');
@@ -1137,26 +1139,35 @@ class WebpackCLI {
11371139
}
11381140
}
11391141

1140-
const { pathToFileURL } = require('url');
1141-
1142-
let importESM;
1143-
1144-
try {
1145-
importESM = new Function('id', 'return import(id);');
1146-
} catch (e) {
1147-
importESM = null;
1148-
}
1149-
11501142
let options;
11511143

11521144
try {
11531145
try {
11541146
options = require(configPath);
11551147
} catch (error) {
1156-
if (pathToFileURL && importESM && error.code === 'ERR_REQUIRE_ESM') {
1148+
let previousModuleCompile;
1149+
1150+
// TODO Workaround https://github.com/zertosh/v8-compile-cache/issues/30
1151+
if (this._originalModuleCompile) {
1152+
previousModuleCompile = Module.prototype._compile;
1153+
1154+
Module.prototype._compile = this._originalModuleCompile;
1155+
}
1156+
1157+
const dynamicImportLoader = this.utils.dynamicImportLoader();
1158+
1159+
if (this._originalModuleCompile) {
1160+
Module.prototype._compile = previousModuleCompile;
1161+
}
1162+
1163+
if (
1164+
(error.code === 'ERR_REQUIRE_ESM' || process.env.WEBPACK_CLI_FORCE_LOAD_ESM_CONFIG) &&
1165+
pathToFileURL &&
1166+
dynamicImportLoader
1167+
) {
11571168
const urlForConfig = pathToFileURL(configPath);
11581169

1159-
options = await importESM(urlForConfig);
1170+
options = await dynamicImportLoader(urlForConfig);
11601171
options = options.default;
11611172

11621173
return { options, path: configPath };

test/config-format/coffee/coffee.test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// eslint-disable-next-line node/no-unpublished-require
21
const { run } = require('../../utils/test-utils');
32

43
describe('webpack cli', () => {

test/config-format/coffee/package.json

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const { run } = require('../../utils/test-utils');
2+
3+
describe('webpack cli', () => {
4+
it('should support CommonJS file', () => {
5+
const { exitCode, stderr, stdout } = run(__dirname, ['-c', 'webpack.config.cjs'], false);
6+
7+
expect(exitCode).toBe(0);
8+
expect(stderr).toBeFalsy();
9+
expect(stdout).toBeTruthy();
10+
});
11+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('Hoshiumi');

0 commit comments

Comments
 (0)