swc_bundler::Bundler
with require: true
fails to transform CommonJS modules with reassigned module.exports
#10499
Labels
Describe the bug
I noticed an issue with the
swc_bundler::Bundler
when trying to bundle certain projects that contain CommonJS modules. I have created a minimal example project (see fork) to reproduce the issue:As you can see, we are reassigning
module.exports
in42.js
andfoo.js
. According to the Node.js CommonJS module docs, reassigningmodule.exports
is allowed. When runningexample_project/index.js
using Node.js and Deno, both runtimes agree on the output.However, when bundling the above project with
require: true
(see fork), the output differs:It seems that the
interop
function that is injected when usingrequire: true
does not handle this scenario correctly. I would be curious to hear if this is the expected behavior ofswc_bundler
(due to esm and cjs interop) or if this is something that should be fixed.Thanks for looking into this!
Input code
Config
Link to the code that reproduces this issue
https://play.swc.rs/?version=1.11.24&code=H4sIAAAAAAAAA0vOzyvOz0nVy8lP1yhKLSzNLErVUNLTNzHSyypW0tS05krGriAtPx%2BqAgBI%2Bi%2FYQgAAAA%3D%3D&config=H4sIAAAAAAAAA32US5LjIAyG932KlNe9nVnMAXo3Z6AICIc0Ri4k0nF15e4jY%2BcxHTk7W59%2BCSSh77fdrjuS6%2F7svuVTfkZbCMrtXyw0ZbZnsXTgBkuuxJG79ys90oyCTQTNdFlIx7b0wE1Fv1b3LiESXN1X2xBzDNNjQofDWIDowSZWiVcHyEz%2F61dW8GsGXOqjfY%2BYwOYXxFgyMTP0ULTADlOyI4E52aJEmU9qSyTUUsywMngzFhxVnn3kiFlyPlMP1huHHhQUCziOJ9BkkktkmeR6yn0a9rCvfd%2Ba%2FEMNJ5uqZSUnnFtL5LRK1ANGYhNq1kq4wI0aLHAt7k9lDKYA15KfdUeMeaMnnwBSgWSJsh1Ai9s8gszTljq8VMYcZGR5UrjMt3bLDL0U1cQYlMrOlYHCUetmAV8dzJV12nFWvFE%2Bih4MhCCzooSmr8juoCXlaQQMCpD%2B2qBN1QLM7RVu8PlBvMAfckvWB2z1GCwftilNwx7TiwQD8AH9CwdpBeM2LrIlzuM2r9mDjAZ41aVSA89LQB4Ao0ltWT7NhjwPiWj6hPv7mlgdLrclPNjc39%2F7soffVoduQF8bXDf83N9lL%2F%2Fu7k7XLXw7eBfp71XZkl7%2BAVPL6HotBgAA
SWC Info output
No response
Expected behavior
I expected that the bundled output program behaves similar to the input program. More specifically, I expected that
require('...')
evaluates to the value assigned tomodule.exports
.Actual behavior
After bundling,
require('...')
no longer evaluates to the value assigned tomodule.exports
. Instead, it returns anewObj
that preserves the keys ofmodule.exports
and anewObj.default
property that contains the desired value.Version
swc_bundler 19.0.0
Additional context
No response
The text was updated successfully, but these errors were encountered: