rspack
rspack copied to clipboard
[Bug Report]: output.library.type = 'module' not outputting ESM correctly
System Info
System: OS: Linux 5.10 Ubuntu 20.04 LTS (Focal Fossa) CPU: (12) x64 Intel(R) Core(TM) i7-10810U CPU @ 1.10GHz Memory: 1.63 GB / 15.51 GB Container: Yes Shell: 5.0.17 - /bin/bash Binaries: Node: 16.13.0 - ~/.nvm/versions/node/v16.13.0/bin/node Yarn: 1.22.15 - ~/.nvm/versions/node/v16.13.0/bin/yarn npm: 8.1.0 - ~/.nvm/versions/node/v16.13.0/bin/npm npmPackages: @rspack/cli: 0.1.1 => 0.1.1
Details
The output.library.target = 'module' does not output ESM modules, support for this is mentioned in the documentation: https://www.rspack.dev/config/output.html#outputlibrarytype
In Webpack this worked when an experiment was enabled: https://webpack.js.org/configuration/output/#module-definition-systems
# ./rspack.config.js
const path = require('path');
module.exports = {
entry: {
add: './src/add.js',
},
target: 'es2020',
output: {
filename: '[name].bundle.mjs',
path: path.resolve(__dirname, 'dist'),
library: {
type: 'module',
},
},
};
# ./src/add.js
export const add = (a, b) => a + b
This gives output of
# ./dist/add.bundle.mjs
!function(){var e={380:function(e,r,t){"use strict";Object.defineProperty(r,"__esModule",{value:!0}),Object.defineProperty(r,"add",{enumerable:!0,get:function(){return n}});let n=(e,r)=>e+r}},r={};!function t(n){var o=r[n];if(void 0!==o)return o.exports;var u=r[n]={exports:{}};return e[n](u,u.exports,t),u.exports}("380")}();
//# sourceMappingURL=add.bundle.mjs.map
I would expect it to output something along the lines of the original source using export
# expected
const add = (a, b) => a + b
export { add }
Reproduce link
https://github.com/thebreiflabb/rspack-es-module-error-repro
Reproduce Steps
pnpm install
pnpm build
pnpm test
The test fails because the outputted dist/add.bundle.mjs does not export { add }
sorry we are not supportting esm output yet
When is esm output supported? We need esm and system. What can be done to support this?
This will be easier to land after https://github.com/webpack/webpack/issues/17121 and by extension https://github.com/webpack/webpack/issues/2933 lands in webpack.
esm output is supported, please check it in @0.1.19 version
Awesome, it works with the new version when enabling experiments.outputModule = true, thank you!