Repository navigation
fix(bundle): apply node-style CJS interop on all platforms - #34939
Merged
Merged
Conversation
PR #34533 fixed broken CJS/ESM default-interop for dual CJS/ESM packages (e.g. tslib) by rewriting esbuild's __toESM helper into node mode, but gated that rewrite behind should_replace_require_shim, which only returns true for --platform=deno. As a result --platform=browser still produced bundles that threw at module-init time with "Cannot destructure property '__extends' of 'Opt.default' as it is undefined". The createRequire shim is genuinely Deno-specific (it injects an import from node:module), but the node-style CJS interop fix-up is not: it reflects how the source module resolves its default import of a CJS entry (Node interop semantics, which is what these packages are authored against), independent of the runtime target. Decouple the two so force_node_cjs_interop runs for every platform while replace_require_shim stays Deno-only. Fixes #34837.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
deno bundle --platform=browsermiscompiled imports of dual CJS/ESMpackages whose ESM entry default-imports a CJS entry that sets
module.exports.__esModule = true.tslib'smodules/index.jsis thecanonical example. The generated bundle threw at module-init time with
TypeError: Cannot destructure property '__extends' of 'Opt.default' as it is undefined, even though the same code runs fine underdeno runand under
--platform=deno.This is the same root cause as #34524, which was fixed in #34533 by
rewriting esbuild's
__toESMinterop helper into "node mode" (so thedefault import resolves to the whole
module.exportsrather than to asynthesized
.defaultthat the__esModulemarker suppresses). Thatfix, however, was gated behind
should_replace_require_shim, which onlyreturns true for
--platform=deno, so the browser platform neverreceived it.
The
createRequireshim genuinely is Deno-specific, since it injects animport from
node:modulethat a browser bundle must not contain. Thenode-style CJS interop fix-up is not: it reflects how the source module
resolves its default import of a CJS entry (Node interop semantics, which
is what these packages are authored against), and that is a property of
the importing module, not of the runtime target. This decouples the two
so
force_node_cjs_interopruns for every platform whilereplace_require_shimstays Deno-only.The existing
cjs_esm_default_interopspec test gainsbundle_browserand
bundle_browser_minifiedvariants covering the--platform=browserpath.
Fixes #34837.