Skip to content

perf(bundler): optimize minify transform - #780

Merged
harlan-zw merged 2 commits into
unjs:mainfrom
TheAlexLichter:perf/optimize-minify-transform
Jun 24, 2026
Merged

harlan-zw merged 2 commits into
unjs:mainfrom
TheAlexLichter:perf/optimize-minify-transform

Conversation

@TheAlexLichter

@TheAlexLichter TheAlexLichter commented Jun 21, 2026 •

Copy link
Copy Markdown
Member

Summary

This PR optimizes the minify transform with fast paths, caching, and broader import handling.

Measurements

Measured on Node 24.17.0 / macOS, medians after warmup:

Workload Before After Change
156-file read-only Nuxt source corpus, 2.5 MB total, no-op minifiers 160.13 ms 31.06 ms -80.6%
156 false-positive modules, 6.7 KiB each 116.12 ms 1.08 ms -99.1%
156 identical literals with Rolldown, sequential transforms 8.95 ms / 156 calls 4.52 ms / 1 call -49.5%

Summary by CodeRabbit

Summary by CodeRabbit

  • Bug Fixes

    • Improved detection of useHead/useServerHead calls across aliased, namespace, and member-expression import patterns.
    • Prevented unintended transforms when matching identifiers are shadowed by local parameters.
  • Performance

    • Added a fast path to avoid unnecessary parsing when inline HTML/text isn’t likely present.
    • Upgraded inline script/style minification with cached, deduplicated concurrent requests and safer “only-shorter” replacements.
  • Tests

    • Expanded coverage for comments between content keys and values, escaped content keys, concurrency deduplication, and sourcemap content preservation.

@coderabbitai

coderabbitai Bot commented Jun 21, 2026 •

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

Pull request was closed or merged during review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: eb517a41-9d4e-463d-8e3f-cb4e711187af

📥 Commits

Reviewing files that changed from the base of the PR and between cb41c7f and 6e3b068.

📒 Files selected for processing (2)
  • packages/bundler/src/unplugin/MinifyTransform.ts
  • packages/bundler/test/minifyTransform.test.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/bundler/test/minifyTransform.test.ts
  • packages/bundler/src/unplugin/MinifyTransform.ts

📝 Walkthrough

Walkthrough

MinifyTransform now collects pending inline minification work during AST traversal, deduplicates repeated minification through a per-tag promise cache, and applies MagicString overwrites after all pending results resolve. Tests expand for import resolution, escaped names, concurrency, and sourcemap output.

Changes

MinifyTransform refactor and tests

Layer / File(s) Summary
PendingMinification type and cache setup
packages/bundler/src/unplugin/MinifyTransform.ts
Adds the tag-type constants, cache limit, PendingMinification record, and the per-tag minifyCache map used for inline content minification.
Parse gate, AST collection, and overwrite application
packages/bundler/src/unplugin/MinifyTransform.ts
Adds the parse fast-path, routes head-call detection through resolveHeadFunctionName, collects PendingMinification entries, resolves all pending minifications, and applies MagicString overwrites only when the minified output is shorter.
Helper refactor and coverage updates
packages/bundler/test/minifyTransform.test.ts
Refactors the test harness into transformWithPlugin and adds coverage for aliased imports, namespace imports, shadowed identifiers, escaped content names, comments between property and value, concurrent deduplication, and sourcemap contents.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Poem

🐇 I hop through scripts both neat and small,
I cache the minify work for one and all.
With fast-path eyes and sourcemaps bright,
I trim the fluff and keep things light.
Hop, hop—MagicString makes it shine,
A tidy bundle, line by line!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.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 clearly matches the main change: optimizing the bundler minify transform.
Description check ✅ Passed Mostly complete, though it omits the linked issue and type-of-change sections required by the template.
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.

@TheAlexLichter
TheAlexLichter marked this pull request as draft June 21, 2026 12:14
@TheAlexLichter
TheAlexLichter force-pushed the perf/optimize-minify-transform branch from b63582a to cb41c7f Compare June 21, 2026 12:41
@TheAlexLichter
TheAlexLichter marked this pull request as ready for review June 21, 2026 12:50
@harlan-zw

Copy link
Copy Markdown
Collaborator

Woo impressive, thanks for your work 💚

@harlan-zw
harlan-zw merged commit cc9c36c into unjs:main Jun 24, 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