Skip to content

Commit 06cf3cd

Browse files
authored
feat: use Svelte 4 typings when packaging if dependencies allow it (#10328)
When the (peer)dependency states that only Svelte 4 is allowed, invoke the dts generation script with the Svelte 4 shims instead of the Svelte 3 shims. The noticable difference is that then SvelteComponent instead of the deprecated SvelteComponentTyped will be used to declare the component typings. closes #10223
1 parent 486a971 commit 06cf3cd

File tree

24 files changed

+141
-56
lines changed

24 files changed

+141
-56
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/package': minor
3+
---
4+
5+
feat: use Svelte 4 typings when packaging if dependencies allow it

packages/package/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
"chokidar": "^3.5.3",
1515
"kleur": "^4.1.5",
1616
"sade": "^1.8.1",
17-
"svelte2tsx": "~0.6.16"
17+
"semver": "^7.5.3",
18+
"svelte2tsx": "~0.6.19"
1819
},
1920
"devDependencies": {
2021
"@types/node": "^16.18.6",
22+
"@types/semver": "^7.5.0",
2123
"svelte": "^4.0.3",
2224
"svelte-preprocess": "^5.0.4",
2325
"typescript": "^4.9.4",

packages/package/src/config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,17 @@ export async function load_config({ cwd = process.cwd() } = {}) {
2525

2626
return config;
2727
}
28+
29+
/**
30+
* @param {string} cwd
31+
* @returns Record<string, any>
32+
*/
33+
export function load_pkg_json(cwd = process.cwd()) {
34+
const pkg_json_file = path.join(cwd, 'package.json');
35+
36+
if (!fs.existsSync(pkg_json_file)) {
37+
return {};
38+
}
39+
40+
return JSON.parse(fs.readFileSync(pkg_json_file, 'utf-8'));
41+
}

packages/package/src/typescript.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import * as fs from 'node:fs';
22
import * as path from 'node:path';
33
import { createRequire } from 'node:module';
4+
import semver from 'semver';
45
import { posixify, mkdirp, rimraf, walk } from './filesystem.js';
56
import { resolve_aliases, write } from './utils.js';
67
import { emitDts } from 'svelte2tsx';
8+
import { load_pkg_json } from './config.js';
79

810
/**
911
* Generates d.ts files by invoking TypeScript's "emit d.ts files from input files".
@@ -22,9 +24,14 @@ export async function emit_dts(input, output, cwd, alias, files) {
2224
mkdirp(tmp);
2325

2426
const require = createRequire(import.meta.url);
27+
const pkg = load_pkg_json(cwd);
28+
const svelte_dep = pkg.peerDependencies?.svelte || pkg.dependencies?.svelte || '3.0';
29+
const no_svelte_3 = !semver.intersects(svelte_dep, '^3.0.0');
2530
await emitDts({
2631
libRoot: input,
27-
svelteShimsPath: require.resolve('svelte2tsx/svelte-shims.d.ts'),
32+
svelteShimsPath: no_svelte_3
33+
? require.resolve('svelte2tsx/svelte-shims-v4.d.ts')
34+
: require.resolve('svelte2tsx/svelte-shims.d.ts'),
2835
declarationDir: path.relative(cwd, tmp)
2936
});
3037

packages/package/test/fixtures/javascript/expected/Test.svelte.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/** @typedef {typeof __propDef.props} TestProps */
22
/** @typedef {typeof __propDef.events} TestEvents */
33
/** @typedef {typeof __propDef.slots} TestSlots */
4-
export default class Test extends SvelteComponentTyped<
4+
export default class Test extends SvelteComponent<
55
{
66
astring?: string;
77
},
@@ -21,7 +21,7 @@ export default class Test extends SvelteComponentTyped<
2121
export type TestProps = typeof __propDef.props;
2222
export type TestEvents = typeof __propDef.events;
2323
export type TestSlots = typeof __propDef.slots;
24-
import { SvelteComponentTyped } from 'svelte';
24+
import { SvelteComponent } from 'svelte';
2525
declare const __propDef: {
2626
props: {
2727
astring?: string;

packages/package/test/fixtures/javascript/expected/Test2.svelte.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/** @typedef {typeof __propDef.props} Test2Props */
22
/** @typedef {typeof __propDef.events} Test2Events */
33
/** @typedef {typeof __propDef.slots} Test2Slots */
4-
export default class Test2 extends SvelteComponentTyped<
4+
export default class Test2 extends SvelteComponent<
55
{
66
foo: boolean;
77
},
@@ -13,7 +13,7 @@ export default class Test2 extends SvelteComponentTyped<
1313
export type Test2Props = typeof __propDef.props;
1414
export type Test2Events = typeof __propDef.events;
1515
export type Test2Slots = typeof __propDef.slots;
16-
import { SvelteComponentTyped } from 'svelte';
16+
import { SvelteComponent } from 'svelte';
1717
declare const __propDef: {
1818
props: {
1919
foo: import('./foo').Foo;

packages/package/test/fixtures/javascript/expected/internal/Test.svelte.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/** @typedef {typeof __propDef.props} TestProps */
22
/** @typedef {typeof __propDef.events} TestEvents */
33
/** @typedef {typeof __propDef.slots} TestSlots */
4-
export default class Test extends SvelteComponentTyped<
4+
export default class Test extends SvelteComponent<
55
{
66
foo: boolean;
77
},
@@ -13,7 +13,7 @@ export default class Test extends SvelteComponentTyped<
1313
export type TestProps = typeof __propDef.props;
1414
export type TestEvents = typeof __propDef.events;
1515
export type TestSlots = typeof __propDef.slots;
16-
import { SvelteComponentTyped } from 'svelte';
16+
import { SvelteComponent } from 'svelte';
1717
declare const __propDef: {
1818
props: {
1919
foo: import('./foo').Foo;

packages/package/test/fixtures/resolve-alias/expected/Test.svelte.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SvelteComponentTyped } from 'svelte';
1+
import { SvelteComponent } from 'svelte';
22
declare const __propDef: {
33
props: {
44
bar?: import('./sub/foo').Foo;
@@ -11,5 +11,5 @@ declare const __propDef: {
1111
export type TestProps = typeof __propDef.props;
1212
export type TestEvents = typeof __propDef.events;
1313
export type TestSlots = typeof __propDef.slots;
14-
export default class Test extends SvelteComponentTyped<TestProps, TestEvents, TestSlots> {}
14+
export default class Test extends SvelteComponent<TestProps, TestEvents, TestSlots> {}
1515
export {};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script>
2+
import { createEventDispatcher } from 'svelte';
3+
export const astring = 'potato';
4+
const dispatch = createEventDispatcher();
5+
dispatch('event', true);
6+
</script>
7+
8+
<slot {astring} />
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { SvelteComponentTyped } from 'svelte';
2+
declare const __propDef: {
3+
props: {
4+
astring?: string;
5+
};
6+
events: {
7+
event: CustomEvent<boolean>;
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 {};

0 commit comments

Comments
 (0)