Skip to content

perf(externals): memoize tryResolve per importer directory in node-externals (v2) - #4800

Open
lx3133584 wants to merge 1 commit into
nitrojs:v2from
lx3133584:fix/node-externals-memoize-try-resolve
Open

lx3133584 wants to merge 1 commit into
nitrojs:v2from
lx3133584:fix/node-externals-memoize-try-resolve

Conversation

@lx3133584

Copy link
Copy Markdown

Description

Resolves #4799

In Nitro 2 (node-externals), tryResolve(id, importer) is invoked repeatedly during resolveId for every bare import across all server chunks.

Because tryResolve calls exsolve.resolveModuleURL with from: [pathToFileURL(importer), ...], each chunk file path was passed as a distinct URL, preventing caching across different chunk files that reside in the same directory. In large builds (such as Nuxt apps emitting hundreds of server chunks), resolving the same bare import from the same importer directory repeatedly triggered dozens of filesystem statSync calls per import, dominating build times (e.g. 8 of 10 minutes on Windows).

Changes

  1. Memoize tryResolve:
    • Added a resolveCache Map scoped to the externals plugin instance.
    • Keys are normalized by ${id}\0${fromDir} where fromDir = importer && isAbsolute(importer) ? dirname(importer) : "" (since Node.js module resolution ancestry depends on the parent directory, all chunks in the same directory resolve bare imports identically).
    • Both resolved module paths and negative resolution results (undefined) are memoized to avoid redundant filesystem traversal for unresolvable IDs.
  2. Unit Tests:
    • Added test/unit/externals-cache.test.ts to verify that repeated module resolutions across chunks in the same directory and repeated unresolvable module probes are memoized and invoke resolveModuleURL only once.

Verification

  • vitest run test/unit/externals-cache.test.ts passed (2 tests).
  • vitest run test/unit/ passed (all 18 suites, 101 tests).
  • pnpm build (unbuild) succeeded cleanly.
  • eslint and prettier -c passed with 0 errors.

🤖 Generated with Claude Code

…ternals (v2)

Co-Authored-By: Claude Code <noreply@anthropic.com>
@lx3133584
lx3133584 requested a review from pi0 as a code owner October 15, 2026 21:24
@vercel

vercel Bot commented Oct 15, 2026

Copy link
Copy Markdown

@lx3133584 is attempting to deploy a commit to the Nitro Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 905e128a-9de0-4679-9968-9345f66eb63a

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@pkg-pr-new

pkg-pr-new Bot commented Oct 15, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/nitropack@4800

commit: 6cf3b8c

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant