Skip to content

Commit

Permalink
Merge pull request #17088 from webpack/feat-allow-cli-to-be-esm
Browse files Browse the repository at this point in the history
feat: allow CLI to be ESM
  • Loading branch information
TheLarkInn committed Apr 28, 2023
2 parents b71c780 + 6ce8ad2 commit 102e25f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ module.exports = {
}
},
overrides: [
{
// Allow to use `dynamic` import
files: ["bin/**/*.js"],
parserOptions: {
ecmaVersion: 2020
}
},
{
files: ["lib/**/*.runtime.js", "hot/*.js"],
env: {
Expand Down
15 changes: 13 additions & 2 deletions bin/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,19 @@ const runCli = cli => {
const pkgPath = require.resolve(`${cli.package}/package.json`);
// eslint-disable-next-line node/no-missing-require
const pkg = require(pkgPath);
// eslint-disable-next-line node/no-missing-require
require(path.resolve(path.dirname(pkgPath), pkg.bin[cli.binName]));

if (pkg.type === "module" || /\.mjs/i.test(pkg.bin[cli.binName])) {
// eslint-disable-next-line node/no-unsupported-features/es-syntax
import(path.resolve(path.dirname(pkgPath), pkg.bin[cli.binName])).catch(
error => {
console.error(error);
process.exitCode = 1;
}
);
} else {
// eslint-disable-next-line node/no-missing-require
require(path.resolve(path.dirname(pkgPath), pkg.bin[cli.binName]));
}
};

/**
Expand Down

0 comments on commit 102e25f

Please sign in to comment.