Skip to content

Commit a93a39f

Browse files
authored
feat: provide SvelteKit html typings (#11222)
This makes it possible to delete these from svelte/elements in Svelte 5 and have them controled in SvelteKit, decoupling the two closes #10534
1 parent aedb572 commit a93a39f

File tree

5 files changed

+52
-1
lines changed

5 files changed

+52
-1
lines changed

.changeset/smart-buttons-return.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': minor
3+
---
4+
5+
feat: provide SvelteKit html typings

packages/kit/src/core/sync/sync.js

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { write_root } from './write_root.js';
55
import { write_tsconfig } from './write_tsconfig.js';
66
import { write_types, write_all_types } from './write_types/index.js';
77
import { write_ambient } from './write_ambient.js';
8+
import { write_non_ambient } from './write_non_ambient.js';
89
import { write_server } from './write_server.js';
910

1011
/**
@@ -15,6 +16,7 @@ import { write_server } from './write_server.js';
1516
export function init(config, mode) {
1617
write_tsconfig(config.kit);
1718
write_ambient(config.kit, mode);
19+
write_non_ambient(config.kit);
1820
}
1921

2022
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import path from 'node:path';
2+
import { GENERATED_COMMENT } from '../../constants.js';
3+
import { write_if_changed } from './utils.js';
4+
5+
// `declare module "svelte/elements"` needs to happen in a non-ambient module, and dts-buddy generates one big ambient module,
6+
// so we can't add it there - therefore generate the typings ourselves here.
7+
// We're not using the `declare namespace svelteHTML` variant because that one doesn't augment the HTMLAttributes interface
8+
// people could use to type their own components.
9+
// The T generic is needed or else there's a "all declarations must have identical type parameters" error.
10+
const template = `
11+
${GENERATED_COMMENT}
12+
13+
declare module "svelte/elements" {
14+
export interface HTMLAttributes<T> {
15+
'data-sveltekit-keepfocus'?: true | '' | 'off' | undefined | null;
16+
'data-sveltekit-noscroll'?: true | '' | 'off' | undefined | null;
17+
'data-sveltekit-preload-code'?:
18+
| true
19+
| ''
20+
| 'eager'
21+
| 'viewport'
22+
| 'hover'
23+
| 'tap'
24+
| 'off'
25+
| undefined
26+
| null;
27+
'data-sveltekit-preload-data'?: true | '' | 'hover' | 'tap' | 'off' | undefined | null;
28+
'data-sveltekit-reload'?: true | '' | 'off' | undefined | null;
29+
'data-sveltekit-replacestate'?: true | '' | 'off' | undefined | null;
30+
}
31+
}
32+
33+
export {};
34+
`;
35+
36+
/**
37+
* Writes non-ambient declarations to the output directory
38+
* @param {import('types').ValidatedKitConfig} config
39+
*/
40+
export function write_non_ambient(config) {
41+
write_if_changed(path.join(config.outDir, 'non-ambient.d.ts'), template);
42+
}

packages/kit/src/core/sync/write_tsconfig.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export function get_tsconfig(kit, include_base_url) {
8686

8787
const include = new Set([
8888
'ambient.d.ts',
89+
'non-ambient.d.ts',
8990
'./types/**/$types.d.ts',
9091
config_relative('vite.config.js'),
9192
config_relative('vite.config.ts')
@@ -109,7 +110,7 @@ export function get_tsconfig(kit, include_base_url) {
109110
include.add(config_relative(`${test_folder}/**/*.ts`));
110111
include.add(config_relative(`${test_folder}/**/*.svelte`));
111112

112-
const exclude = [config_relative('node_modules/**'), './[!ambient.d.ts]**'];
113+
const exclude = [config_relative('node_modules/**')];
113114
if (path.extname(kit.files.serviceWorker)) {
114115
exclude.push(config_relative(kit.files.serviceWorker));
115116
} else {

packages/kit/src/core/sync/write_tsconfig.spec.js

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ test('Creates tsconfig include from kit.files', () => {
100100

101101
expect(include).toEqual([
102102
'ambient.d.ts',
103+
'non-ambient.d.ts',
103104
'./types/**/$types.d.ts',
104105
'../vite.config.js',
105106
'../vite.config.ts',

0 commit comments

Comments
 (0)