Skip to content

Conversation

@schiller-manuel
Copy link
Contributor

@schiller-manuel schiller-manuel commented Dec 23, 2025

Summary by CodeRabbit

  • Performance

    • Optimized build performance through intelligent module resolution caching for faster production builds.
    • Improved development experience with environment-aware optimization that preserves hot module reloading.
    • Streamlined module resolution for common import scenarios reduces unnecessary processing overhead.
  • Tests

    • Updated test expectations to align with enhanced module resolution behavior.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 23, 2025

Walkthrough

Introduces per-module resolution caching and a fast-path AST traversal optimization to the ServerFnCompiler. Adds mode-aware behavior (dev/build) to control caching behavior independently. Updates the plugin to pass mode configuration and adjusts test expectations to reflect the expanded resolveId signature.

Changes

Cohort / File(s) Summary
Compiler Implementation
packages/start-plugin-core/src/create-server-fn-plugin/compiler.ts
Introduces per-module resolution caching (resolveIdCache, exportResolutionCache), mode-aware getter, new helper methods (resolveIdCached, getExportResolutionCache, extractModuleInfo), and fast-path optimization helper (areAllKindsTopLevelOnly). Updates module/library resolution calls to use cached resolution instead of direct options.resolveId. Adds mode configuration to constructor options.
Plugin Integration
packages/start-plugin-core/src/create-server-fn-plugin/plugin.ts
Computes mode ('dev' or 'build') based on environment and passes it to ServerFnCompiler constructor as new option. Defaults to 'dev' for unknown environments.
Test Updates
packages/start-plugin-core/tests/createMiddleware/createMiddleware.test.ts, packages/start-plugin-core/tests/createServerFn/createServerFn.test.ts
Updates resolveId call expectations to include undefined as second argument in initial library resolution assertions. Affects fast-path and factory-pattern test cases.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • TanStack/router#5040: Introduces the ServerFnCompiler structure that this PR builds upon with caching and mode-aware logic enhancements.
  • TanStack/router#5245: Adds lookupKinds and LookupSetup to the same compiler file alongside this PR's caching mechanism.
  • TanStack/router#6164: Modifies module export and re-export resolution logic in the same compiler, overlapping with this PR's cached resolution enhancements.

Pre-merge checks and finishing touches

❌ Failed checks (1 warning, 1 inconclusive)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
Title check ❓ Inconclusive The title 'performance: compiler' is vague and doesn't clearly describe the specific optimization changes (caching, fast-path traversal, mode-aware workflow). Consider a more specific title like 'perf: add caching and fast-path optimization to ServerFnCompiler' to better communicate the nature of the performance improvements.
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch compiler-performance

📜 Recent review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f7b94b7 and 94fb8d4.

📒 Files selected for processing (4)
  • packages/start-plugin-core/src/create-server-fn-plugin/compiler.ts
  • packages/start-plugin-core/src/create-server-fn-plugin/plugin.ts
  • packages/start-plugin-core/tests/createMiddleware/createMiddleware.test.ts
  • packages/start-plugin-core/tests/createServerFn/createServerFn.test.ts
🧰 Additional context used
📓 Path-based instructions (2)
**/*.{ts,tsx}

📄 CodeRabbit inference engine (AGENTS.md)

Use TypeScript strict mode with extensive type safety for all code

Files:

  • packages/start-plugin-core/tests/createMiddleware/createMiddleware.test.ts
  • packages/start-plugin-core/tests/createServerFn/createServerFn.test.ts
  • packages/start-plugin-core/src/create-server-fn-plugin/plugin.ts
  • packages/start-plugin-core/src/create-server-fn-plugin/compiler.ts
**/*.{js,ts,tsx}

📄 CodeRabbit inference engine (AGENTS.md)

Implement ESLint rules for router best practices using the ESLint plugin router

Files:

  • packages/start-plugin-core/tests/createMiddleware/createMiddleware.test.ts
  • packages/start-plugin-core/tests/createServerFn/createServerFn.test.ts
  • packages/start-plugin-core/src/create-server-fn-plugin/plugin.ts
  • packages/start-plugin-core/src/create-server-fn-plugin/compiler.ts
🧠 Learnings (1)
📚 Learning: 2025-11-02T16:16:24.898Z
Learnt from: nlynzaad
Repo: TanStack/router PR: 5732
File: packages/start-client-core/src/client/hydrateStart.ts:6-9
Timestamp: 2025-11-02T16:16:24.898Z
Learning: In packages/start-client-core/src/client/hydrateStart.ts, the `import/no-duplicates` ESLint disable is necessary for imports from `#tanstack-router-entry` and `#tanstack-start-entry` because both aliases resolve to the same placeholder file (`fake-start-entry.js`) in package.json during static analysis, even though they resolve to different files at runtime.

Applied to files:

  • packages/start-plugin-core/tests/createMiddleware/createMiddleware.test.ts
  • packages/start-plugin-core/tests/createServerFn/createServerFn.test.ts
🧬 Code graph analysis (1)
packages/start-plugin-core/src/create-server-fn-plugin/plugin.ts (1)
packages/start-plugin-core/src/create-server-fn-plugin/compiler.ts (1)
  • mode (274-276)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Test
🔇 Additional comments (11)
packages/start-plugin-core/src/create-server-fn-plugin/compiler.ts (8)

179-187: LGTM: Clean optimization guard.

The logic correctly identifies when fast-path traversal can be used (ServerFn-only files).


247-276: LGTM: Well-designed caching infrastructure.

