Skip to content

perf(bundler): add further transform hook filters - #783

Merged
harlan-zw merged 2 commits into
unjs:mainfrom
TheAlexLichter:perf/bundler-transform-hook-filters
Jun 25, 2026
Merged

harlan-zw merged 2 commits into
unjs:mainfrom
TheAlexLichter:perf/bundler-transform-hook-filters

Conversation

@TheAlexLichter

@TheAlexLichter TheAlexLichter commented Jun 24, 2026 •

Copy link
Copy Markdown
Member

❓ Type of change

  • 📖 Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

The branch adds more native transform.filter goodies.

More id/code prefilters across the bundler transforms, Vite-only transforms, and React/Solid/Svelte streaming transforms while keeping transformInclude for unplugin adapter compatibility.

I have removed the old wrapper helpers, centralizes shared and dropped tests that only asserted filter object shape.

Summary by CodeRabbit

  • Bug Fixes
    • Tightened head/SEO transform conditions so they only run when relevant composable patterns (including useHeadSafe) are present.
    • Improved streaming composable detection and ensured skipped inputs consistently return no transform result across React, Solid, and Svelte.
    • Reduced unnecessary work in minification and SSR head.ssr replacement transforms for more reliable builds.
  • Tests
    • Updated and removed filter-related assertions to match the new skip/targeting behavior.

@coderabbitai

coderabbitai Bot commented Jun 24, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 6df04d09-349c-45c7-88f9-bfd1a722b094

📥 Commits

Reviewing files that changed from the base of the PR and between a2e9835 and 6c47837.

📒 Files selected for processing (8)
  • packages/bundler/src/devtools/vite.ts
  • packages/bundler/src/unplugin/SSRStaticReplace.ts
  • packages/react/src/stream/plugin.ts
  • packages/react/test/vite-plugin.test.ts
  • packages/solid-js/src/stream/plugin.ts
  • packages/solid-js/test/vite-plugin.test.ts
  • packages/svelte/src/stream/plugin.ts
  • packages/svelte/test/vite-plugin.test.ts
🚧 Files skipped from review as they are similar to previous changes (5)
  • packages/solid-js/test/vite-plugin.test.ts
  • packages/solid-js/src/stream/plugin.ts
  • packages/bundler/src/unplugin/SSRStaticReplace.ts
  • packages/bundler/src/devtools/vite.ts
  • packages/react/src/stream/plugin.ts

📝 Walkthrough

Walkthrough

Replaces substring-based transform gating with regex-driven transformInclude/transform.filter logic across bundler unplugins and streaming plugins. Adds codeFilter?: RegExp to streaming plugin options and updates React, Solid-JS, and Svelte stream transforms to use the new code filter.

Changes

Transform Filter Refactor

Layer / File(s) Summary
Shared filter utility and StreamingPluginOptions contract
packages/bundler/src/unplugin/utils.ts, packages/unhead/src/stream/unplugin.ts
utils.ts exports NODE_MODULES_RE, JS_VUE_RE, and createJsVueTransformIdFilter. StreamingPluginOptions adds codeFilter?: RegExp, and transform.filter now includes it when present.
Devtools and CreateHeadTransform regex constants
packages/bundler/src/devtools/vite.ts, packages/bundler/src/unplugin/CreateHeadTransform.ts, packages/bundler/test/createHeadTransform.test.ts
HEAD_COMPOSABLE_RE and CREATE_HEAD_RE replace substring checks for head-composable and createHead detection; the test helper now exposes the plugin instance.
SSRStaticReplace and TreeshakeServerComposables refactor
packages/bundler/src/unplugin/SSRStaticReplace.ts, packages/bundler/src/unplugin/TreeshakeServerComposables.ts, packages/bundler/test/ssrStaticReplace.test.ts, packages/bundler/test/treeshakeServerComposables.test.ts
Both plugins move to explicit id/code predicates and structured transform filters, and the filter-assertion tests are removed.
MinifyTransform and UseSeoMetaTransform refactor
packages/bundler/src/unplugin/MinifyTransform.ts, packages/bundler/src/unplugin/UseSeoMetaTransform.ts, packages/bundler/test/minifyTransform.test.ts, packages/bundler/test/useSeoMetaTransform.test.ts
Both plugins switch to explicit transform predicates and regex-based code gating while keeping the AST rewrite and minification flow intact; the filter-only tests are removed.
Streaming plugins codeFilter wiring
packages/react/src/stream/plugin.ts, packages/solid-js/src/stream/plugin.ts, packages/svelte/src/stream/plugin.ts, packages/react/test/vite-plugin.test.ts, packages/solid-js/test/vite-plugin.test.ts, packages/svelte/test/vite-plugin.test.ts
React, Solid-JS, and Svelte streaming plugins use shared head-composable regex detection, pass codeFilter into plugin options, and update transform tests for skipped results and useHeadSafe cases.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

  • unjs/unhead#731: Shares the packages/bundler/src/devtools/vite.ts head-composable detection path updated here.
  • unjs/unhead#780: Touches packages/bundler/src/unplugin/MinifyTransform.ts, which this PR also restructures around transform gating.
  • unjs/unhead#782: Modifies the same unplugin transform filtering flow in packages/bundler/src/unplugin, overlapping with the predicate-based refactor here.

