Skip to content

Commit d9f1522

Browse files
dummdidummbenmccanneltigerchino
authored
fix: adjust declaration map paths (#8843)
closes #7793 --------- Co-authored-by: Ben McCann <[email protected]> Co-authored-by: Tee Ming <[email protected]>
1 parent 0d813dc commit d9f1522

File tree

19 files changed

+120
-12
lines changed

19 files changed

+120
-12
lines changed

.changeset/dirty-doors-hide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/package': patch
3+
---
4+
5+
chore: bump `svelte2tsx` dependency for more up-to-date `d.ts` output

.changeset/pretty-lizards-attend.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/package': patch
3+
---
4+
5+
fix: adjust declaration map paths

documentation/docs/30-advanced/70-packaging.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,23 @@ You should think carefully about whether or not the changes you make to your pac
218218
}
219219
```
220220

221+
## Source maps
222+
223+
You can create so-called declaration maps (`d.ts.map` files) by setting `"declarationMap": true` in your `tsconfig.json`. This will allow editors such as VS Code to go to the original `.ts` or `.svelte` file when using features like _Go to Definition_. This means you also need to publish your source files alongside your dist folder in a way that the relative path inside the declaration files leads to a file on disk. Assuming that you have all your library code inside `src/lib` as suggested by Svelte's CLI, this is as simple as adding `src/lib` to `files` in your `package.json`:
224+
225+
```json
226+
{
227+
"files": [
228+
"dist",
229+
"!dist/**/*.test.*",
230+
"!dist/**/*.spec.*",
231+
+++"src/lib",
232+
"!src/lib/**/*.test.*",
233+
"!src/lib/**/*.spec.*"+++
234+
]
235+
}
236+
```
237+
221238
## Options
222239

223240
`svelte-package` accepts the following options:

packages/package/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"kleur": "^4.1.5",
2525
"sade": "^1.8.1",
2626
"semver": "^7.5.4",
27-
"svelte2tsx": "~0.7.16"
27+
"svelte2tsx": "~0.7.33"
2828
},
2929
"devDependencies": {
3030
"@sveltejs/vite-plugin-svelte": "^5.0.1",

packages/package/src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ async function do_build(options, analyse_code) {
3434
const files = scan(input, extensions);
3535

3636
if (options.types) {
37-
await emit_dts(input, temp, options.cwd, alias, files, tsconfig);
37+
await emit_dts(input, temp, output, options.cwd, alias, files, tsconfig);
3838
}
3939

4040
for (const file of files) {
@@ -137,7 +137,7 @@ export async function watch(options) {
137137

138138
if (!errored && options.types) {
139139
try {
140-
await emit_dts(input, output, options.cwd, alias, files, tsconfig);
140+
await emit_dts(input, output, output, options.cwd, alias, files, tsconfig);
141141
console.log('Updated .d.ts files');
142142
} catch (e) {
143143
errored = true;

packages/package/src/typescript.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ import { load_pkg_json } from './config.js';
1414
*
1515
* @param {string} input
1616
* @param {string} output
17+
* @param {string} final_output
1718
* @param {string} cwd
1819
* @param {Record<string, string>} alias
1920
* @param {import('./types.js').File[]} files
2021
* @param {string | undefined} tsconfig
2122
*/
22-
export async function emit_dts(input, output, cwd, alias, files, tsconfig) {
23+
export async function emit_dts(input, output, final_output, cwd, alias, files, tsconfig) {
2324
const tmp = `${output}/__package_types_tmp__`;
2425
rimraf(tmp);
2526
mkdirp(tmp);
@@ -54,8 +55,28 @@ export async function emit_dts(input, output, cwd, alias, files, tsconfig) {
5455
console.warn(`Using $lib/${normalized} instead of generated .d.ts file`);
5556
}
5657

57-
const source = fs.readFileSync(path.join(tmp, normalized), 'utf8');
58-
write(path.join(output, normalized), resolve_aliases(input, normalized, source, alias));
58+
let source = fs.readFileSync(path.join(tmp, normalized), 'utf8');
59+
if (file.endsWith('.d.ts.map')) {
60+
// Because we put the .d.ts files in a temporary directory, the relative path needs to be adjusted
61+
const parsed = JSON.parse(source);
62+
if (parsed.sources) {
63+
parsed.sources = /** @type {string[]} */ (parsed.sources).map((source) =>
64+
posixify(
65+
path.join(
66+
path.relative(
67+
path.dirname(path.join(final_output, normalized)),
68+
path.dirname(path.join(input, normalized))
69+
),
70+
path.basename(source)
71+
)
72+
)
73+
);
74+
source = JSON.stringify(parsed);
75+
}
76+
} else {
77+
source = resolve_aliases(input, normalized, source, alias);
78+
}
79+
write(path.join(output, normalized), source);
5980
}
6081

6182
rimraf(tmp);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script lang="ts">
2+
let { foo } = $props();
3+
</script>
4+
5+
{foo}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
type $$ComponentProps = {
2+
foo: string;
3+
};
4+
declare const Test: import("svelte").Component<$$ComponentProps, {}, "">;
5+
type Test = ReturnType<typeof Test>;
6+
export default Test
7+
//# sourceMappingURL=Test.svelte.d.ts.map

packages/package/test/fixtures/typescript-declaration-map/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/package/test/fixtures/typescript-declaration-map/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: 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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "typescript-declaration-map",
3+
"private": true,
4+
"version": "1.0.0",
5+
"description": "standard typescript package with declarationMap:true",
6+
"type": "module",
7+
"peerDependencies": {
8+
"svelte": "^5.0.0"
9+
},
10+
"exports": {
11+
".": {
12+
"types": "./dist/index.d.ts",
13+
"svelte": "./dist/index.js"
14+
}
15+
}
16+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script lang="ts">
2+
let { foo }: { foo: string } = $props();
3+
</script>
4+
5+
{foo}
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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import preprocess from 'svelte-preprocess';
2+
3+
export default {
4+
preprocess: preprocess()
5+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ESNext",
4+
"module": "ESNext",
5+
"declaration": true,
6+
"declarationMap": true
7+
}
8+
}

packages/package/test/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ async function test_make_package(path, options) {
4242

4343
assert.equal(actual_files, expected_files);
4444

45-
const extensions = ['.json', '.svelte', '.ts', 'js'];
45+
const extensions = ['.json', '.svelte', '.ts', 'js', '.map'];
4646
for (const file of actual_files) {
4747
const pathname = join(output, file);
4848
if (fs.statSync(pathname).isDirectory()) continue;
@@ -68,6 +68,10 @@ async function test_make_package(path, options) {
6868
* @param {string} content
6969
*/
7070
async function format(file, content) {
71+
if (file.endsWith('.map')) {
72+
return content;
73+
}
74+
7175
if (file.endsWith('package.json')) {
7276
// For some reason these are ordered differently in different test environments
7377
const json = JSON.parse(content);
@@ -152,6 +156,10 @@ test('SvelteKit interop', async () => {
152156
await test_make_package('svelte-kit');
153157
});
154158

159+
test('create package with declaration map', async () => {
160+
await test_make_package('typescript-declaration-map');
161+
});
162+
155163
test('create package with tsconfig specified', async () => {
156164
await test_make_package('tsconfig-specified', { tsconfig: 'tsconfig.build.json' });
157165
});

pnpm-lock.yaml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)