Add test for sourcemap offset

pull/5754/head
dmitrage 5 years ago
parent c842d1617f
commit 1c241dd1a8

@ -191,8 +191,10 @@ function get_replacement(
decoded_map = decoded_sourcemap_from_generator(decoded_map); decoded_map = decoded_sourcemap_from_generator(decoded_map);
} }
// offset only segments pointing at original component source // offset only segments pointing at original component source
const source_index = decoded_map.sources.findIndex(source => source === file_basename); const source_index = decoded_map.sources.indexOf(file_basename);
sourcemap_add_offset(decoded_map, get_location(offset + prefix.length), source_index); if (source_index !== -1) {
sourcemap_add_offset(decoded_map, get_location(offset + prefix.length), source_index);
}
} }
const processed_with_map = StringWithSourcemap.from_processed(processed.code, decoded_map); const processed_with_map = StringWithSourcemap.from_processed(processed.code, decoded_map);

@ -15,7 +15,7 @@ function last_line_length(s: string) {
export function sourcemap_add_offset( export function sourcemap_add_offset(
map: DecodedSourceMap, offset: SourceLocation, source_index: number map: DecodedSourceMap, offset: SourceLocation, source_index: number
) { ) {
if (map.mappings.length == 0 || source_index < 0) return; if (map.mappings.length == 0) return;
// shift lines // shift lines
for (let line = 0; line < map.mappings.length; line++) { for (let line = 0; line < map.mappings.length; line++) {
const segment_list = map.mappings[line]; const segment_list = map.mappings[line];

@ -0,0 +1,15 @@
import MagicString from 'magic-string';
import { magic_string_preprocessor_result } from '../../helpers';
export default {
js_map_sources: ['input.svelte'],
preprocess: [
{
script: ({ content }) => {
const src = new MagicString(content);
src.prepend('console.log("Injected first line");\n');
return magic_string_preprocessor_result('input.svelte', src);
}
}
]
};

@ -0,0 +1,3 @@
<script>console.log('Target')</script>
<h1>Hello</h1>

@ -0,0 +1,9 @@
import { assert_mapped } from '../../helpers';
export function test({ input, preprocessed }) {
assert_mapped({
code: 'Target',
input: input.locate,
preprocessed
});
}
Loading…
Cancel
Save