mirror of https://github.com/sveltejs/svelte
When a bundler plugin (e.g. a Vite plugin using MagicString.generateMap({ source: id })
where id is the absolute module path) passes a sourcemap to compileOptions.sourcemap,
the source entry is a full or relative path (e.g. /project/src/App.svelte) rather than
just the basename. The previous fix (PR #18518) only handled empty sources[0] (from
generateMap() without a source option), but this case was still failing because a strict
string comparison against the basename didn't match, causing @jridgewell/remapping to
silently drop the upstream chain and produce wrong devtools/stack-trace positions.
Fixes #18778
pull/18882/head
parent
4bf15ae6f3
commit
8b0e42ff7f
@ -0,0 +1,5 @@
|
||||
---
|
||||
'svelte': patch
|
||||
---
|
||||
|
||||
fix: correctly chain upstream plugin sourcemaps whose `sources` contain full paths (e.g. from `MagicString.generateMap({ source: id })`) by normalizing source entries to the file basename before remapping
|
||||
@ -0,0 +1,23 @@
|
||||
import * as fs from 'node:fs';
|
||||
import MagicString from 'magic-string';
|
||||
import { test } from '../../test';
|
||||
|
||||
// Simulates a bundler plugin (e.g. a Vite plugin using `magic-string`) that transforms
|
||||
// the Svelte source *before* it reaches `compile()` and calls `generateMap({ source: id })`
|
||||
// where `id` is an absolute/fully-qualified module id (as Vite does). The source entry in
|
||||
// the resulting map is a full path (e.g. `/project/src/App.svelte`) rather than a basename,
|
||||
// which previously failed to match during map chaining (see #18778) and caused the upstream
|
||||
// mappings to be silently dropped, producing wrong devtools/stack-trace positions.
|
||||
const input = fs.readFileSync(new URL('./input.svelte', import.meta.url), 'utf-8');
|
||||
const src = new MagicString(input);
|
||||
// Perform a no-op transform but generate the map with a full absolute path as `source`
|
||||
// (basename is still `input.svelte`), reproducing the scenario from the bug report where
|
||||
// the reporter tried `m.generateMap({source: id})`, `m.generateMap({file: id})`, etc.
|
||||
const simulated_vite_id = '/fake/project/path/src/input.svelte';
|
||||
|
||||
export default test({
|
||||
compileOptions: {
|
||||
sourcemap: src.generateMap({ source: simulated_vite_id, hires: true })
|
||||
},
|
||||
client: [{ str: 'let doubled' }]
|
||||
});
|
||||
@ -0,0 +1,6 @@
|
||||
<script>
|
||||
let count = 0;
|
||||
let doubled = count * 2;
|
||||
</script>
|
||||
|
||||
<button>clicks: {count}</button>
|
||||
Loading…
Reference in new issue