how to deal with commonjs & esm #1241
jerrykingxyz
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Summary
In this discussion, we will discuss how to handle commonjs and esm in rspack with reference to processing in webpack and babel.
Issues
#1211
Since we injected helpers, the input cjs code is converted to esm and cjs hybrid state. the interop inject only depends on
import
andexport
keywords, so the result code format is converted to esm.From this issue, we need to care about these problems
How Webpack does it
Inject helper
Inline helper is no problem, but not what we need. We only discuss the injected helper case.
typescript can use
importHelpers: true
to enable inject.typescript will check the
module
config. If the output format is esm, it will be injected using import, otherwise require.babel preset env will inline inject helpers. We can use
@babel/plugin-transform-runtime
to make helpers external, babel will inject with import or require by moduleType.Swc alway using import when inject helpers.
Input mix code
Webpack will inject
__esModule
when source code hasimport
andexport
keywords.Webpack will inject
hmd check
when source code is esm and usemodule.exports
.Conclusion
plan 1: Check code format when inject. esm use import, cjs use require.
plan 2: Transform code to cjs when parse.
plan 3: Transform esm to cjs with __esModule when source code is esm
module.exports
Beta Was this translation helpful? Give feedback.
All reactions