How do I tell Webpack that it should treat my loader as an ES Module and not CommonJS?
My goal is to write a Loader that uses the ES Module syntax (export default...
)
Demo (not working): https://stackblitz.com/edit/webpack-webpack-js-org-7gmdc8?file=webpack.config.js
Apparently Webpack is trying to load the module as CommonJS. If you look at loadLoader, the condition loader.type === "module"
fails, since loader.type
is undefined
.
// webpack.conf.js:
const path = require('path');
module.exports = {
mode: 'development',
entry: './src/app.js',
output: {
filename: 'app.js',
path: path.resolve(__dirname, 'dist'),
clean: true,
},
module: {
rules: [
{
test: /\.js$/,
use: ['my-loader'],
},
],
},
resolveLoader: {
alias: {
// WEBPACK IS TRYING TO LOAD THIS AS COMMONJS... WHY???
'my-loader':
'/home/projects/webpack-webpack-js-org-7gmdc8/my-loader/main.mjs',
},
},
};
Sample stacktrace:
ERROR in ./index.js
Module build failed (from ../my-webpack-loader-esm/dist/index.bundle.mjs):
Error [ERR_REQUIRE_ESM]: require() of ES Module /workspaces/webpack-loader-lab/my-webpack-loader-esm/dist/index.bundle.mjs not supported.
Instead change the require of /workspaces/webpack-loader-lab/my-webpack-loader-esm/dist/index.bundle.mjs to a dynamic import() which is available in all CommonJS modules.
at loadLoader (/workspaces/webpack-loader-lab/the-client/node_modules/loader-runner/lib/loadLoader.js:23:17)
"type": "module"
and<file>.mjs
to no avail. There's also an issue for it: github.com/webpack/loader-runner/issues/61 – Didiextensions
andmodules
configuration inresolveLoader
– Corr