Skip to content

perf(nuxt,vite,webpack): use string parsing for module ids#34451

Merged
danielroe merged 7 commits intomainfrom
perf/mode-id-parsing
Mar 6, 2026
Merged

perf(nuxt,vite,webpack): use string parsing for module ids#34451
danielroe merged 7 commits intomainfrom
perf/mode-id-parsing

Conversation

@danielroe
Copy link
Copy Markdown
Member

🔗 Linked issue

📚 Description

this is a performance-focused PR to improve id resolution speed - in some situations this is 14 THOUSAND times faster.

did some quick benchmarks with mitata - obviously this is a micro-benchmark but within a plugin, this is called often and seems worthwhile ...

Input Before Now Speedup
POSIX, no query 1.22 µs 1.41 ns 862x
POSIX, with query 1.57 µs 384 ps 4,088x
Windows fwd-slash, no query 1.69 µs 8 ns 211x
Windows fwd-slash, with query 2.22 µs 383 ps 5,806x
Windows backslash, no query 2.10 µs 147 ps 14,340x
Windows backslash, with query 2.25 µs 390 ps 5,786x

previously we were doing a new URL() construction via pathToFileURL, decodeURIComponent, then 3 regex matches inside parseURL/parsePath from ufo.

The new parseModuleId is just a single indexOf('?') + at most two slice calls — both O(1) in V8 as they create string slices without copying ...

@bolt-new-by-stackblitz
Copy link
Copy Markdown

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Mar 5, 2026

Open in StackBlitz

@nuxt/kit

npm i https://pkg.pr.new/@nuxt/kit@34451

@nuxt/nitro-server

npm i https://pkg.pr.new/@nuxt/nitro-server@34451

nuxt

npm i https://pkg.pr.new/nuxt@34451

@nuxt/rspack-builder

npm i https://pkg.pr.new/@nuxt/rspack-builder@34451

@nuxt/schema

npm i https://pkg.pr.new/@nuxt/schema@34451

@nuxt/vite-builder

npm i https://pkg.pr.new/@nuxt/vite-builder@34451

@nuxt/webpack-builder

npm i https://pkg.pr.new/@nuxt/webpack-builder@34451

commit: f936572

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Mar 5, 2026

Merging this PR will not alter performance

✅ 20 untouched benchmarks
⏩ 3 skipped benchmarks1


Comparing perf/mode-id-parsing (36531e2) with main (bcc0d86)2

Open in CodSpeed

Footnotes

  1. 3 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

  2. No successful run was found on main (4f935ad) during the generation of this report, so bcc0d86 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@danielroe danielroe marked this pull request as ready for review March 5, 2026 21:32
@danielroe danielroe added this pull request to the merge queue Mar 5, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 5, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 7b084167-8ade-44e0-8e27-7cdbdd513254

📥 Commits

Reviewing files that changed from the base of the PR and between d777a24 and 36531e2.

📒 Files selected for processing (2)
  • packages/nuxt/src/pages/plugins/page-meta.ts
  • packages/nuxt/test/components-tree-shake.test.ts

Walkthrough

A new parseModuleId util was exported and many plugins migrated from URL-based parsing (pathToFileURL, parseURL, parseQuery) to module-id parsing via parseModuleId (and normalize where needed). Transform filters now use id-based include/exclude patterns. Several regex constants were introduced (e.g. STYLE_QUERY_RE, MACRO_QUERY_RE, VUE_ID_RE, NUXT_COMPONENT_RE) and query checks were converted to regex-based tests.

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 7.14% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarises the primary change: replacing URL-based module ID resolution with string parsing for performance improvements across nuxt, vite, and webpack packages.
Description check ✅ Passed The description clearly relates to the changeset, detailing the performance motivation, implementation approach with parseModuleId, and benchmarks demonstrating significant speedup improvements across multiple scenarios.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch perf/mode-id-parsing

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 and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
packages/vite/src/plugins/ssr-styles.ts (1)

