fix: cache the result of inline require() of bundled ESM modules#4441
Open
MarshallOfSound wants to merge 1 commit intoevanw:mainfrom
Open
fix: cache the result of inline require() of bundled ESM modules#4441MarshallOfSound wants to merge 1 commit intoevanw:mainfrom
require() of bundled ESM modules#4441MarshallOfSound wants to merge 1 commit intoevanw:mainfrom
Conversation
When a bundled CJS-shaped file calls require() on a bundled ESM-shaped module, esbuild emits `(init_foo(), __toCommonJS(foo_exports))` at each call site. Since f4ff26d the __toCommonJS helper allocates a fresh wrapper on every call, so two require() calls for the same module return different objects — diverging from Node's CJS cache semantics, webpack's __webpack_require__ cache, and esbuild ≤ 0.14.26. This adds a separate __toCommonJSCached runtime helper (WeakMap- memoized, falling back to no-cache when WeakMap is unavailable) and routes only the inline-require code path to it. The entry-point path (`module.exports = __toCommonJS(exports)`, called once) continues to use the uncached helper, so bundles with no inline ESM require() pay no extra cost. Fixes evanw#4440.
require() of bundled ESM modulesrequire() of bundled ESM modules
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.
Note
Found this issue while attempting to switch Electron core from webpack -> esbuild. Claude identified the underlying issue and I manually verified. This PR and the issue linked below both used AI assistance (via claude code)
When a bundled CJS-shaped file calls
require()on a bundled ESM-shaped module, esbuild emits(init_foo(), __toCommonJS(foo_exports))at each call site. Since f4ff26d (0.14.27),__toCommonJSallocates a fresh wrapper on every call, so tworequire()calls for the same module return different objects — diverging from Node's CJS cache semantics, webpack's__webpack_require__cache, and esbuild ≤ 0.14.26.This adds a separate
__toCommonJSCachedruntime helper (WeakMap-memoized, with a no-cache fallback whenWeakMapis unavailable) and routes only the inline-require()code path to it. The entry-point path (module.exports = __toCommonJS(exports)/return __toCommonJS(exports), called once) continues to use the uncached helper, so bundles with no inline ESMrequire()pay no extra cost.Fixes #4440.