From be5215588cc73d677e1d28f9c05b0ed455ebcabd Mon Sep 17 00:00:00 2001 From: dominikg Date: Fri, 30 Jul 2021 17:56:27 +0200 Subject: [PATCH 1/4] [fix] use consistent relative source filename for js sourcemaps --- src/compiler/compile/Component.ts | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/compiler/compile/Component.ts b/src/compiler/compile/Component.ts index 6a8d5b178ef0..1ac7f211913b 100644 --- a/src/compiler/compile/Component.ts +++ b/src/compiler/compile/Component.ts @@ -343,19 +343,21 @@ export default class Component { ? { code: null, map: null } : result.css; + const sourcemap_source_filename = get_sourcemap_source_filename(compile_options); + js = print(program, { - sourceMapSource: compile_options.filename + sourceMapSource: sourcemap_source_filename }); js.map.sources = [ - compile_options.filename ? get_relative_path(compile_options.outputFilename || '', compile_options.filename) : null + sourcemap_source_filename ]; js.map.sourcesContent = [ this.source ]; - js.map = apply_preprocessor_sourcemap(this.file, js.map, compile_options.sourcemap as (string | RawSourceMap | DecodedSourceMap)); + js.map = apply_preprocessor_sourcemap(sourcemap_source_filename, js.map, compile_options.sourcemap as (string | RawSourceMap | DecodedSourceMap)); } return { @@ -1551,3 +1553,18 @@ function get_relative_path(from: string, to: string) { return from_parts.concat(to_parts).join('/'); } + +function get_basename(filename: string) { + return filename.split(/[/\\]/).pop(); +} + +function get_sourcemap_source_filename(compile_options: CompileOptions) { + if (compile_options.filename) { + if (compile_options.outputFilename) { + return get_relative_path(compile_options.outputFilename, compile_options.filename); + } else { + return get_basename(compile_options.filename); + } + } + return null; +} From 9537f987eeae8a231b33a8d7a22acb922158d137 Mon Sep 17 00:00:00 2001 From: dominikg Date: Fri, 30 Jul 2021 23:44:40 +0200 Subject: [PATCH 2/4] chore: improve codestyle --- src/compiler/compile/Component.ts | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/compiler/compile/Component.ts b/src/compiler/compile/Component.ts index 1ac7f211913b..fabe9e0316ca 100644 --- a/src/compiler/compile/Component.ts +++ b/src/compiler/compile/Component.ts @@ -1559,12 +1559,9 @@ function get_basename(filename: string) { } function get_sourcemap_source_filename(compile_options: CompileOptions) { - if (compile_options.filename) { - if (compile_options.outputFilename) { - return get_relative_path(compile_options.outputFilename, compile_options.filename); - } else { - return get_basename(compile_options.filename); - } - } - return null; + if (!compile_options.filename) return null; + + return compile_options.outputFilename + ? get_relative_path(compile_options.outputFilename, compile_options.filename) + : get_basename(compile_options.filename); } From b8c121ce8c43bb34b50daee8907e448bd2c7ce45 Mon Sep 17 00:00:00 2001 From: dominikg Date: Sat, 31 Jul 2021 12:08:47 +0200 Subject: [PATCH 3/4] test: add testcase for correct sourcemap source filename when no output filename is specified --- .../_config.js | 18 ++++++++++++++++++ .../input.svelte | 1 + .../test.js | 3 +++ 3 files changed, 22 insertions(+) create mode 100644 test/sourcemaps/samples/sourcemap-basename-without-outputname/_config.js create mode 100644 test/sourcemaps/samples/sourcemap-basename-without-outputname/input.svelte create mode 100644 test/sourcemaps/samples/sourcemap-basename-without-outputname/test.js diff --git a/test/sourcemaps/samples/sourcemap-basename-without-outputname/_config.js b/test/sourcemaps/samples/sourcemap-basename-without-outputname/_config.js new file mode 100644 index 000000000000..d9004f7e585d --- /dev/null +++ b/test/sourcemaps/samples/sourcemap-basename-without-outputname/_config.js @@ -0,0 +1,18 @@ +export const component_filepath = 'src/some/deep/path/input.svelte'; + +export const component_file_basename = 'input.svelte'; + +export default { + + js_map_sources: [component_file_basename], + + options: { + filename: component_filepath + }, + compile_options: { + filename: component_filepath, + // ../../index.ts initializes output filenames, reset to undefined for this test + outputFilename: undefined, + cssOutputFilename: undefined + } +}; diff --git a/test/sourcemaps/samples/sourcemap-basename-without-outputname/input.svelte b/test/sourcemaps/samples/sourcemap-basename-without-outputname/input.svelte new file mode 100644 index 000000000000..4548af5548aa --- /dev/null +++ b/test/sourcemaps/samples/sourcemap-basename-without-outputname/input.svelte @@ -0,0 +1 @@ +

sourcemap-basename-without-outputname

diff --git a/test/sourcemaps/samples/sourcemap-basename-without-outputname/test.js b/test/sourcemaps/samples/sourcemap-basename-without-outputname/test.js new file mode 100644 index 000000000000..c5747713a7fb --- /dev/null +++ b/test/sourcemaps/samples/sourcemap-basename-without-outputname/test.js @@ -0,0 +1,3 @@ +// no additional test needed, _config.js values and the js.map.sources assertion in ../../index.ts cover it +export function test() {} + From 9ae3cb52a874adce0217dcb19284089a1f1bd01e Mon Sep 17 00:00:00 2001 From: dominikg Date: Mon, 2 Aug 2021 13:18:24 +0200 Subject: [PATCH 4/4] test: add css sourcemap to testcase for output filename --- .../_config.js | 21 ++++++++++++++++--- .../input.svelte | 1 + 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/test/sourcemaps/samples/sourcemap-basename-without-outputname/_config.js b/test/sourcemaps/samples/sourcemap-basename-without-outputname/_config.js index d9004f7e585d..15c346bfb9b9 100644 --- a/test/sourcemaps/samples/sourcemap-basename-without-outputname/_config.js +++ b/test/sourcemaps/samples/sourcemap-basename-without-outputname/_config.js @@ -1,11 +1,26 @@ -export const component_filepath = 'src/some/deep/path/input.svelte'; +import {magic_string_bundle} from '../../helpers'; + +export const component_filepath = 'src/some/deep/path/input.svelte'; export const component_file_basename = 'input.svelte'; +export const css_file_basename = 'input.css'; -export default { +const input_css = ' h1 {color: blue;}'; +export default { + preprocess: [ + { + style: ({ content, filename }) => { + const style_to_add = `/* Filename from preprocess: ${filename} */` + input_css; + return magic_string_bundle([ + { code: content, filename: component_file_basename }, + { code: style_to_add, filename: css_file_basename } + ],component_filepath); + } + } + ], js_map_sources: [component_file_basename], - + css_map_sources: [css_file_basename,component_file_basename], options: { filename: component_filepath }, diff --git a/test/sourcemaps/samples/sourcemap-basename-without-outputname/input.svelte b/test/sourcemaps/samples/sourcemap-basename-without-outputname/input.svelte index 4548af5548aa..189f28c390b9 100644 --- a/test/sourcemaps/samples/sourcemap-basename-without-outputname/input.svelte +++ b/test/sourcemaps/samples/sourcemap-basename-without-outputname/input.svelte @@ -1 +1,2 @@

sourcemap-basename-without-outputname

+