Skip to content

Commit

Permalink
fix: remove internal __sveltekit/ module declarations from types
Browse files Browse the repository at this point in the history
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
  • Loading branch information
dummdidumm committed Jan 12, 2024
1 parent e54d56e commit 4fdea3b
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 91 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
12 changes: 11 additions & 1 deletion packages/kit/scripts/generate-dts.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createBundle } from 'dts-buddy';
import { readFileSync } from 'fs';

Check failure on line 2 in packages/kit/scripts/generate-dts.js

View workflow job for this annotation

GitHub Actions / lint-all

Prefer `node:fs` over `fs`

createBundle({
await createBundle({
output: 'types/index.d.ts',
modules: {
'@sveltejs/kit': 'src/exports/public.d.ts',
Expand All @@ -16,3 +17,12 @@ createBundle({
},
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: 11 additions & 1 deletion packages/kit/src/runtime/app/environment.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { BROWSER, DEV } from 'esm-env';
export { building, version } from '__sveltekit/environment';
import { building as _building, version as _version } from '__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 = _building;

/**
* The value of `config.kit.version.name`.
*/
export const version = _version;

/**
* `true` if the app is running in the browser.
Expand Down
19 changes: 17 additions & 2 deletions packages/kit/src/runtime/app/paths.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
export { base, assets } from '__sveltekit/paths';
import { base } from '__sveltekit/paths';
import { base as _base, assets as _assets } from '__sveltekit/paths';
import { resolve_route } from '../../utils/routing.js';

/**
* 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.
* @type {'' | `https://${string}` | `http://${string}` | '/_svelte_kit_assets'}
*/
export const assets = _assets;

/**
* A string that matches [`config.kit.paths.base`](https://kit.svelte.dev/docs/configuration#paths).
*
* Example usage: `<a href="{base}/your-page">Link</a>`
* @type {'' | `/${string}`}
*/
export const base = _base;

/**
* Populate a route ID with params to resolve a pathname.
* @example
Expand Down
44 changes: 35 additions & 9 deletions packages/kit/src/types/ambient-private.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
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' {
/**
* 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;
}

export {};
/** 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;
}
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 {};
60 changes: 20 additions & 40 deletions packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1892,7 +1892,14 @@ declare module '@sveltejs/kit/vite' {
}

declare module '$app/environment' {
export { building, version } from '__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;
/**
* The value of `config.kit.version.name`.
*/
export const version: string;
/**
* `true` if the app is running in the browser.
*/
Expand Down Expand Up @@ -2067,7 +2074,6 @@ declare module '$app/navigation' {
}

declare module '$app/paths' {
export { base, assets } from '__sveltekit/paths';
/**
* Populate a route ID with params to resolve a pathname.
* @example
Expand All @@ -2082,6 +2088,18 @@ declare module '$app/paths' {
* ```
* */
export function resolveRoute(id: string, params: Record<string, string | undefined>): 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 const assets: '' | `https://${string}` | `http://${string}` | '/_svelte_kit_assets';
/**
* 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 const base: '' | `/${string}`;
}

declare module '$app/stores' {
Expand Down Expand Up @@ -2198,42 +2216,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

0 comments on commit 4fdea3b

Please sign in to comment.