Skip to content

Invalid sourcemap when there's \n in the script block #17003

@hyrious

Description

@hyrious

Describe the bug

Similar issues #13478 #16615 #9961. But here is a possible minimal reproduction:

Reproduction

Save and run this snippet

import * as svelte from 'svelte/compiler'

const code = `<script>
</script>
`

const { js, css } = svelte.compile(code, {
	filename: 'a.svelte',
})

const _16to8 = (x) => unescape(encodeURIComponent(x))

function sourceMapVisual(name, a) {
	if (!a?.code) return
	let [code, map] = [_16to8(a.code), _16to8(a.map)]
	let url =
    'https://evanw.github.io/source-map-visualization/#' +
    btoa(code.length + '\0' + code + map.length + '\0' + map).replace(/\=+$/, '')
	console.log(name, `\x1b]8;;${url}\x1b\\source-map-visualization\x1b]8;;\x07`, a.map)
}

sourceMapVisual(' JS', js)
sourceMapVisual('CSS', css)

Try to change the code to the following strings:

Single line of code: works fine.

<script>let a = 1</script>

Multi line: The "mappings" field of the imported source map contains invalid data. Invalid VLQ data at index 19: Invalid original column: -1. on Source Map Visualization.

<script>
  // You can put anything here
</script>

Logs

I was trying to integrate svelte in esbuild, while esbuild complains:

 [WARNING] Bad "mappings" data in source map at character 95: Invalid original column value: -1 [invalid-source-mappings]

    fixture/App.svelte#sourceMappingURL:1:415:
      1  ...ASK,KAAK,WAAU,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK;;;CAAI,KAAK,KAAK,CAAC;;;;;AAC7D,CAAC"}
                                                                                          ~~

  The source map "fixture/App.svelte#sourceMappingURL" was referenced by the file
  "fixture/App.svelte" here:

    fixture/App.svelte:18:21:
      18  ...rceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcy...
                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

System Info

  System:
    OS: macOS 26.0.1
    CPU: (8) arm64 Apple M3
    Memory: 1.13 GB / 24.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.20.0 - /opt/homebrew/opt/node@22/bin/node
    Yarn: 1.22.22 - /opt/homebrew/bin/yarn
    npm: 10.9.3 - /opt/homebrew/opt/node@22/bin/npm
    pnpm: 10.18.3 - /opt/homebrew/bin/pnpm
    bun: 1.2.23 - /Users/hyrious/.bun/bin/bun
    Deno: 2.5.4 - /opt/homebrew/bin/deno
  npmPackages:
    rollup: ^4.52.5 => 4.52.5
    svelte: ^5.41.1 => 5.41.1

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions