From 1c241dd1a83458a4dbf3add79a8ac9a37c1a10f6 Mon Sep 17 00:00:00 2001 From: dmitrage Date: Thu, 10 Dec 2020 01:34:32 +0300 Subject: [PATCH] Add test for sourcemap offset --- src/compiler/preprocess/index.ts | 6 ++++-- src/compiler/utils/string_with_sourcemap.ts | 2 +- test/sourcemaps/samples/concat/_config.js | 15 +++++++++++++++ test/sourcemaps/samples/concat/input.svelte | 3 +++ test/sourcemaps/samples/concat/test.js | 9 +++++++++ 5 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 test/sourcemaps/samples/concat/_config.js create mode 100644 test/sourcemaps/samples/concat/input.svelte create mode 100644 test/sourcemaps/samples/concat/test.js 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 @@ + + +

Hello

\ No newline at end of file diff --git a/test/sourcemaps/samples/concat/test.js b/test/sourcemaps/samples/concat/test.js new file mode 100644 index 0000000000..0d7af05407 --- /dev/null +++ b/test/sourcemaps/samples/concat/test.js @@ -0,0 +1,9 @@ +import { assert_mapped } from '../../helpers'; + +export function test({ input, preprocessed }) { + assert_mapped({ + code: 'Target', + input: input.locate, + preprocessed + }); +}