Repository navigation
fix(externals): resolve non-hoisted traced require() ids from rolldown's __require helper - #4992
Conversation
…ire helper Rolldown routes `require()` in bundled CommonJS through one shared `__require` helper, so a non-hoisted traced id must be relative to the chunk that defines it, not to the calling chunk. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@Gourav224 is attempting to deploy a commit to the Nitro Team on Vercel. A member of the Team first needs to authorize it. |
|
Caution Review failedThe pull request is closed. Walkthrough
Pre-merge checks:
|
Rolldown renames its helper (`__require$1`) when another module in the chunk declares its own `__require` (e.g. tsup/esbuild output). Also resolve the helper's chunk once per chunk.
require() ids from rolldown's __require helper
commit: |
Scanning the chunk for an import of `__require` could match a comment or a string when the helper is defined in the chunk itself (inlined builds, or a lib chunk that holds the rolldown runtime). Use the chunk whose modules include the rolldown runtime instead.
🔗 Linked issue
Resolves #4991
❓ Type of change
📚 Description
Since #4740, when a traced dependency is not the hoisted version, Nitro writes it as a path relative to the chunk that uses it. That is right for
import, but not forrequire()in bundled CommonJS. Rolldown (and Vite) send those calls through one shared__requirehelper (createRequire(import.meta.url)), defined in the chunk that holds the rolldown runtime (usually_runtime.mjs). So the path is resolved from that chunk, not from the calling chunk. Any chunk in a subfolder (_libs/…) then fails withMODULE_NOT_FOUND. Example from the issue:tsyringerequirestslib@1while the app usestslib@2.Fix (
renderChunkinnitro:externals):import/import()keep the path relative to the chunk.__require("…")calls, the path is computed from the chunk that defines the helper. That chunk is found from bundle metadata (meta.chunks): the chunk whose modules include\0rolldown/runtime.js. It can be_runtime.mjs, a lib chunk (when only one chunk uses the runtime), or the calling chunk itself (inlineDynamicImports). If no chunk has it, the old chunk-relative path is used.__require$1when another module in the same chunk declares its own__require(tsup/esbuild ESM output does this). The renamed form is matched too.The Rollup builder is not affected:
@rollup/plugin-commonjsturns external requires into imports.Tests (in the existing "resolve module version conflicts" test):
nitro-cjs-nested-requirer: a bundled CommonJS package in_libs/that requires its own nested@fixture/[email protected].nitro-cjs-shadowed-requirer: the same setup, plus its own__requireso the helper gets renamed (nested@fixture/[email protected]).Both fail on rolldown without the fix (500,
MODULE_NOT_FOUND) and pass with it.test/unit/externals-require-helper.test.tsbuilds a small app where the runtime lives in_libs/pkg-cjs.mjs(and inindex.mjswithinlineDynamicImports), next to a doc comment that looks like an import of__require. It guards against finding the helper by scanning the chunk text.test/presets/node.test.tsandtest/presets/vercel.test.tspass on rolldown and rollup. Lint, format and typecheck are clean.📝 Checklist
🤖 Generated with AI assistant