Skip to content

@tsdown/css: css.inject leaves emitted sourcemaps one line off #1132

Open

Description

@bhovhannes

Reproduction link or steps

https://stackblitz.com/~/github.com/bhovhannes/tsdown-css-sourcemap-offset-issue-repro?file=README.md

Steps are written in README.md

What is expected?

With sourcemap: true and css: { inject: true }, the emitted .map file should map every position in the output back to the right line of the original source.

In the reproduction, console.log('MARKER') is on line 5 of src/index.ts, so its position in dist/index.mjs should map back to line 5.

What is actually happening?

Every mapping in a chunk that gets the injected import './style.css'; points one line below the real source:

dist/index.mjs starts with: "import './style.css';"
MARKER in src/index.ts:  line 5, column 2
MARKER in dist/index.mjs: line 5, column 1
sourcemap maps it to:    line 6, column 2 (../src/index.ts)

FAIL: the sourcemap is off by 1 line(s).

With css: { inject: false }, the same check passes. In DevTools, breakpoints, stack traces and console locations in affected chunks point one line below the real source.

Any additional comments?

Versions: tsdown 0.23.7, @tsdown/css 0.23.7, rolldown 1.3.2 (native and @rolldown/browser).

Run npm install, npm run build, npm run verify in the reproduction. scripts/verify.mjs finds the marker in dist/index.mjs and maps it back through dist/index.mjs.map.

Cause: the @tsdown/css:inject plugin prepends the import in generateBundle and shifts chunk.map to match:

chunk.code = `import '${relativePath}';\n${chunk.code}`
if (chunk.map) chunk.map.mappings = `;${chunk.map.mappings}`

By the time generateBundle runs, rolldown has already added the sourcemap to the bundle as a separate <chunk>.map asset. Changing chunk.map does not update that asset, so the written .map file keeps the unshifted mappings. A debug plugin with generateBundle: { order: 'post' } confirms this: chunk.map.mappings has one more leading ; than the emitted .map asset.

Possible fixes:

  • Also update the chunk's .map asset in the bundle when chunk.map is shifted.
  • Or prepend the import earlier, e.g. in renderChunk, and return a map that rolldown composes into the final sourcemap.

As a workaround, we use a generateBundle plugin with order: 'post' that copies chunk.map.mappings into the emitted .map asset.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Fields

    Priority

    None yet

    Effort

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions