From c5a025c15d50fa57ed73c3b2ef153d3fc05dabac Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Sat, 29 Apr 2023 07:03:07 +0530 Subject: [PATCH] feat: allow CLI to be ESM --- bin/webpack-dev-server.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/bin/webpack-dev-server.js b/bin/webpack-dev-server.js index 5cf7a3bd5a..fc4dd8f3aa 100755 --- a/bin/webpack-dev-server.js +++ b/bin/webpack-dev-server.js @@ -85,8 +85,18 @@ const runCli = (cli) => { const pkgPath = require.resolve(`${cli.package}/package.json`); // eslint-disable-next-line import/no-dynamic-require const pkg = require(pkgPath); - // eslint-disable-next-line import/no-dynamic-require - require(path.resolve(path.dirname(pkgPath), pkg.bin[cli.binName])); + + if (pkg.type === "module" || /\.mjs/i.test(pkg.bin[cli.binName])) { + import(path.resolve(path.dirname(pkgPath), pkg.bin[cli.binName])).catch( + (error) => { + console.error(error); + process.exitCode = 1; + } + ); + } else { + // eslint-disable-next-line import/no-dynamic-require + require(path.resolve(path.dirname(pkgPath), pkg.bin[cli.binName])); + } }; /**