Skip to content

Compiler ignores previous plugin's source map, results in wrong source map #18491

Description

@K1DV5

Describe the bug

Hello, I'm working on a Vite plugin (wuchale) and use magic-string to transform Svelte code and generate the source map. Sometimes, the source maps that the browser sees are sometimes wrong, and I used vite-plugin-inspect to check the output of wuchale and it seems correct, but then Svelte's compiler messes it up after that.

Reproduction

Since the reproduction can't work in the REPL, and since it is fairly small, I'll provide the files here.

Start from the svelte template in npm create vite@latest, and edit only these two files:

// vite.config.js

import { defineConfig } from 'vite'
import { svelte } from '@sveltejs/vite-plugin-svelte'
import MagicString from 'magic-string'

// https://vite.dev/config/
export default defineConfig({
    plugins: [
        {
            name: "tst",
            transform(code, id) {
                if (!id.includes('App.svelte')) {
                    return
                }
                const m = new MagicString(code)
                m.appendRight(10, 'let leben = 42\n\n')
                console.log(m.toString())
                return {
                    code: m.toString(),
                    map: m.generateMap(),
                }
            },
        },
        svelte()
    ],
})
<!-- App.svelte -->

<script>

console.error('helo')

</script>

Hello, see devtools

Then run it, go to devtools, and look at the location of the console.error message, it will be at the beginning of the script block, not at the actual location.

Logs

Not relevant

System Info

System:
    OS: Linux 6.6 AlmaLinux 10.2 (Lavender Lion)
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-1195G7 @ 2.90GHz
    Memory: 5.62 GB / 7.58 GB
    Container: Yes
    Shell: 5.2.26 - /bin/bash
  Binaries:
    Node: 24.14.1 - /usr/bin/node
    npm: 11.11.0 - /usr/bin/npm
    bun: 1.3.11 - /home/K1DV5/.local/bin/bun
  npmPackages:
    svelte: ^5.56.4 => 5.56.4

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions