Skip to content

fix(css): keep the emitted .map asset aligned when css.inject prepends the import - #1049

Open
NullVoxPopuli wants to merge 2 commits into
rolldown:mainfrom
NullVoxPopuli:fix/css-inject-sourcemap-asset
Open

fix(css): keep the emitted .map asset aligned when css.inject prepends the import#1049
NullVoxPopuli wants to merge 2 commits into
rolldown:mainfrom
NullVoxPopuli:fix/css-inject-sourcemap-asset

Conversation

@NullVoxPopuli

Copy link
Copy Markdown

css.inject prepends import './style.css'; to styled chunks in generateBundle and compensates chunk.map with a ; mappings prefix. that compensation is correct — but rolldown materializes the companion .map asset before generateBundle runs and doesn't propagate chunk.map mutations into it (rolldown/rolldown#10676). so the emitted map is one line behind the emitted code, and every mapping in every css-injecting chunk points one line early.

found this while testing sourcemap alignment for ember-scoped-css's tsdown build — every component in the bundle was misaligned, cascading (in this kind of visualization, each statement highlights the line above itself).

the fix: extract the duplicated prepend logic into prependCssImport(), which also refreshes the .map asset from the mutated chunk.map. works with today's rolldown; harmless if rolldown starts syncing the asset itself.

the test decodes the mappings VLQ inline (no new dependency) and asserts export const value = 42 traces to its exact original line through the injected import. red without the fix, green with it.


Disclosure: this investigation, fix, and write-up are AI-assisted exploration (Claude Fable 5 via Claude Code), directed and reviewed by me.

🤖 Generated with Claude Code

…e import

The inject plugin prepends `import './style.css';` to styled chunks in
generateBundle and compensates chunk.map with a `;` mappings prefix -- but
rolldown materializes the companion .map asset before generateBundle runs,
and chunk.map mutations do not propagate into it (unlike rollup). The
emitted map ended up one line behind the emitted code, so every mapping in
every css.inject'd chunk pointed one line early.

Extract the shared prepend logic into prependCssImport() and have it also
refresh the .map asset from the mutated chunk.map.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Copilot AI lite review requested due to automatic review settings August 13, 2026 21:28
@netlify

netlify Bot commented Aug 13, 2026

Copy link
Copy Markdown

Deploy Preview for tsdown-main ready!

Name Link
🔨 Latest commit 9dcba0c
🔍 Latest deploy log https://app.netlify.com/projects/tsdown-main/deploys/6a7e42d90dba0b0008dc3072
😎 Deploy Preview https://deploy-preview-1049--tsdown-main.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@pkg-pr-new

pkg-pr-new Bot commented Aug 13, 2026

Copy link
Copy Markdown

Open in StackBlitz

tsdown

pnpm add https://pkg.pr.new/tsdown@1049 -D
npm i https://pkg.pr.new/tsdown@1049 -D
yarn add https://pkg.pr.new/tsdown@1049.tgz -D

create-tsdown

pnpm add https://pkg.pr.new/create-tsdown@1049 -D
npm i https://pkg.pr.new/create-tsdown@1049 -D
yarn add https://pkg.pr.new/create-tsdown@1049.tgz -D

@tsdown/css

pnpm add https://pkg.pr.new/@tsdown/css@1049 -D
npm i https://pkg.pr.new/@tsdown/css@1049 -D
yarn add https://pkg.pr.new/@tsdown/css@1049.tgz -D

@tsdown/exe

pnpm add https://pkg.pr.new/@tsdown/exe@1049 -D
npm i https://pkg.pr.new/@tsdown/exe@1049 -D
yarn add https://pkg.pr.new/@tsdown/exe@1049.tgz -D

tsdown-migrate

pnpm add https://pkg.pr.new/tsdown-migrate@1049 -D
npm i https://pkg.pr.new/tsdown-migrate@1049 -D
yarn add https://pkg.pr.new/tsdown-migrate@1049.tgz -D

commit: 9dcba0c

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Fixes sourcemap misalignment when css.inject prepends an import during generateBundle by ensuring the emitted companion .map asset is updated to match the mutated chunk.map (working around current rolldown behavior).

Changes:

  • Extracted CSS import-prepend logic into prependCssImport() and refreshed the emitted ${chunk.fileName}.map asset after adjusting mappings.
  • Added a sourcemap alignment regression test for css.inject that decodes VLQ mappings and verifies a specific statement maps back to the correct original line.
  • Added a new snapshot covering the injected import, emitted map, and CSS output.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
tests/css.test.ts Adds a regression test that decodes mappings and asserts emitted map alignment after injected CSS import.
tests/snapshots/sourcemap/css-inject-keeps-the-emitted-map-aligned-with-the-injected-import.snap.md Snapshot verifying the injected import and updated sourcemap mappings.
packages/css/src/plugin.ts Refactors prepend logic into prependCssImport() and updates the .map asset to reflect chunk.map mutations.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread tests/css.test.ts
Review feedback: indexOf() returning -1 was treated as a valid digit via
the bitwise ops. The helper now throws on characters outside the base64
alphabet and on a dangling continuation bit at end of segment.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Suppressed comments (1)

tests/css.test.ts:1871

  • generatedLine becomes 0 if findIndex() fails (returns -1), which makes the subsequent sourcemap assertion fail with a less helpful message. Capturing the index separately and asserting it was found improves test diagnostics and avoids silently passing an invalid line number into originalLineFor().
      const generatedLine =
        code.split('\n').findIndex((line) => line.includes('const value')) + 1

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