Skip to content

Commit 716e59d

Browse files
committed
fix: convert paths to relative for vitePreprocess sourcemap sources
1 parent bc7f03f commit 716e59d

File tree

4 files changed

+38
-33
lines changed

4 files changed

+38
-33
lines changed

packages/e2e-tests/import-queries/__tests__/__snapshots__/import-queries.spec.ts.snap

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -560,10 +560,9 @@ exports[`raw > Dummy.svelte?raw&svelte&type=all&sourcemap 1`] = `
560560
\\"dependencies\\": [],
561561
\\"map\\": {
562562
\\"version\\": 3,
563-
\\"mappings\\": \\"AAAA,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CCCT,MAAA,CAAA,GAAA,CAAI,IAAA;AACX,GAAA,CAAI,MAAA,CAAA,CAAA,CAAS,CAAA;ADCd,CAAC,CAAC,MAAM;;AAER,CAAC;AACD,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACjB,EAAE,MAAM,CAAC,CAAC;AACV,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC;AAC7B;;AAEA,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC;;CAKlB,CAAC,CAAC,KAAK;\\",
563+
\\"mappings\\": \\"AAAA,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CACT,MAAA,CAAA,GAAA,CAAI,IAAA;AACX,GAAA,CAAI,MAAA,CAAA,CAAA,CAAS,CAAA;AACd,CAAC,CAAC,MAAM;;AAER,CAAC;AACD,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACjB,EAAE,MAAM,CAAC,CAAC;AACV,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC;AAC7B;;AAEA,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC;;CAKlB,CAAC,CAAC,KAAK;\\",
564564
\\"names\\": [],
565565
\\"sources\\": [
566-
\\"Dummy.svelte\\",
567566
\\"Dummy.svelte\\"
568567
]
569568
}
@@ -840,7 +839,7 @@ export const dependencies=[]
840839
export const js={\\"code\\":\\"/* src/Dummy.svelte generated by Svelte vXXX */\\\\nimport {\\\\n\\\\tSvelteComponent as SvelteComponent$,\\\\n\\\\tappend as append$,\\\\n\\\\tattr as attr$,\\\\n\\\\tdetach as detach$,\\\\n\\\\telement as element$,\\\\n\\\\tinit as init$,\\\\n\\\\tinsert as insert$,\\\\n\\\\tlisten as listen$,\\\\n\\\\tnoop as noop$,\\\\n\\\\tsafe_not_equal as safe_not_equal$,\\\\n\\\\tset_data as set_data$,\\\\n\\\\ttext as text$\\\\n} from \\\\\\"svelte/internal\\\\\\";\\\\n\\\\nfunction create_fragment(ctx) {\\\\n\\\\tlet button$;\\\\n\\\\tlet t0$;\\\\n\\\\tlet t1$;\\\\n\\\\tlet t2$;\\\\n\\\\tlet mounted;\\\\n\\\\tlet dispose;\\\\n\\\\n\\\\treturn {\\\\n\\\\t\\\\tc() {\\\\n\\\\t\\\\t\\\\tbutton$ = element$(\\\\\\"button\\\\\\");\\\\n\\\\t\\\\t\\\\tt0$ = text$(/*name*/ ctx[0]);\\\\n\\\\t\\\\t\\\\tt1$ = text$(\\\\\\" clicks: \\\\\\");\\\\n\\\\t\\\\t\\\\tt2$ = text$(/*clicks*/ ctx[1]);\\\\n\\\\t\\\\t\\\\tattr$(button$, \\\\\\"class\\\\\\", \\\\\\"svelte-d8vj6a\\\\\\");\\\\n\\\\t\\\\t},\\\\n\\\\t\\\\tm(target, anchor) {\\\\n\\\\t\\\\t\\\\tinsert$(target, button$, anchor);\\\\n\\\\t\\\\t\\\\tappend$(button$, t0$);\\\\n\\\\t\\\\t\\\\tappend$(button$, t1$);\\\\n\\\\t\\\\t\\\\tappend$(button$, t2$);\\\\n\\\\n\\\\t\\\\t\\\\tif (!mounted) {\\\\n\\\\t\\\\t\\\\t\\\\tdispose = listen$(button$, \\\\\\"click\\\\\\", /*click_handler$*/ ctx[2]);\\\\n\\\\t\\\\t\\\\t\\\\tmounted = true;\\\\n\\\\t\\\\t\\\\t}\\\\n\\\\t\\\\t},\\\\n\\\\t\\\\tp(ctx, [dirty]) {\\\\n\\\\t\\\\t\\\\tif (dirty & /*name*/ 1) set_data$(t0$, /*name*/ ctx[0]);\\\\n\\\\t\\\\t\\\\tif (dirty & /*clicks*/ 2) set_data$(t2$, /*clicks*/ ctx[1]);\\\\n\\\\t\\\\t},\\\\n\\\\t\\\\ti: noop$,\\\\n\\\\t\\\\to: noop$,\\\\n\\\\t\\\\td(detaching) {\\\\n\\\\t\\\\t\\\\tif (detaching) detach$(button$);\\\\n\\\\t\\\\t\\\\tmounted = false;\\\\n\\\\t\\\\t\\\\tdispose();\\\\n\\\\t\\\\t}\\\\n\\\\t};\\\\n}\\\\n\\\\nfunction instance$($$self, $$props, $$invalidate) {\\\\n\\\\tlet { name } = $$props;\\\\n\\\\tlet clicks = 0;\\\\n\\\\n\\\\tconst click_handler$ = () => {\\\\n\\\\t\\\\t$$invalidate(1, clicks++, clicks);\\\\n\\\\t};\\\\n\\\\n\\\\t$$self.$$set = $$props => {\\\\n\\\\t\\\\tif ('name' in $$props) $$invalidate(0, name = $$props.name);\\\\n\\\\t};\\\\n\\\\n\\\\treturn [name, clicks, click_handler$];\\\\n}\\\\n\\\\nclass Dummy$ extends SvelteComponent$ {\\\\n\\\\tconstructor(options) {\\\\n\\\\t\\\\tsuper();\\\\n\\\\t\\\\tinit$(this, options, instance$, create_fragment, safe_not_equal$, { name: 0 });\\\\n\\\\t}\\\\n}\\\\n\\\\nexport default Dummy$;\\",\\"map\\":null,\\"dependencies\\":[]}
841840
export const lang=\\"ts\\"
842841
export const normalizedFilename=\\"/src/Dummy.svelte\\"
843-
export const preprocessed={\\"code\\":\\"<script lang=\\\\\\"ts\\\\\\">export let name;\\\\nlet clicks = 0;\\\\n</script>\\\\n\\\\n<button\\\\n\\\\ton:click={() => {\\\\n\\\\t\\\\tclicks++;\\\\n\\\\t}}>{name} clicks: {clicks}</button\\\\n>\\\\n\\\\n<style lang=\\\\\\"scss\\\\\\">button {\\\\n color: #000099;\\\\n}</style>\\\\n\\",\\"dependencies\\":[],\\"map\\":{\\"version\\":3,\\"mappings\\":\\"AAAA,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CCCT,MAAA,CAAA,GAAA,CAAI,IAAA;AACX,GAAA,CAAI,MAAA,CAAA,CAAA,CAAS,CAAA;ADCd,CAAC,CAAC,MAAM;;AAER,CAAC;AACD,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACjB,EAAE,MAAM,CAAC,CAAC;AACV,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC;AAC7B;;AAEA,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC;;CAKlB,CAAC,CAAC,KAAK;\\",\\"names\\":[],\\"sources\\":[\\"Dummy.svelte\\",\\"Dummy.svelte\\"]}}
842+
export const preprocessed={\\"code\\":\\"<script lang=\\\\\\"ts\\\\\\">export let name;\\\\nlet clicks = 0;\\\\n</script>\\\\n\\\\n<button\\\\n\\\\ton:click={() => {\\\\n\\\\t\\\\tclicks++;\\\\n\\\\t}}>{name} clicks: {clicks}</button\\\\n>\\\\n\\\\n<style lang=\\\\\\"scss\\\\\\">button {\\\\n color: #000099;\\\\n}</style>\\\\n\\",\\"dependencies\\":[],\\"map\\":{\\"version\\":3,\\"mappings\\":\\"AAAA,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CACT,MAAA,CAAA,GAAA,CAAI,IAAA;AACX,GAAA,CAAI,MAAA,CAAA,CAAA,CAAS,CAAA;AACd,CAAC,CAAC,MAAM;;AAER,CAAC;AACD,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACjB,EAAE,MAAM,CAAC,CAAC;AACV,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC;AAC7B;;AAEA,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC;;CAKlB,CAAC,CAAC,KAAK;\\",\\"names\\":[],\\"sources\\":[\\"Dummy.svelte\\"]}}
844843
export const source=\\"<script lang=\\\\\\"ts\\\\\\">\\\\n\\\\texport let name: string;\\\\n\\\\tlet clicks = 0;\\\\n</script>\\\\n\\\\n<button\\\\n\\\\ton:click={() => {\\\\n\\\\t\\\\tclicks++;\\\\n\\\\t}}>{name} clicks: {clicks}</button\\\\n>\\\\n\\\\n<style lang=\\\\\\"scss\\\\\\">\\\\n\\\\t$blue: blue;\\\\n\\\\tbutton {\\\\n\\\\t\\\\tcolor: darken($blue, 20);\\\\n\\\\t}\\\\n</style>\\\\n\\"
845844
export const ssr=false
846845
export const stats={\\"timings\\":{\\"total\\":0.123456789,\\"parse\\":{\\"total\\":0.123456789},\\"create component\\":{\\"total\\":0.123456789}}}
@@ -852,7 +851,7 @@ export const warnings=[]
852851
exports[`raw > mixed exports > Dummy.svelte?raw&svelte&type=preprocessed 1`] = `
853852
"export const code=\\"<script lang=\\\\\\"ts\\\\\\">export let name;\\\\nlet clicks = 0;\\\\n</script>\\\\n\\\\n<button\\\\n\\\\ton:click={() => {\\\\n\\\\t\\\\tclicks++;\\\\n\\\\t}}>{name} clicks: {clicks}</button\\\\n>\\\\n\\\\n<style lang=\\\\\\"scss\\\\\\">button {\\\\n color: #000099;\\\\n}</style>\\\\n\\"
854853
export const dependencies=[]
855-
export const map={\\"version\\":3,\\"mappings\\":\\"AAAA,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CCCT,MAAA,CAAA,GAAA,CAAI,IAAA;AACX,GAAA,CAAI,MAAA,CAAA,CAAA,CAAS,CAAA;ADCd,CAAC,CAAC,MAAM;;AAER,CAAC;AACD,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACjB,EAAE,MAAM,CAAC,CAAC;AACV,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC;AAC7B;;AAEA,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC;;CAKlB,CAAC,CAAC,KAAK;\\",\\"names\\":[],\\"sources\\":[\\"Dummy.svelte\\",\\"Dummy.svelte\\"]}
854+
export const map={\\"version\\":3,\\"mappings\\":\\"AAAA,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CACT,MAAA,CAAA,GAAA,CAAI,IAAA;AACX,GAAA,CAAI,MAAA,CAAA,CAAA,CAAS,CAAA;AACd,CAAC,CAAC,MAAM;;AAER,CAAC;AACD,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACjB,EAAE,MAAM,CAAC,CAAC;AACV,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC;AAC7B;;AAEA,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC;;CAKlB,CAAC,CAAC,KAAK;\\",\\"names\\":[],\\"sources\\":[\\"Dummy.svelte\\"]}
856855
export default code
857856
"
858857
`;

packages/vite-plugin-svelte/src/preprocess.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import path from 'path';
21
import { preprocessCSS, resolveConfig, transformWithEsbuild } from 'vite';
32
import type { ESBuildOptions, InlineConfig, ResolvedConfig } from 'vite';
43
// eslint-disable-next-line node/no-missing-import
@@ -7,6 +6,8 @@ import type { Preprocessor, PreprocessorGroup } from 'svelte/types/compiler/prep
76
const supportedStyleLangs = ['css', 'less', 'sass', 'scss', 'styl', 'stylus', 'postcss', 'sss'];
87
const supportedScriptLangs = ['ts'];
98

9+
import { mapSourcesToRelative } from './utils/sourcemaps';
10+
1011
export function vitePreprocess(opts?: {
1112
script?: boolean;
1213
style?: boolean | InlineConfig | ResolvedConfig;
@@ -27,7 +28,7 @@ function viteScript(): { script: Preprocessor } {
2728
async script({ attributes, content, filename = '' }) {
2829
const lang = attributes.lang as string;
2930
if (!supportedScriptLangs.includes(lang)) return;
30-
const transformResult = await transformWithEsbuild(content, filename, {
31+
const { code, map } = await transformWithEsbuild(content, filename, {
3132
loader: lang as ESBuildOptions['loader'],
3233
target: 'esnext',
3334
tsconfigRaw: {
@@ -38,9 +39,12 @@ function viteScript(): { script: Preprocessor } {
3839
}
3940
}
4041
});
42+
43+
mapSourcesToRelative(map, filename);
44+
4145
return {
42-
code: transformResult.code,
43-
map: transformResult.map
46+
code,
47+
map
4448
};
4549
}
4650
};
@@ -70,14 +74,13 @@ function viteStyle(config: InlineConfig | ResolvedConfig = {}): {
7074
transform = getCssTransformFn(resolvedConfig);
7175
}
7276
const moduleId = `${filename}.${lang}`;
73-
const result = await transform(content, moduleId);
74-
// patch sourcemap source to point back to original filename
75-
if (result.map?.sources?.[0] === moduleId) {
76-
result.map.sources[0] = path.basename(filename);
77-
}
77+
const { code, map } = await transform(content, moduleId);
78+
79+
mapSourcesToRelative(map, moduleId);
80+
7881
return {
79-
code: result.code,
80-
map: result.map ?? undefined
82+
code,
83+
map: map ?? undefined
8184
};
8285
};
8386
// @ts-expect-error tag so can be found by v-p-s

packages/vite-plugin-svelte/src/utils/compile.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,12 @@ import { StatCollection } from './vite-plugin-svelte-stats';
99
//eslint-disable-next-line node/no-missing-import
1010
import type { Processed } from 'svelte/types/compiler/preprocess';
1111
import { createInjectScopeEverythingRulePreprocessorGroup } from './preprocess';
12-
import path from 'path';
12+
import { mapSourcesToRelative } from './sourcemaps';
1313

1414
const scriptLangRE = /<script [^>]*lang=["']?([^"' >]+)["']?[^>]*>/;
1515

1616
export type CompileSvelte = ReturnType<typeof _createCompileSvelte>;
1717

18-
function mapSourcesToRelative(map: { sources?: string[] }, filename: string) {
19-
// sourcemap sources are relative to the sourcemap itself
20-
// assume the sourcemap location is the same as filename and turn absolute paths to relative
21-
// to avoid leaking fs information like vite root
22-
if (map?.sources) {
23-
map.sources = map.sources.map((s) => {
24-
if (path.isAbsolute(s)) {
25-
const relative = path.relative(filename, s);
26-
// empty string a source is not allowed, use simple filename
27-
return relative === '' ? path.basename(filename) : relative;
28-
} else {
29-
return s;
30-
}
31-
});
32-
}
33-
}
34-
3518
const _createCompileSvelte = (makeHot: Function) => {
3619
let stats: StatCollection | undefined;
3720
const devStylePreprocessor = createInjectScopeEverythingRulePreprocessorGroup();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import path from 'path';
2+
3+
/**
4+
* sourcemap sources are relative to the sourcemap itself
5+
* assume the sourcemap location is the same as filename and turn absolute paths to relative
6+
* to avoid leaking fs information like vite root
7+
*/
8+
export function mapSourcesToRelative(map: { sources?: string[] }, filename: string) {
9+
if (map?.sources) {
10+
map.sources = map.sources.map((s) => {
11+
if (path.isAbsolute(s)) {
12+
const relative = path.relative(filename, s);
13+
// empty string as a source is not allowed, use simple filename
14+
return relative === '' ? path.basename(filename) : relative;
15+
} else {
16+
return s;
17+
}
18+
});
19+
}
20+
}

0 commit comments

Comments
 (0)