rspack icon indicating copy to clipboard operation
rspack copied to clipboard

[Bug Report]: output.library.type = 'module' not outputting ESM correctly

Open thebreiflabb opened this issue 2 years ago • 1 comments

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 }

thebreiflabb avatar Mar 16 '23 07:03 thebreiflabb

sorry we are not supportting esm output yet

hardfist avatar Mar 17 '23 10:03 hardfist

When is esm output supported? We need esm and system. What can be done to support this?

FlorianRappl avatar Apr 04 '23 16:04 FlorianRappl

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.

zackarychapple avatar May 05 '23 17:05 zackarychapple

esm output is supported, please check it in @0.1.19 version

hardfist avatar May 06 '23 02:05 hardfist

Awesome, it works with the new version when enabling experiments.outputModule = true, thank you!

thebreiflabb avatar May 06 '23 08:05 thebreiflabb