Skip to content

Commit 6ce8ad2

Browse files
feat: allow CLI to be ESM
1 parent 5b20c9a commit 6ce8ad2

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

.eslintrc.js

+7
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ module.exports = {
7777
}
7878
},
7979
overrides: [
80+
{
81+
// Allow to use `dynamic` import
82+
files: ["bin/**/*.js"],
83+
parserOptions: {
84+
ecmaVersion: 2020
85+
}
86+
},
8087
{
8188
files: ["lib/**/*.runtime.js", "hot/*.js"],
8289
env: {

bin/webpack.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,19 @@ const runCli = cli => {
7878
const pkgPath = require.resolve(`${cli.package}/package.json`);
7979
// eslint-disable-next-line node/no-missing-require
8080
const pkg = require(pkgPath);
81-
// eslint-disable-next-line node/no-missing-require
82-
require(path.resolve(path.dirname(pkgPath), pkg.bin[cli.binName]));
81+
82+
if (pkg.type === "module" || /\.mjs/i.test(pkg.bin[cli.binName])) {
83+
// eslint-disable-next-line node/no-unsupported-features/es-syntax
84+
import(path.resolve(path.dirname(pkgPath), pkg.bin[cli.binName])).catch(
85+
error => {
86+
console.error(error);
87+
process.exitCode = 1;
88+
}
89+
);
90+
} else {
91+
// eslint-disable-next-line node/no-missing-require
92+
require(path.resolve(path.dirname(pkgPath), pkg.bin[cli.binName]));
93+
}
8394
};
8495

8596
/**

0 commit comments

Comments
 (0)