Skip to content

Commit

Permalink
fix: remove internal __sveltekit/ module declarations from types (#…
Browse files Browse the repository at this point in the history
…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]>
  • Loading branch information
dummdidumm and Rich-Harris committed Jan 12, 2024
1 parent 067d369 commit 2137717
Show file tree
Hide file tree
Showing 13 changed files with 132 additions and 142 deletions.
5 changes: 5 additions & 0 deletions .changeset/brave-cats-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/kit": patch
---

fix: remove internal `__sveltekit/` module declarations from types
16 changes: 13 additions & 3 deletions packages/kit/scripts/generate-dts.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import { createBundle } from 'dts-buddy';
import { readFileSync } from 'node:fs';

createBundle({
await createBundle({
output: 'types/index.d.ts',
modules: {
'@sveltejs/kit': 'src/exports/public.d.ts',
'@sveltejs/kit/hooks': 'src/exports/hooks/index.js',
'@sveltejs/kit/node': 'src/exports/node/index.js',
'@sveltejs/kit/node/polyfills': 'src/exports/node/polyfills.js',
'@sveltejs/kit/vite': 'src/exports/vite/index.js',
'$app/environment': 'src/runtime/app/environment.js',
'$app/environment': 'src/runtime/app/environment/types.d.ts',
'$app/forms': 'src/runtime/app/forms.js',
'$app/navigation': 'src/runtime/app/navigation.js',
'$app/paths': 'src/runtime/app/paths.js',
'$app/paths': 'src/runtime/app/paths/types.d.ts',
'$app/stores': 'src/runtime/app/stores.js'
},
include: ['src']
});

// 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
const types = readFileSync('./types/index.d.ts', 'utf-8');
if (types.includes('__sveltekit/')) {
throw new Error(
'Found __sveltekit/ in types/index.d.ts - make sure to hide internal modules by not just reexporting them. Contents:\n\n' +
types
);
}
12 changes: 0 additions & 12 deletions packages/kit/src/runtime/app/environment.js

This file was deleted.

6 changes: 6 additions & 0 deletions packages/kit/src/runtime/app/environment/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { BROWSER, DEV } from 'esm-env';
export { building, version } from '__sveltekit/environment';

export const browser = BROWSER;

export const dev = DEV;
19 changes: 19 additions & 0 deletions packages/kit/src/runtime/app/environment/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* `true` if the app is running in the browser.
*/
export const browser: boolean;

/**
* Whether the dev server is running. This is not guaranteed to correspond to `NODE_ENV` or `MODE`.
*/
export const dev: boolean;

/**
* SvelteKit analyses your app during the `build` step by running it. During this process, `building` is `true`. This also applies during prerendering.
*/
export const building: boolean;

/**
* The value of `config.kit.version.name`.
*/
export const version: string;
23 changes: 0 additions & 23 deletions packages/kit/src/runtime/app/paths.js

This file was deleted.

8 changes: 8 additions & 0 deletions packages/kit/src/runtime/app/paths/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export { base, assets } from '__sveltekit/paths';
import { base } from '__sveltekit/paths';
import { resolve_route } from '../../../utils/routing.js';

/** @type {import('./types.d.ts').resolveRoute} */
export function resolveRoute(id, params) {
return base + resolve_route(id, params);
}
28 changes: 28 additions & 0 deletions packages/kit/src/runtime/app/paths/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* A string that matches [`config.kit.paths.base`](https://kit.svelte.dev/docs/configuration#paths).
*
* Example usage: `<a href="{base}/your-page">Link</a>`
*/
export let base: '' | `/${string}`;

/**
* An absolute path that matches [`config.kit.paths.assets`](https://kit.svelte.dev/docs/configuration#paths).
*
* > 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.
*/
export let assets: '' | `https://${string}` | `http://${string}` | '/_svelte_kit_assets';

/**
* Populate a route ID with params to resolve a pathname.
* @example
* ```js
* resolveRoute(
* `/blog/[slug]/[...somethingElse]`,
* {
* slug: 'hello-world',
* somethingElse: 'something/else'
* }
* ); // `/blog/hello-world/something/else`
* ```
*/
export function resolveRoute(id: string, params: Record<string, string | undefined>): string;
25 changes: 16 additions & 9 deletions packages/kit/src/types/ambient-private.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
declare global {
const __SVELTEKIT_ADAPTER_NAME__: string;
const __SVELTEKIT_APP_VERSION_FILE__: string;
const __SVELTEKIT_APP_VERSION_POLL_INTERVAL__: number;
const __SVELTEKIT_DEV__: boolean;
const __SVELTEKIT_EMBEDDED__: boolean;
var Bun: object;
var Deno: object;
/** Internal version of $app/environment */
declare module '__sveltekit/environment' {
export const building: boolean;
export const prerendering: boolean;
export const version: string;
export function set_building(): void;
export function set_prerendering(): void;
}

export {};
/** Internal version of $app/paths */
declare module '__sveltekit/paths' {
export let base: '' | `/${string}`;
export let assets: '' | `https://${string}` | `http://${string}` | '/_svelte_kit_assets';
export let relative: boolean;
export function reset(): void;
export function override(paths: { base: string; assets: string }): void;
export function set_assets(path: string): void;
}
38 changes: 0 additions & 38 deletions packages/kit/src/types/ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,41 +79,3 @@ declare module '$service-worker' {
*/
export const version: string;
}

/** Internal version of $app/environment */
declare module '__sveltekit/environment' {
/**
* SvelteKit analyses your app during the `build` step by running it. During this process, `building` is `true`. This also applies during prerendering.
*/
export const building: boolean;
/**
* True during prerendering, false otherwise.
*/
export const prerendering: boolean;
/**
* The value of `config.kit.version.name`.
*/
export const version: string;
export function set_building(): void;
export function set_prerendering(): void;
}

/** Internal version of $app/paths */
declare module '__sveltekit/paths' {
/**
* A string that matches [`config.kit.paths.base`](https://kit.svelte.dev/docs/configuration#paths).
*
* Example usage: `<a href="{base}/your-page">Link</a>`
*/
export let base: '' | `/${string}`;
/**
* An absolute path that matches [`config.kit.paths.assets`](https://kit.svelte.dev/docs/configuration#paths).
*
* > 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.
*/
export let assets: '' | `https://${string}` | `http://${string}` | '/_svelte_kit_assets';
export let relative: boolean;
export function reset(): void;
export function override(paths: { base: string; assets: string }): void;
export function set_assets(path: string): void;
}
11 changes: 11 additions & 0 deletions packages/kit/src/types/global-private.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
declare global {
const __SVELTEKIT_ADAPTER_NAME__: string;
const __SVELTEKIT_APP_VERSION_FILE__: string;
const __SVELTEKIT_APP_VERSION_POLL_INTERVAL__: number;
const __SVELTEKIT_DEV__: boolean;
const __SVELTEKIT_EMBEDDED__: boolean;
var Bun: object;
var Deno: object;
}

export {};
67 changes: 26 additions & 41 deletions packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1892,15 +1892,25 @@ declare module '@sveltejs/kit/vite' {
}

declare module '$app/environment' {
export { building, version } from '__sveltekit/environment';
/**
* `true` if the app is running in the browser.
*/
export const browser: boolean;

/**
* Whether the dev server is running. This is not guaranteed to correspond to `NODE_ENV` or `MODE`.
*/
export const dev: boolean;

/**
* SvelteKit analyses your app during the `build` step by running it. During this process, `building` is `true`. This also applies during prerendering.
*/
export const building: boolean;

/**
* The value of `config.kit.version.name`.
*/
export const version: string;
}

declare module '$app/forms' {
Expand Down Expand Up @@ -2067,7 +2077,20 @@ declare module '$app/navigation' {
}

declare module '$app/paths' {
export { base, assets } from '__sveltekit/paths';
/**
* A string that matches [`config.kit.paths.base`](https://kit.svelte.dev/docs/configuration#paths).
*
* Example usage: `<a href="{base}/your-page">Link</a>`
*/
export let base: '' | `/${string}`;

/**
* An absolute path that matches [`config.kit.paths.assets`](https://kit.svelte.dev/docs/configuration#paths).
*
* > 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.
*/
export let assets: '' | `https://${string}` | `http://${string}` | '/_svelte_kit_assets';

/**
* Populate a route ID with params to resolve a pathname.
* @example
Expand All @@ -2080,7 +2103,7 @@ declare module '$app/paths' {
* }
* ); // `/blog/hello-world/something/else`
* ```
* */
*/
export function resolveRoute(id: string, params: Record<string, string | undefined>): string;
}

Expand Down Expand Up @@ -2198,42 +2221,4 @@ declare module '$service-worker' {
export const version: string;
}

/** Internal version of $app/environment */
declare module '__sveltekit/environment' {
/**
* SvelteKit analyses your app during the `build` step by running it. During this process, `building` is `true`. This also applies during prerendering.
*/
export const building: boolean;
/**
* True during prerendering, false otherwise.
*/
export const prerendering: boolean;
/**
* The value of `config.kit.version.name`.
*/
export const version: string;
export function set_building(): void;
export function set_prerendering(): void;
}

/** Internal version of $app/paths */
declare module '__sveltekit/paths' {
/**
* A string that matches [`config.kit.paths.base`](https://kit.svelte.dev/docs/configuration#paths).
*
* Example usage: `<a href="{base}/your-page">Link</a>`
*/
export let base: '' | `/${string}`;
/**
* An absolute path that matches [`config.kit.paths.assets`](https://kit.svelte.dev/docs/configuration#paths).
*
* > 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.
*/
export let assets: '' | `https://${string}` | `http://${string}` | '/_svelte_kit_assets';
export let relative: boolean;
export function reset(): void;
export function override(paths: { base: string; assets: string }): void;
export function set_assets(path: string): void;
}

//# sourceMappingURL=index.d.ts.map
16 changes: 0 additions & 16 deletions sites/kit.svelte.dev/scripts/types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,22 +310,6 @@ for (const file of await readdir(dir)) {
}
}

// need to do some unfortunate finagling here, hopefully we can remove this one day
const app_paths = modules.find((module) => module.name === '$app/paths');
const app_environment = modules.find((module) => module.name === '$app/environment');
const __sveltekit_paths = modules.find((module) => module.name === '__sveltekit/paths');
const __sveltekit_environment = modules.find((module) => module.name === '__sveltekit/environment');

app_paths?.exports.push(
__sveltekit_paths.exports.find((e) => e.name === 'assets'),
__sveltekit_paths.exports.find((e) => e.name === 'base')
);

app_environment?.exports.push(
__sveltekit_environment.exports.find((e) => e.name === 'building'),
__sveltekit_environment.exports.find((e) => e.name === 'version')
);

modules.sort((a, b) => (a.name < b.name ? -1 : 1));

mkdirp('src/lib/generated');
Expand Down

0 comments on commit 2137717

Please sign in to comment.