16-18: Consider centralising duplicated regex constants.

STYLE_QUERY_RE is defined identically here and in runtime-paths.ts. Additionally, packages/nuxt/src/core/utils/plugins.ts already exports similar constants (MACRO_RE, NUXT_COMPONENT_RE) that could be reused instead of redefining MACRO_QUERY_RE and NUXT_COMPONENT_QUERY_RE.

Consider exporting STYLE_QUERY_RE from the core utils alongside the existing patterns and importing them where needed to reduce duplication.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/vite/src/plugins/ssr-styles.ts` around lines 16 - 18, Duplicate
regex constants are being defined; export STYLE_QUERY_RE from the central core
utils module that already exports MACRO_RE and NUXT_COMPONENT_RE, then remove
the local definitions and import the shared constants instead of redefining
MACRO_QUERY_RE, NUXT_COMPONENT_QUERY_RE and STYLE_QUERY_RE; also delete the
duplicate STYLE_QUERY_RE in the other module (runtime-paths.ts) and update its
usages to import the shared STYLE_QUERY_RE so all files use the single exported
regex constant.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/nuxt/src/pages/plugins/page-meta.ts`:
- Around line 380-387: The macro detection is order-sensitive because it checks
id.includes('?macro=true') instead of reading the parsed query; update the logic
in the block around parseModuleId and params so you set query.macro from
params.get('macro') (e.g., query.macro = params.get('macro') ?? undefined or
query.macro = params.get('macro') === 'true' ? 'true' : undefined) rather than
using id.includes('?macro=true'), ensuring
parseModuleId(id.replace(/\?macro=true$/,'')) still runs and query.macro is
populated whenever macro is present anywhere in the query string.

---

Nitpick comments:
In `@packages/vite/src/plugins/ssr-styles.ts`:
- Around line 16-18: Duplicate regex constants are being defined; export
STYLE_QUERY_RE from the central core utils module that already exports MACRO_RE
and NUXT_COMPONENT_RE, then remove the local definitions and import the shared
constants instead of redefining MACRO_QUERY_RE, NUXT_COMPONENT_QUERY_RE and
STYLE_QUERY_RE; also delete the duplicate STYLE_QUERY_RE in the other module
(runtime-paths.ts) and update its usages to import the shared STYLE_QUERY_RE so
all files use the single exported regex constant.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 27179682-4049-4069-96c1-fe30345f8cff

📥 Commits

Reviewing files that changed from the base of the PR and between bcc0d86 and 4a9a046.

📒 Files selected for processing (12)
  • packages/nuxt/src/components/plugins/islands-transform.ts
  • packages/nuxt/src/components/plugins/transform.ts
  • packages/nuxt/src/components/plugins/tree-shake.ts
  • packages/nuxt/src/core/plugins/extract-async-data-handlers.ts
  • packages/nuxt/src/core/plugins/keyed-functions.ts
  • packages/nuxt/src/core/utils/index.ts
  • packages/nuxt/src/core/utils/plugins.ts
  • packages/nuxt/src/pages/plugins/page-meta.ts
  • packages/vite/src/plugins/runtime-paths.ts
  • packages/vite/src/plugins/ssr-styles.ts
  • packages/vite/src/utils/index.ts
  • packages/webpack/src/plugins/ssr-styles.ts

