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
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
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
importandexportkeywords, 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: trueto enable inject.typescript will check the
moduleconfig. 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-runtimeto 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
__esModulewhen source code hasimportandexportkeywords.Webpack will inject
hmd checkwhen 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.exportsBeta Was this translation helpful? Give feedback.
All reactions