Skip to content

Generate sourcemaps for transformed modules - #78

Merged
nstepien merged 4 commits into
mainfrom
sourcemap-support
Aug 30, 2026
Merged

nstepien merged 4 commits into
mainfrom
sourcemap-support

Conversation

@nstepien

@nstepien nstepien commented Aug 30, 2026 •

Copy link
Copy Markdown
Owner

Problem

With build.sourcemap enabled, Rolldown/Vite logged SOURCEMAP_BROKEN warnings because the ecij transform hook returned modified code without a sourcemap, and emitted maps had empty mappings. (Sourcemaps were a listed TODO in the README.)

Changes

  • src/index.ts: apply the css``` `` replacements (overwrite) and side-effect import injection (prepend) through RolldownMagicString` — the Rust-backed magic-string implementation exported by rolldown itself. No new runtime dependency: the export exists since rolldown 1.2.0, the current peer floor. The descending-order replacement sort is obsolete (magic-string edits use original offsets) and was removed.
  • test/plugin.test.ts: new test building with sourcemaps enabled that expects zero plugin warnings, checks the map's sources/mappings, and traces the generated class name string in the output back to the exact line/column of the css``` `` tag it replaced (via new devDependency @jridgewell/trace-mapping). The build helper gained a sourcemapoption and now tolerates the extra.js.map` asset.
  • Docs: removed "Sourcemaps" from the README TODO; noted the mechanism in AGENTS.md.

When Rolldown provides a lazily-created MagicString via the transform hook's meta.magicString, that instance is mutated and returned directly as code, letting Rolldown apply the edits and generate the sourcemap natively. When it is absent (e.g. Vite's dev-mode plugin container, which passes no magicString and wouldn't handle an object code), the hook falls back to the classic { code: string, map } shape with generateMap({ hires: 'boundary' }).

Verification

  • The new test failed with SOURCEMAP_BROKEN before the fix and passes after.
  • All 10 tests pass with no snapshot changes — the transformed output code is byte-identical; only maps were added. The test's exact-position assertion runs through the native MagicString path, since Vite builds run transform hooks through rolldown's bindings.
  • Tests also pass in CI mode (CI=true), where they import the built dist plugin instead of the source.
  • format, typecheck, build, test all green.

🤖 Generated with Claude Code

https://claude.ai/code/session_014gjLLnGPcEuNU9RydaN7W3

Summary by CodeRabbit

  • New Features

    • Improved sourcemap generation for CSS class-name transformations.
    • Sourcemaps now accurately connect generated code and extracted CSS back to original source locations.
  • Bug Fixes

    • Prevented unnecessary transformations when no CSS class replacements are detected.
    • Reduced plugin warnings during sourcemap-enabled builds.
  • Documentation

    • Expanded transform-hook documentation.
    • Removed sourcemaps from the README TODO list.

When build.sourcemap is enabled, Rolldown/Vite warned with
SOURCEMAP_BROKEN because the transform hook returned modified code
without a sourcemap.

Apply the css`` replacements and side-effect import injection through
RolldownMagicString (the Rust-backed magic-string shipped with
rolldown, available since 1.2.0 - no new runtime dependency) and
return the generated map alongside the transformed code.

Add a test that builds with sourcemaps enabled, expects zero plugin
warnings, and traces a generated class name string back to the exact
position of the css`` tag it replaced.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014gjLLnGPcEuNU9RydaN7W3
@coderabbitai

coderabbitai Bot commented Aug 30, 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 Plus

Run ID: 276b1496-9f40-4412-832b-d93ba3d5c581

📥 Commits

Reviewing files that changed from the base of the PR and between 1a3e47d and fd05bf8.

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (5)
  • AGENTS.md
  • README.md
  • package.json
  • src/index.ts
  • test/plugin.test.ts
💤 Files with no reviewable changes (1)
  • README.md

Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

The plugin now applies CSS class replacements through RolldownMagicString. The transform hook preserves provided magic strings or generates code and sourcemaps. Tests verify sourcemap mappings, CSS output handling, and warning-free builds. Documentation and development dependencies reflect sourcemap support.

Changes

CSS extraction sourcemaps

Layer / File(s) Summary
Magic-string extraction
src/index.ts
extractCssFromCode creates or reuses RolldownMagicString and applies CSS class replacements through overwrite.
Transform integration and sourcemaps
src/index.ts
The transform hook passes metadata to CSS extraction, prepends imports through the magic string, and returns native or generated sourcemaps.
Validation and project metadata
test/plugin.test.ts, AGENTS.md, README.md, package.json
Tests validate sourcemap mappings and build output. Documentation and development dependencies reflect the new sourcemap behavior.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Merge Risk: ⚪ Minimal · up to fd05b

This change adds sourcemap generation while preserving transformed output, with the stated formatting, typecheck, build, and test checks passing. No actionable merge-blocking risk remains.

Sequence Diagram(s)

sequenceDiagram
  participant ViteBuild
  participant TransformHook
  participant extractCssFromCode
  participant RolldownMagicString
  ViteBuild->>TransformHook: provide source code and transform metadata
  TransformHook->>extractCssFromCode: pass code and magic string metadata
  extractCssFromCode->>RolldownMagicString: overwrite CSS class ranges
  TransformHook->>RolldownMagicString: prepend CSS imports
  RolldownMagicString-->>ViteBuild: return transformed code and sourcemap
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 2 files. (2 skipped: 2… Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the pull request's primary change: adding sourcemap generation for transformed modules.
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.
Full details: Docstring Coverage

Explanation

Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 2 files. (2 skipped: 2 unsupported.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch sourcemap-support

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.

nstepien and others added 2 commits August 30, 2026 23:03
Rolldown provides a lazily-created RolldownMagicString for the
module's code via the transform hook's meta argument. Reuse it and
return the instance itself as `code`, letting rolldown apply the
edits and generate the sourcemap natively instead of serializing a
JSON map through the JS side.

When meta.magicString is absent (e.g. Vite's dev-mode plugin
container), fall back to constructing the instance and returning
stringified code plus an explicit sourcemap as before.

The existing sourcemap test exercises the native path, as Vite builds
run transform hooks through rolldown's bindings.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014gjLLnGPcEuNU9RydaN7W3
Use plain assertions instead of inline snapshots and a manual throw:
- expect(logs).toStrictEqual([]) states the "no warnings" intent directly
- vitest's assert() narrows the map instead of an if/throw guard

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014gjLLnGPcEuNU9RydaN7W3
@nstepien
nstepien marked this pull request as ready for review August 30, 2026 22:35
@nstepien
nstepien merged commit 72a621a into main Aug 30, 2026
5 checks passed
@nstepien
nstepien deleted the sourcemap-support branch August 30, 2026 22:45
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