Open hardfist opened 2 years ago
Searching "WrapESM" in the source:
Basically two circumstances:
require()
to import an ESM module.splitting
, then any dynamic import will result in the dependency be wrapped in __esm
. See it in REPLI finally find the reason why tree shaking fails, because some one use dynamic import, which will make tree shaking fail, It seems fail both in rollup and webpack. you can see it in repl
There's a workaround to still apply tree-shaking to lazy modules -- add a wrapper to re-export names you need:
// components.js
export let a = 1
export let b = 2
// wrapper.js, manually export used names
export { a } from './components'
// main.js
const { a } = await import('./wrapper')
I met an case which esm treeshaking not working, but it's hard to make a minimal reproducible demo. It's odd that the esm module generates an __esm wrapper for esm exports, which I think maybe related to the problem.
components/index.js
generated code