Poem

🐇 I hop through filters, clean and neat,
Regex whiskers make code sweet.
useHeadSafe now joins the ride,
While stream and bundler hop in stride.
A tidy burrow, sharp and bright—
This rabbit nuzzles transforms right.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise, conventional, and matches the PR's main change: adding more transform hook filters.
Description check ✅ Passed Most required sections are present and the change is described clearly; only the linked issue section is missing.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 4

🧹 Nitpick comments (2)
packages/bundler/src/unplugin/SSRStaticReplace.ts (1)

6-7: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Consolidate the duplicated head.ssr patterns.

HEAD_SSR_FILTER_RE and HEAD_SSR_RE share the same source and differ only by the g flag. Keeping two literals risks them drifting if the pattern is ever edited. Consider deriving the global variant from a single source.

♻️ Optional consolidation
-const HEAD_SSR_FILTER_RE = /\bhead\.ssr\b/
-const HEAD_SSR_RE = /\bhead\.ssr\b/g
+const HEAD_SSR_FILTER_RE = /\bhead\.ssr\b/
+const HEAD_SSR_RE = new RegExp(HEAD_SSR_FILTER_RE.source, 'g')
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/bundler/src/unplugin/SSRStaticReplace.ts` around lines 6 - 7, The
two regex constants in SSRStaticReplace duplicate the same `head.ssr` source, so
update the module to keep a single source of truth and derive the global variant
from it. Refactor the `HEAD_SSR_FILTER_RE` and `HEAD_SSR_RE` definitions so the
shared pattern is maintained in one place while preserving the non-global and
global behavior used elsewhere in SSRStaticReplace.
packages/bundler/src/unplugin/TreeshakeServerComposables.ts (1)

30-61: 📐 Maintainability & Code Quality | 🔵 Trivial | 🏗️ Heavy lift

DRY: shouldTransformId/TRANSFORM_RE are near-duplicated across plugins.

This shouldTransformId body (and TRANSFORM_RE) is essentially repeated in MinifyTransform.ts (Lines 64-85), UseSeoMetaTransform.ts (Lines 63-86) and here, differing only by the options.enabled short-circuit. Consider extracting a shared createShouldTransformId(options) helper in utils.ts to keep id-eligibility semantics aligned with createJsVueTransformIdFilter.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/bundler/src/unplugin/TreeshakeServerComposables.ts` around lines 30
- 61, The id-eligibility logic in shouldTransformId and the TRANSFORM_RE check
are duplicated across TreeshakeServerComposables, MinifyTransform, and
UseSeoMetaTransform, with only the options.enabled short-circuit differing.
Extract the shared matching behavior into a reusable
createShouldTransformId(options) helper in utils.ts, then update
shouldTransformId in TreeshakeServerComposables to use it so the semantics stay
aligned with createJsVueTransformIdFilter and future changes only need to be
made in one place.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/bundler/src/devtools/vite.ts`:
- Line 14: The transform prefilter in vite.ts is out of sync with the
composables list: HEAD_COMPOSABLE_RE currently misses useHeadSafe, so modules
using only that composable never reach the _source injection path. Update the
filter used in the Vite transform logic so it includes useHeadSafe, ideally by
deriving it from HEAD_COMPOSABLES instead of hardcoding the regex, and keep the
check aligned with the composables consumed by the transform.

In `@packages/react/src/stream/plugin.ts`:
- Around line 8-11: The head-composable detection in hasHeadComposable() is
missing useHeadSafe, so React modules using that API are skipped by both the
streaming trigger and codeFilter. Update HEAD_COMPOSABLE_RE in plugin.ts to
include useHeadSafe alongside useHead and useSeoMeta so the transform still runs
and HeadStream injection is applied for those files.

In `@packages/solid-js/src/stream/plugin.ts`:
- Around line 8-12: The head-composable detection regex currently excludes
useHeadSafe, so Solid components using that API are no longer picked up by
hasHeadComposable() or transform.filter.code and skip the streaming transform.
Update the HEAD_COMPOSABLE_RE pattern in plugin.ts to include useHeadSafe again,
and ensure the same shared match logic continues to cover both
hasHeadComposable() and the transform filter so all head composables are
transformed consistently.

In `@packages/svelte/src/stream/plugin.ts`:
- Around line 10-13: The head-composable trigger regex in hasHeadComposable now
misses useHeadSafe, which causes codeFilter to skip files that still need the
transform. Update HEAD_COMPOSABLE_RE so it matches useHeadSafe alongside useHead
and useSeoMeta, and ensure the same trigger is used consistently wherever the
filter/transform decision is made in plugin.ts.

---

Nitpick comments:
In `@packages/bundler/src/unplugin/SSRStaticReplace.ts`:
- Around line 6-7: The two regex constants in SSRStaticReplace duplicate the
same `head.ssr` source, so update the module to keep a single source of truth
and derive the global variant from it. Refactor the `HEAD_SSR_FILTER_RE` and
`HEAD_SSR_RE` definitions so the shared pattern is maintained in one place while
preserving the non-global and global behavior used elsewhere in
SSRStaticReplace.

In `@packages/bundler/src/unplugin/TreeshakeServerComposables.ts`:
- Around line 30-61: The id-eligibility logic in shouldTransformId and the
TRANSFORM_RE check are duplicated across TreeshakeServerComposables,
MinifyTransform, and UseSeoMetaTransform, with only the options.enabled
short-circuit differing. Extract the shared matching behavior into a reusable
createShouldTransformId(options) helper in utils.ts, then update
shouldTransformId in TreeshakeServerComposables to use it so the semantics stay
aligned with createJsVueTransformIdFilter and future changes only need to be
made in one place.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: bc5ec1bc-be2d-4fab-a714-42472c7bce45

📥 Commits

Reviewing files that changed from the base of the PR and between 8d87797 and a2e9835.

📒 Files selected for processing (19)
  • packages/bundler/src/devtools/vite.ts
  • packages/bundler/src/unplugin/CreateHeadTransform.ts
  • packages/bundler/src/unplugin/MinifyTransform.ts
  • packages/bundler/src/unplugin/SSRStaticReplace.ts
  • packages/bundler/src/unplugin/TreeshakeServerComposables.ts
  • packages/bundler/src/unplugin/UseSeoMetaTransform.ts
  • packages/bundler/src/unplugin/utils.ts
  • packages/bundler/test/createHeadTransform.test.ts
  • packages/bundler/test/minifyTransform.test.ts
  • packages/bundler/test/ssrStaticReplace.test.ts
  • packages/bundler/test/treeshakeServerComposables.test.ts
  • packages/bundler/test/useSeoMetaTransform.test.ts
  • packages/react/src/stream/plugin.ts
  • packages/react/test/vite-plugin.test.ts
  • packages/solid-js/src/stream/plugin.ts
  • packages/solid-js/test/vite-plugin.test.ts
  • packages/svelte/src/stream/plugin.ts
  • packages/svelte/test/vite-plugin.test.ts
  • packages/unhead/src/stream/unplugin.ts
💤 Files with no reviewable changes (4)
  • packages/bundler/test/ssrStaticReplace.test.ts
  • packages/bundler/test/useSeoMetaTransform.test.ts
  • packages/bundler/test/treeshakeServerComposables.test.ts
  • packages/bundler/test/minifyTransform.test.ts

Comment thread packages/bundler/src/devtools/vite.ts Resolved
Comment thread packages/react/src/stream/plugin.ts Resolved
Comment thread packages/solid-js/src/stream/plugin.ts Resolved
Comment thread packages/svelte/src/stream/plugin.ts Resolved
@harlan-zw
harlan-zw merged commit 2470401 into unjs:main Jun 25, 2026
3 of 4 checks passed
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.

2 participants