@danielroe danielroe removed this pull request from the merge queue due to a manual request Mar 5, 2026
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/nuxt/src/pages/plugins/page-meta.ts`:
- Around line 383-388: The regexes TYPE_PARAM_RE, LANG_PARAM_RE and
MACRO_QUERY_RE are being executed against the full id which can include path
segments that look like query tokens; restrict these checks to the actual query
string instead (e.g., extract the search/query portion from id before testing)
so that query.type, query.lang and query.macro are derived only from the URL
query, not the pathname; update the code that builds query (the variable query
and uses of TYPE_PARAM_RE, LANG_PARAM_RE, MACRO_QUERY_RE on id) to run those
regexes against the isolated search/query substring (or parsed search via URL
parsing) before assigning to query.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 25522a06-cf93-4c54-bfcc-5f6d40ee99d5

📥 Commits

Reviewing files that changed from the base of the PR and between 4a9a046 and 9cc35fb.

📒 Files selected for processing (1)
  • packages/nuxt/src/pages/plugins/page-meta.ts

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
packages/nuxt/src/core/plugins/keyed-functions.ts (1)

30-35: Consider extracting shared constants to reduce duplication.

The constants SUPPORTED_EXT_RE, STYLE_QUERY_RE, and MACRO_QUERY_RE are defined identically in packages/nuxt/src/core/plugins/extract-async-data-handlers.ts (lines 10-13). Extracting these to a shared location (e.g., a constants file in core/utils/) would reduce duplication and ensure consistency if patterns need updating in future.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/nuxt/src/core/plugins/keyed-functions.ts` around lines 30 - 35,
Extract the duplicated regex constants SUPPORTED_EXT_RE, STYLE_QUERY_RE, and
MACRO_QUERY_RE into a shared module (e.g., export them from a new
core/utils/constants or core/utils/regexes file), replace the local declarations
in keyed-functions.ts (remove the SUPPORTED_EXT_RE, STYLE_QUERY_RE,
MACRO_QUERY_RE definitions) with imports from that shared module, and update the
other file (extract-async-data-handlers.ts) to import the same symbols; ensure
the exported names match exactly (SUPPORTED_EXT_RE, STYLE_QUERY_RE,
MACRO_QUERY_RE) and run a quick search to update any other usages to import from
the new shared file.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/nuxt/src/pages/plugins/page-meta.ts`:
- Around line 54-55: The include regex /[?&]macro=true/ is too loose and matches
values like macro=truex; update the include used in page-meta plugin to use the
same precise pattern as MACRO_QUERY_RE (i.e., require a parameter boundary after
"true" such as & or end/hash/encoded equivalents) so only exact macro=true
matches pass; replace the current include regex with the MACRO_QUERY_RE or an
equivalent boundary-aware regex and keep the existing exclude entry unchanged.

---

Nitpick comments:
In `@packages/nuxt/src/core/plugins/keyed-functions.ts`:
- Around line 30-35: Extract the duplicated regex constants SUPPORTED_EXT_RE,
STYLE_QUERY_RE, and MACRO_QUERY_RE into a shared module (e.g., export them from
a new core/utils/constants or core/utils/regexes file), replace the local
declarations in keyed-functions.ts (remove the SUPPORTED_EXT_RE, STYLE_QUERY_RE,
MACRO_QUERY_RE definitions) with imports from that shared module, and update the
other file (extract-async-data-handlers.ts) to import the same symbols; ensure
the exported names match exactly (SUPPORTED_EXT_RE, STYLE_QUERY_RE,
MACRO_QUERY_RE) and run a quick search to update any other usages to import from
the new shared file.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 02c7c174-8e0b-404b-a6f9-477f6c4ec736

📥 Commits

Reviewing files that changed from the base of the PR and between 9cc35fb and d777a24.

📒 Files selected for processing (5)
  • packages/nuxt/src/components/plugins/tree-shake.ts
  • packages/nuxt/src/core/plugins/extract-async-data-handlers.ts
  • packages/nuxt/src/core/plugins/keyed-functions.ts
  • packages/nuxt/src/core/utils/plugins.ts
  • packages/nuxt/src/pages/plugins/page-meta.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/nuxt/src/core/plugins/extract-async-data-handlers.ts

@danielroe danielroe enabled auto-merge March 5, 2026 22:27
@danielroe danielroe added this pull request to the merge queue Mar 5, 2026
Merged via the queue into main with commit 9ed341e Mar 6, 2026
96 of 99 checks passed
@danielroe danielroe deleted the perf/mode-id-parsing branch March 6, 2026 00:00
This was referenced Mar 5, 2026
This was referenced Mar 9, 2026
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.

1 participant