diff --git a/src/compiler/preprocess/index.ts b/src/compiler/preprocess/index.ts index 29a5e45931..e878cdc019 100644 --- a/src/compiler/preprocess/index.ts +++ b/src/compiler/preprocess/index.ts @@ -191,8 +191,10 @@ function get_replacement( decoded_map = decoded_sourcemap_from_generator(decoded_map); } // offset only segments pointing at original component source - const source_index = decoded_map.sources.findIndex(source => source === file_basename); - sourcemap_add_offset(decoded_map, get_location(offset + prefix.length), source_index); + const source_index = decoded_map.sources.indexOf(file_basename); + 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); diff --git a/src/compiler/utils/string_with_sourcemap.ts b/src/compiler/utils/string_with_sourcemap.ts index 3dbd0a674a..4b4be28846 100644 --- a/src/compiler/utils/string_with_sourcemap.ts +++ b/src/compiler/utils/string_with_sourcemap.ts @@ -15,7 +15,7 @@ function last_line_length(s: string) { export function sourcemap_add_offset( map: DecodedSourceMap, offset: SourceLocation, source_index: number ) { - if (map.mappings.length == 0 || source_index < 0) return; + if (map.mappings.length == 0) return; // shift lines for (let line = 0; line < map.mappings.length; line++) { const segment_list = map.mappings[line]; diff --git a/test/sourcemaps/samples/concat/_config.js b/test/sourcemaps/samples/concat/_config.js new file mode 100644 index 0000000000..835b3c9e48 --- /dev/null +++ b/test/sourcemaps/samples/concat/_config.js @@ -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); + } + } + ] +}; diff --git a/test/sourcemaps/samples/concat/input.svelte b/test/sourcemaps/samples/concat/input.svelte new file mode 100644 index 0000000000..18a1ac775f --- /dev/null +++ b/test/sourcemaps/samples/concat/input.svelte @@ -0,0 +1,3 @@ + + +