mirror of https://github.com/sveltejs/svelte
fix: account for sourcemap in meta info (#8778)
We need to use a different method for getting the meta info because `locate` is used to help construct the source map that references the preprocessed Svelte file. If we would now add source maps to that `locate` function it would go the the original source directly which means skipping potentially intermediate source maps which we would need in other situations. Sadly we can't map the character offset because for that we would need to the original source contents which we don't have in this context. fixes #8360 closes #8362pull/8772/head
parent
5702142d9e
commit
ef1b98f9d9
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'svelte': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix: account for preprocessor source maps when calculating meta info
|
@ -0,0 +1,33 @@
|
|||||||
|
import MagicString from 'magic-string';
|
||||||
|
import * as path from 'node:path';
|
||||||
|
|
||||||
|
// fake preprocessor by doing transforms on the source
|
||||||
|
const str = new MagicString(
|
||||||
|
`<script>
|
||||||
|
type Foo = 'foo';
|
||||||
|
let foo = 'foo';
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<h1>{foo}</h1>
|
||||||
|
`.replace(/\r\n/g, '\n')
|
||||||
|
);
|
||||||
|
str.remove(8, 26); // remove line type Foo = ...
|
||||||
|
str.remove(55, 56); // remove whitespace before <h1>
|
||||||
|
|
||||||
|
export default {
|
||||||
|
compileOptions: {
|
||||||
|
dev: true,
|
||||||
|
sourcemap: str.generateMap({ hires: true })
|
||||||
|
},
|
||||||
|
|
||||||
|
test({ assert, target }) {
|
||||||
|
const h1 = target.querySelector('h1');
|
||||||
|
|
||||||
|
assert.deepEqual(h1.__svelte_meta.loc, {
|
||||||
|
file: path.relative(process.cwd(), path.resolve(__dirname, 'main.svelte')),
|
||||||
|
line: 5, // line 4 in main.svelte, but that's the preprocessed code, the original code is above in the fake preprocessor
|
||||||
|
column: 1, // line 0 in main.svelte, but that's the preprocessed code, the original code is above in the fake preprocessor
|
||||||
|
char: 38 // TODO this is wrong but we can't backtrace it due to limitations, see add_location function usage comment for more info
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
@ -0,0 +1,5 @@
|
|||||||
|
<script>
|
||||||
|
let foo = 'foo';
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<h1>{foo}</h1>
|
Loading…
Reference in new issue