From 440a5463e2f6939db4560955e5969850de855ad6 Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Thu, 25 Jan 2024 18:55:55 +0100 Subject: [PATCH] chore: clean up emulator types (#11737) * chore: clean up emulator types - the Emulator type is not a class export - make validated config types more correct by leaving adapter types as optional * fix * ok that doesnt work for some reason --------- Co-authored-by: Rich Harris --- packages/kit/src/core/adapt/index.js | 3 ++- packages/kit/src/exports/public.d.ts | 2 +- packages/kit/src/exports/vite/dev/index.js | 2 -- packages/kit/src/exports/vite/index.js | 1 - packages/kit/src/exports/vite/preview/index.js | 1 - packages/kit/src/types/internal.d.ts | 12 +++++++++--- packages/kit/types/index.d.ts | 11 +++++++++-- 7 files changed, 21 insertions(+), 11 deletions(-) diff --git a/packages/kit/src/core/adapt/index.js b/packages/kit/src/core/adapt/index.js index 235d18526687..3cfe52753248 100644 --- a/packages/kit/src/core/adapt/index.js +++ b/packages/kit/src/core/adapt/index.js @@ -19,7 +19,8 @@ export async function adapt( log, vite_config ) { - const { name, adapt } = config.kit.adapter; + // This is only called when adapter is truthy, so the cast is safe + const { name, adapt } = /** @type {import('@sveltejs/kit').Adapter} */ (config.kit.adapter); console.log(colors.bold().cyan(`\n> Using ${name}`)); diff --git a/packages/kit/src/exports/public.d.ts b/packages/kit/src/exports/public.d.ts index a9b808387123..e8d1c16ea01b 100644 --- a/packages/kit/src/exports/public.d.ts +++ b/packages/kit/src/exports/public.d.ts @@ -268,7 +268,7 @@ export interface Cookies { /** * A collection of functions that influence the environment during dev, build and prerendering */ -export class Emulator { +export interface Emulator { /** * A function that is called with the current route `config` and `prerender` option * and returns an `App.Platform` object diff --git a/packages/kit/src/exports/vite/dev/index.js b/packages/kit/src/exports/vite/dev/index.js index c3616321094e..503cb51feb45 100644 --- a/packages/kit/src/exports/vite/dev/index.js +++ b/packages/kit/src/exports/vite/dev/index.js @@ -419,8 +419,6 @@ export async function dev(vite, vite_config, svelte_config) { }); const env = loadEnv(vite_config.mode, svelte_config.kit.env.dir, ''); - - // TODO because of `RecursiveRequired`, TypeScript thinks this is guaranteed to exist, but it isn't const emulator = await svelte_config.kit.adapter?.emulate?.(); return () => { diff --git a/packages/kit/src/exports/vite/index.js b/packages/kit/src/exports/vite/index.js index 94d511ab86e7..e80fb786aca9 100644 --- a/packages/kit/src/exports/vite/index.js +++ b/packages/kit/src/exports/vite/index.js @@ -164,7 +164,6 @@ export async function sveltekit() { preprocess, onwarn: svelte_config.onwarn, compilerOptions: { - // @ts-expect-error SvelteKit requires hydratable true by default hydratable: isSvelte5Plus() ? undefined : true, ...svelte_config.compilerOptions }, diff --git a/packages/kit/src/exports/vite/preview/index.js b/packages/kit/src/exports/vite/preview/index.js index 3246fc6fe008..1e70d40573ee 100644 --- a/packages/kit/src/exports/vite/preview/index.js +++ b/packages/kit/src/exports/vite/preview/index.js @@ -51,7 +51,6 @@ export async function preview(vite, vite_config, svelte_config) { read: (file) => createReadableStream(`${dir}/${file}`) }); - // TODO because of `RecursiveRequired`, TypeScript thinks this is guaranteed to exist, but it isn't const emulator = await svelte_config.kit.adapter?.emulate?.(); return () => { diff --git a/packages/kit/src/types/internal.d.ts b/packages/kit/src/types/internal.d.ts index 864de0be71a3..af50cbe74c7d 100644 --- a/packages/kit/src/types/internal.d.ts +++ b/packages/kit/src/types/internal.d.ts @@ -16,7 +16,8 @@ import { Reroute, RequestEvent, SSRManifest, - Emulator + Emulator, + Adapter } from '@sveltejs/kit'; import { HttpMethod, @@ -434,9 +435,14 @@ export interface Uses { search_params: Set; } -export type ValidatedConfig = RecursiveRequired; +export type ValidatedConfig = Config & { + kit: ValidatedKitConfig; + extensions: string[]; +}; -export type ValidatedKitConfig = RecursiveRequired; +export type ValidatedKitConfig = Omit, 'adapter'> & { + adapter?: Adapter; +}; export * from '../exports/index.js'; export * from './private.js'; diff --git a/packages/kit/types/index.d.ts b/packages/kit/types/index.d.ts index 5c43bbac143f..eaa09c454eff 100644 --- a/packages/kit/types/index.d.ts +++ b/packages/kit/types/index.d.ts @@ -250,7 +250,7 @@ declare module '@sveltejs/kit' { /** * A collection of functions that influence the environment during dev, build and prerendering */ - export class Emulator { + export interface Emulator { /** * A function that is called with the current route `config` and `prerender` option * and returns an `App.Platform` object @@ -1748,7 +1748,14 @@ declare module '@sveltejs/kit' { endpoint_id?: string; } - type ValidatedConfig = RecursiveRequired; + type ValidatedConfig = Config & { + kit: ValidatedKitConfig; + extensions: string[]; + }; + + type ValidatedKitConfig = Omit, 'adapter'> & { + adapter?: Adapter; + }; /** * Throws an error with a HTTP status code and an optional message. * When called during request handling, this will cause SvelteKit to