Skip to content

Commit 2137717

Browse files
fix: remove internal __sveltekit/ module declarations from types (#11620)
* fix: remove internal `__sveltekit/` module declarations from types Takes advantage of the fact that dts-buddy doesn't detect the ambient-private.d.ts module declarations (which arguably is a bit weird and could result in buggy behavior, but we can use it to our advantage here). fixes #11607 * lint * re-export values from internal modules * move files * point dts-buddy at facade .d.ts files * remove some junk we no longer need --------- Co-authored-by: Rich Harris <[email protected]>
1 parent 067d369 commit 2137717

File tree

13 files changed

+132
-142
lines changed

13 files changed

+132
-142
lines changed

.changeset/brave-cats-tan.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@sveltejs/kit": patch
3+
---
4+
5+
fix: remove internal `__sveltekit/` module declarations from types

packages/kit/scripts/generate-dts.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
import { createBundle } from 'dts-buddy';
2+
import { readFileSync } from 'node:fs';
23

3-
createBundle({
4+
await createBundle({
45
output: 'types/index.d.ts',
56
modules: {
67
'@sveltejs/kit': 'src/exports/public.d.ts',
78
'@sveltejs/kit/hooks': 'src/exports/hooks/index.js',
89
'@sveltejs/kit/node': 'src/exports/node/index.js',
910
'@sveltejs/kit/node/polyfills': 'src/exports/node/polyfills.js',
1011
'@sveltejs/kit/vite': 'src/exports/vite/index.js',
11-
'$app/environment': 'src/runtime/app/environment.js',
12+
'$app/environment': 'src/runtime/app/environment/types.d.ts',
1213
'$app/forms': 'src/runtime/app/forms.js',
1314
'$app/navigation': 'src/runtime/app/navigation.js',
14-
'$app/paths': 'src/runtime/app/paths.js',
15+
'$app/paths': 'src/runtime/app/paths/types.d.ts',
1516
'$app/stores': 'src/runtime/app/stores.js'
1617
},
1718
include: ['src']
1819
});
20+
21+
// dts-buddy doesn't inline imports of module declaration in ambient-private.d.ts but also doesn't include them, resulting in broken types - guard against that
22+
const types = readFileSync('./types/index.d.ts', 'utf-8');
23+
if (types.includes('__sveltekit/')) {
24+
throw new Error(
25+
'Found __sveltekit/ in types/index.d.ts - make sure to hide internal modules by not just reexporting them. Contents:\n\n' +
26+
types
27+
);
28+
}

packages/kit/src/runtime/app/environment.js

-12
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { BROWSER, DEV } from 'esm-env';
2+
export { building, version } from '__sveltekit/environment';
3+
4+
export const browser = BROWSER;
5+
6+
export const dev = DEV;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* `true` if the app is running in the browser.
3+
*/
4+
export const browser: boolean;
5+
6+
/**
7+
* Whether the dev server is running. This is not guaranteed to correspond to `NODE_ENV` or `MODE`.
8+
*/
9+
export const dev: boolean;
10+
11+
/**
12+
* SvelteKit analyses your app during the `build` step by running it. During this process, `building` is `true`. This also applies during prerendering.
13+
*/
14+
export const building: boolean;
15+
16+
/**
17+
* The value of `config.kit.version.name`.
18+
*/
19+
export const version: string;

packages/kit/src/runtime/app/paths.js

-23
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export { base, assets } from '__sveltekit/paths';
2+
import { base } from '__sveltekit/paths';
3+
import { resolve_route } from '../../../utils/routing.js';
4+
5+
/** @type {import('./types.d.ts').resolveRoute} */
6+
export function resolveRoute(id, params) {
7+
return base + resolve_route(id, params);
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* A string that matches [`config.kit.paths.base`](https://kit.svelte.dev/docs/configuration#paths).
3+
*
4+
* Example usage: `<a href="{base}/your-page">Link</a>`
5+
*/
6+
export let base: '' | `/${string}`;
7+
8+
/**
9+
* An absolute path that matches [`config.kit.paths.assets`](https://kit.svelte.dev/docs/configuration#paths).
10+
*
11+
* > If a value for `config.kit.paths.assets` is specified, it will be replaced with `'/_svelte_kit_assets'` during `vite dev` or `vite preview`, since the assets don't yet live at their eventual URL.
12+
*/
13+
export let assets: '' | `https://${string}` | `http://${string}` | '/_svelte_kit_assets';
14+
15+
/**
16+
* Populate a route ID with params to resolve a pathname.
17+
* @example
18+
* ```js
19+
* resolveRoute(
20+
* `/blog/[slug]/[...somethingElse]`,
21+
* {
22+
* slug: 'hello-world',
23+
* somethingElse: 'something/else'
24+
* }
25+
* ); // `/blog/hello-world/something/else`
26+
* ```
27+
*/
28+
export function resolveRoute(id: string, params: Record<string, string | undefined>): string;
+16-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
declare global {
2-
const __SVELTEKIT_ADAPTER_NAME__: string;
3-
const __SVELTEKIT_APP_VERSION_FILE__: string;
4-
const __SVELTEKIT_APP_VERSION_POLL_INTERVAL__: number;
5-
const __SVELTEKIT_DEV__: boolean;
6-
const __SVELTEKIT_EMBEDDED__: boolean;
7-
var Bun: object;
8-
var Deno: object;
1+
/** Internal version of $app/environment */
2+
declare module '__sveltekit/environment' {
3+
export const building: boolean;
4+
export const prerendering: boolean;
5+
export const version: string;
6+
export function set_building(): void;
7+
export function set_prerendering(): void;
98
}
109

11-
export {};
10+
/** Internal version of $app/paths */
11+
declare module '__sveltekit/paths' {
12+
export let base: '' | `/${string}`;
13+
export let assets: '' | `https://${string}` | `http://${string}` | '/_svelte_kit_assets';
14+
export let relative: boolean;
15+
export function reset(): void;
16+
export function override(paths: { base: string; assets: string }): void;
17+
export function set_assets(path: string): void;
18+
}

packages/kit/src/types/ambient.d.ts

-38
Original file line numberDiff line numberDiff line change
@@ -79,41 +79,3 @@ declare module '$service-worker' {
7979
*/
8080
export const version: string;
8181
}
82-
83-
/** Internal version of $app/environment */
84-
declare module '__sveltekit/environment' {
85-
/**
86-
* SvelteKit analyses your app during the `build` step by running it. During this process, `building` is `true`. This also applies during prerendering.
87-
*/
88-
export const building: boolean;
89-
/**
90-
* True during prerendering, false otherwise.
91-
*/
92-
export const prerendering: boolean;
93-
/**
94-
* The value of `config.kit.version.name`.
95-
*/
96-
export const version: string;
97-
export function set_building(): void;
98-
export function set_prerendering(): void;
99-
}
100-
101-
/** Internal version of $app/paths */
102-
declare module '__sveltekit/paths' {
103-
/**
104-
* A string that matches [`config.kit.paths.base`](https://kit.svelte.dev/docs/configuration#paths).
105-
*
106-
* Example usage: `<a href="{base}/your-page">Link</a>`
107-
*/
108-
export let base: '' | `/${string}`;
109-
/**
110-
* An absolute path that matches [`config.kit.paths.assets`](https://kit.svelte.dev/docs/configuration#paths).
111-
*
112-
* > If a value for `config.kit.paths.assets` is specified, it will be replaced with `'/_svelte_kit_assets'` during `vite dev` or `vite preview`, since the assets don't yet live at their eventual URL.
113-
*/
114-
export let assets: '' | `https://${string}` | `http://${string}` | '/_svelte_kit_assets';
115-
export let relative: boolean;
116-
export function reset(): void;
117-
export function override(paths: { base: string; assets: string }): void;
118-
export function set_assets(path: string): void;
119-
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
declare global {
2+
const __SVELTEKIT_ADAPTER_NAME__: string;
3+
const __SVELTEKIT_APP_VERSION_FILE__: string;
4+
const __SVELTEKIT_APP_VERSION_POLL_INTERVAL__: number;
5+
const __SVELTEKIT_DEV__: boolean;
6+
const __SVELTEKIT_EMBEDDED__: boolean;
7+
var Bun: object;
8+
var Deno: object;
9+
}
10+
11+
export {};

packages/kit/types/index.d.ts

+26-41
Original file line numberDiff line numberDiff line change
@@ -1892,15 +1892,25 @@ declare module '@sveltejs/kit/vite' {
18921892
}
18931893

18941894
declare module '$app/environment' {
1895-
export { building, version } from '__sveltekit/environment';
18961895
/**
18971896
* `true` if the app is running in the browser.
18981897
*/
18991898
export const browser: boolean;
1899+
19001900
/**
19011901
* Whether the dev server is running. This is not guaranteed to correspond to `NODE_ENV` or `MODE`.
19021902
*/
19031903
export const dev: boolean;
1904+
1905+
/**
1906+
* SvelteKit analyses your app during the `build` step by running it. During this process, `building` is `true`. This also applies during prerendering.
1907+
*/
1908+
export const building: boolean;
1909+
1910+
/**
1911+
* The value of `config.kit.version.name`.
1912+
*/
1913+
export const version: string;
19041914
}
19051915

19061916
declare module '$app/forms' {
@@ -2067,7 +2077,20 @@ declare module '$app/navigation' {
20672077
}
20682078

20692079
declare module '$app/paths' {
2070-
export { base, assets } from '__sveltekit/paths';
2080+
/**
2081+
* A string that matches [`config.kit.paths.base`](https://kit.svelte.dev/docs/configuration#paths).
2082+
*
2083+
* Example usage: `<a href="{base}/your-page">Link</a>`
2084+
*/
2085+
export let base: '' | `/${string}`;
2086+
2087+
/**
2088+
* An absolute path that matches [`config.kit.paths.assets`](https://kit.svelte.dev/docs/configuration#paths).
2089+
*
2090+
* > If a value for `config.kit.paths.assets` is specified, it will be replaced with `'/_svelte_kit_assets'` during `vite dev` or `vite preview`, since the assets don't yet live at their eventual URL.
2091+
*/
2092+
export let assets: '' | `https://${string}` | `http://${string}` | '/_svelte_kit_assets';
2093+
20712094
/**
20722095
* Populate a route ID with params to resolve a pathname.
20732096
* @example
@@ -2080,7 +2103,7 @@ declare module '$app/paths' {
20802103
* }
20812104
* ); // `/blog/hello-world/something/else`
20822105
* ```
2083-
* */
2106+
*/
20842107
export function resolveRoute(id: string, params: Record<string, string | undefined>): string;
20852108
}
20862109

@@ -2198,42 +2221,4 @@ declare module '$service-worker' {
21982221
export const version: string;
21992222
}
22002223

2201-
/** Internal version of $app/environment */
2202-
declare module '__sveltekit/environment' {
2203-
/**
2204-
* SvelteKit analyses your app during the `build` step by running it. During this process, `building` is `true`. This also applies during prerendering.
2205-
*/
2206-
export const building: boolean;
2207-
/**
2208-
* True during prerendering, false otherwise.
2209-
*/
2210-
export const prerendering: boolean;
2211-
/**
2212-
* The value of `config.kit.version.name`.
2213-
*/
2214-
export const version: string;
2215-
export function set_building(): void;
2216-
export function set_prerendering(): void;
2217-
}
2218-
2219-
/** Internal version of $app/paths */
2220-
declare module '__sveltekit/paths' {
2221-
/**
2222-
* A string that matches [`config.kit.paths.base`](https://kit.svelte.dev/docs/configuration#paths).
2223-
*
2224-
* Example usage: `<a href="{base}/your-page">Link</a>`
2225-
*/
2226-
export let base: '' | `/${string}`;
2227-
/**
2228-
* An absolute path that matches [`config.kit.paths.assets`](https://kit.svelte.dev/docs/configuration#paths).
2229-
*
2230-
* > If a value for `config.kit.paths.assets` is specified, it will be replaced with `'/_svelte_kit_assets'` during `vite dev` or `vite preview`, since the assets don't yet live at their eventual URL.
2231-
*/
2232-
export let assets: '' | `https://${string}` | `http://${string}` | '/_svelte_kit_assets';
2233-
export let relative: boolean;
2234-
export function reset(): void;
2235-
export function override(paths: { base: string; assets: string }): void;
2236-
export function set_assets(path: string): void;
2237-
}
2238-
22392224
//# sourceMappingURL=index.d.ts.map

sites/kit.svelte.dev/scripts/types/index.js

-16
Original file line numberDiff line numberDiff line change
@@ -310,22 +310,6 @@ for (const file of await readdir(dir)) {
310310
}
311311
}
312312

313-
// need to do some unfortunate finagling here, hopefully we can remove this one day
314-
const app_paths = modules.find((module) => module.name === '$app/paths');
315-
const app_environment = modules.find((module) => module.name === '$app/environment');
316-
const __sveltekit_paths = modules.find((module) => module.name === '__sveltekit/paths');
317-
const __sveltekit_environment = modules.find((module) => module.name === '__sveltekit/environment');
318-
319-
app_paths?.exports.push(
320-
__sveltekit_paths.exports.find((e) => e.name === 'assets'),
321-
__sveltekit_paths.exports.find((e) => e.name === 'base')
322-
);
323-
324-
app_environment?.exports.push(
325-
__sveltekit_environment.exports.find((e) => e.name === 'building'),
326-
__sveltekit_environment.exports.find((e) => e.name === 'version')
327-
);
328-
329313
modules.sort((a, b) => (a.name < b.name ? -1 : 1));
330314

331315
mkdirp('src/lib/generated');

0 commit comments

Comments
 (0)