Skip to content

Commit 7d0909f

Browse files
authored
(fix) adjust sourcemap paths (#1851)
- .svelte.ts -> .svelte - remove path prefix
1 parent 941921e commit 7d0909f

File tree

9 files changed

+90
-8
lines changed

9 files changed

+90
-8
lines changed

packages/svelte2tsx/src/emitDts.ts

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ async function createTsCompilerHost(options: any, svelteMap: SvelteMap) {
9292
// TypeScript writes the files relative to the found tsconfig/jsconfig
9393
// which - at least in the case of the tests - is wrong. Therefore prefix
9494
// the output paths. See Typescript issue #25430 for more.
95-
const pathPrefix = path.relative(process.cwd(), path.dirname(options.configFilePath));
95+
const pathPrefix = path
96+
.relative(process.cwd(), path.dirname(options.configFilePath))
97+
.split(path.sep)
98+
.join('/');
9699

97100
const svelteSys: ts.System = {
98101
...ts.sys,
@@ -123,11 +126,37 @@ async function createTsCompilerHost(options: any, svelteMap: SvelteMap) {
123126
return ts.sys.readDirectory(path, extensionsWithSvelte, exclude, include, depth);
124127
},
125128
writeFile(fileName, data, writeByteOrderMark) {
126-
return ts.sys.writeFile(
127-
pathPrefix ? path.join(pathPrefix, fileName) : fileName,
128-
data,
129-
writeByteOrderMark
130-
);
129+
fileName = pathPrefix ? path.join(pathPrefix, fileName) : fileName;
130+
if (fileName.endsWith('d.ts.map')) {
131+
data = data.replace(/"sources":\["(.+?)"\]/, (_, sourcePath: string) => {
132+
// Due to our hack of treating .svelte files as .ts files, we need to adjust the extension
133+
if (sourcePath.endsWith('.svelte.ts')) {
134+
sourcePath = sourcePath.slice(0, -3);
135+
}
136+
// The inverse of the pathPrefix adjustment
137+
sourcePath =
138+
pathPrefix && sourcePath.includes(pathPrefix)
139+
? sourcePath.slice(0, sourcePath.indexOf(pathPrefix)) +
140+
sourcePath.slice(
141+
sourcePath.indexOf(pathPrefix) + pathPrefix.length + 1
142+
)
143+
: sourcePath;
144+
return `"sources":["${sourcePath}"]`;
145+
});
146+
} else if (fileName.endsWith('js.map')) {
147+
data = data.replace(/"sources":\["(.+?)"\]/, (_, sourcePath: string) => {
148+
// The inverse of the pathPrefix adjustment
149+
sourcePath =
150+
pathPrefix && sourcePath.includes(pathPrefix)
151+
? sourcePath.slice(0, sourcePath.indexOf(pathPrefix)) +
152+
sourcePath.slice(
153+
sourcePath.indexOf(pathPrefix) + pathPrefix.length + 1
154+
)
155+
: sourcePath;
156+
return `"sources":["${sourcePath}"]`;
157+
});
158+
}
159+
return ts.sys.writeFile(fileName, data, writeByteOrderMark);
131160
}
132161
};
133162

packages/svelte2tsx/test/emitDts/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ async function testEmitDts(sample: string) {
1818
declarationDir: 'package',
1919
svelteShimsPath: require.resolve(join(process.cwd(), 'svelte-shims.d.ts')),
2020
...config,
21-
libRoot: config.libRoot ? join(cwd, config.libRoot) : cwd
21+
libRoot: config.libRoot ? join(cwd, config.libRoot) : join(cwd, 'src')
2222
});
2323
const expectedFiles = fs.readdirSync(join(cwd, 'expected'));
2424
const actual_files = fs.readdirSync(join(cwd, 'package'));
@@ -59,6 +59,6 @@ describe('emitDts', async () => {
5959
let samplesToTest = samples.filter((s) => s.endsWith('.solo'));
6060
samplesToTest = samplesToTest.length ? samplesToTest : samples;
6161
for (const sample of samplesToTest) {
62-
it(sample, async () => await testEmitDts(sample)).timeout(5000);
62+
it(sample, async () => await testEmitDts(sample)).timeout(10000);
6363
}
6464
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { SvelteComponentTyped } from "svelte";
2+
declare const __propDef: {
3+
props: {
4+
astring: string;
5+
};
6+
events: {
7+
event: CustomEvent<any>;
8+
} & {
9+
[evt: string]: CustomEvent<any>;
10+
};
11+
slots: {
12+
default: {
13+
astring: string;
14+
};
15+
};
16+
};
17+
export type TestProps = typeof __propDef.props;
18+
export type TestEvents = typeof __propDef.events;
19+
export type TestSlots = typeof __propDef.slots;
20+
export default class Test extends SvelteComponentTyped<TestProps, TestEvents, TestSlots> {
21+
get astring(): string;
22+
}
23+
export {};
24+
//# sourceMappingURL=Test.svelte.d.ts.map

packages/svelte2tsx/test/emitDts/samples/typescript-declarationMap/expected/Test.svelte.d.ts.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { default as Test } from './Test.svelte';
2+
//# sourceMappingURL=index.d.ts.map

packages/svelte2tsx/test/emitDts/samples/typescript-declarationMap/expected/index.d.ts.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script lang="ts">
2+
import { createEventDispatcher } from 'svelte';
3+
export const astring: string;
4+
5+
const dispatch = createEventDispatcher();
6+
dispatch('event', true);
7+
</script>
8+
9+
<slot {astring} />
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default as Test } from './Test.svelte';
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"moduleResolution": "node",
4+
"module": "es2020",
5+
"lib": ["es2020", "DOM"],
6+
"target": "es2019",
7+
"isolatedModules": true,
8+
"resolveJsonModule": true,
9+
"esModuleInterop": true,
10+
"allowJs": true,
11+
"checkJs": true,
12+
"declarationMap": true
13+
},
14+
"include": ["./src/**/*.d.ts", "./src/**/*.js", "./src/**/*.ts", "./src/**/*.svelte"]
15+
}

0 commit comments

Comments
 (0)