The mode-aware caching strategy is sound:

  • Dev mode bypasses caches (preserves HMR behavior)
  • Build mode enables caches (optimizes build performance)
  • Safe default to 'dev' prevents unintended caching

278-300: LGTM: Efficient caching implementation.

The cache key construction properly handles both absolute and relative imports by including the importer context. Caching null values for failed resolutions is a good optimization.


362-477: LGTM: Clean extraction of module parsing logic.

Extracting extractModuleInfo improves code organization without changing behavior. The method is now reusable and testable independently.


479-483: LGTM: Accurate documentation of cache invalidation strategy.

The comment correctly explains that resolution caches don't need invalidation since they're only populated in build mode (no HMR).


794-863: LGTM: Correct caching strategy for export resolution.

The caching logic is well-designed:

  • Only caches at the top level (visitedModules.size === 0) to avoid inconsistent intermediate results
  • Caches both positive and negative lookups
  • Properly uses resolveIdCached for re-export chains

325-325: LGTM: Consistent use of cached resolution.

All resolveIdCached calls are appropriately placed and correctly use the new signature with optional importer parameter.

Also applies to: 832-835, 889-889, 1033-1036


513-636: Fast-path optimization is correctly implemented.

The code safely skips direct call detection in the fast path. ServerFn uses only method chain patterns (.handler()) and never requires direct call detection. When fileKinds contains only ServerFn, needsDirectCallDetection() returns false because ServerFn has type 'methodChain' with no allowRootAsCandidate property. Therefore, checkDirectCalls is false when the fast path is active, and the optimization correctly avoids unnecessary traversal.

packages/start-plugin-core/tests/createServerFn/createServerFn.test.ts (1)

253-256: LGTM: Test expectations updated for new resolveId signature.

The test assertions correctly reflect that resolveId is now called with two arguments (id, undefined) for initial library resolution, matching the resolveIdCached implementation.

Also applies to: 308-312

packages/start-plugin-core/tests/createMiddleware/createMiddleware.test.ts (1)

100-103: LGTM: Consistent test updates across test suites.

The middleware tests are updated identically to the server function tests, ensuring consistent expectations for the new resolveId signature.

Also applies to: 155-159

packages/start-plugin-core/src/create-server-fn-plugin/plugin.ts (1)

121-132: LGTM: Conservative mode determination strategy.

The mode is correctly derived from the environment and passed to the compiler. Defaulting to 'dev' (no caching) for non-build modes is the safe choice for preserving HMR behavior.


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

@nx-cloud
Copy link

nx-cloud bot commented Dec 23, 2025

View your CI Pipeline Execution ↗ for commit 94fb8d4

Command Status Duration Result
nx affected --targets=test:eslint,test:unit,tes... ✅ Succeeded 8m 21s View ↗
nx run-many --target=build --exclude=examples/*... ✅ Succeeded 21s View ↗

☁️ Nx Cloud last updated this comment at 2025-12-23 01:09:13 UTC

@pkg-pr-new
Copy link

pkg-pr-new bot commented Dec 23, 2025

More templates

@tanstack/arktype-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/arktype-adapter@6190

@tanstack/directive-functions-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/directive-functions-plugin@6190

@tanstack/eslint-plugin-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/eslint-plugin-router@6190

@tanstack/history

npm i https://pkg.pr.new/TanStack/router/@tanstack/history@6190

@tanstack/nitro-v2-vite-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/nitro-v2-vite-plugin@6190

@tanstack/react-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router@6190

@tanstack/react-router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-devtools@6190

@tanstack/react-router-ssr-query

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-ssr-query@6190

@tanstack/react-start

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start@6190

@tanstack/react-start-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-client@6190

@tanstack/react-start-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-server@6190

@tanstack/router-cli

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-cli@6190

@tanstack/router-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-core@6190

@tanstack/router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools@6190

@tanstack/router-devtools-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools-core@6190

@tanstack/router-generator

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-generator@6190

@tanstack/router-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-plugin@6190

@tanstack/router-ssr-query-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-ssr-query-core@6190

@tanstack/router-utils

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-utils@6190

@tanstack/router-vite-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-vite-plugin@6190

@tanstack/server-functions-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/server-functions-plugin@6190

@tanstack/solid-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router@6190

@tanstack/solid-router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router-devtools@6190

@tanstack/solid-router-ssr-query

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router-ssr-query@6190

@tanstack/solid-start

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start@6190

@tanstack/solid-start-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-client@6190

@tanstack/solid-start-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-server@6190

@tanstack/start-client-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-client-core@6190

@tanstack/start-fn-stubs

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-fn-stubs@6190

@tanstack/start-plugin-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-plugin-core@6190

@tanstack/start-server-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-core@6190

@tanstack/start-static-server-functions

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-static-server-functions@6190

@tanstack/start-storage-context

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-storage-context@6190

@tanstack/valibot-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/valibot-adapter@6190

@tanstack/virtual-file-routes

npm i https://pkg.pr.new/TanStack/router/@tanstack/virtual-file-routes@6190

@tanstack/vue-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-router@6190

@tanstack/vue-router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-router-devtools@6190

@tanstack/vue-router-ssr-query

npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-router-ssr-query@6190

@tanstack/vue-start

npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-start@6190

@tanstack/vue-start-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-start-client@6190

@tanstack/vue-start-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-start-server@6190

@tanstack/zod-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/zod-adapter@6190

commit: 94fb8d4

@schiller-manuel schiller-manuel merged commit 7f14405 into main Dec 23, 2025
6 checks passed
@schiller-manuel schiller-manuel deleted the compiler-performance branch December 23, 2025 01:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants