diff --git a/.changeset/fifty-breads-judge.md b/.changeset/fifty-breads-judge.md new file mode 100644 index 000000000000..4b49fc2d8725 --- /dev/null +++ b/.changeset/fifty-breads-judge.md @@ -0,0 +1,6 @@ +--- +"@sveltejs/kit": minor +--- + +feat: allow adapters to receive the Svelte config as a function argument when adding Vite plugins + \ No newline at end of file diff --git a/packages/adapter-cloudflare/index.js b/packages/adapter-cloudflare/index.js index 9a78873ee070..f04f9ac4cfb1 100644 --- a/packages/adapter-cloudflare/index.js +++ b/packages/adapter-cloudflare/index.js @@ -203,17 +203,15 @@ export default function (options = {}) { ).cf = globalThis.__sveltekit_cloudflare_platform?.cf; return request; }, - plugins: { - pre: [ - virtual_workers_module( - { - configPath: options.config, - ...options.platformProxy - }, - stub_import - ) - ] - } + plugins: [ + virtual_workers_module( + { + configPath: options.config, + ...options.platformProxy + }, + stub_import + ) + ] } }; } diff --git a/packages/kit/src/core/config/index.js b/packages/kit/src/core/config/index.js index 31ba3baa2952..bdb938a8f4a3 100644 --- a/packages/kit/src/core/config/index.js +++ b/packages/kit/src/core/config/index.js @@ -200,6 +200,12 @@ export function validate_config(config) { } } + if (typeof config.adapter?.vite === 'function') { + validated.adapter.vite = config.adapter.vite({ + config: validated + }); + } + return validated; } catch (e) { const error = /** @type {Error} */ (e); diff --git a/packages/kit/src/exports/public.d.ts b/packages/kit/src/exports/public.d.ts index 8f0a9665b069..1f1e441f109a 100644 --- a/packages/kit/src/exports/public.d.ts +++ b/packages/kit/src/exports/public.d.ts @@ -57,34 +57,47 @@ export interface Adapter { * during dev, build and prerendering. */ emulate?: () => MaybePromise; - vite?: { - /** - * This function overrides the default behavior during Vite's dev and preview modes - * to convert an `http.IncomingMessage` to a `Request` object. - * To call the original `setRequest` function, import it from `@sveltejs/kit/node`. - * @since 3.0.0 - */ - getRequest?: typeof getRequest; - /** - * This function overrides the default behavior in Vite's dev and preview modes - * to write a `Response` object to a `http.ServerResponse`. - * To call the original `setResponse` function, import it from `@sveltejs/kit/node`. - * @since 3.0.0 - */ - setResponse?: typeof setResponse; - plugins?: { - /** - * Vite plugins placed before any of SvelteKit's own plugins. - * @since 3.0.0 - */ - pre?: Plugin[]; - /** - * Vite plugins placed after any of SvelteKit's own plugins. - * @since 3.0.0 - */ - post?: Plugin[]; - }; - }; + /** + * Options for configuring and interacting with Vite + * @since 3.0.0 + */ + vite?: AdapterViteConfig | ((ctx: { config: ValidatedConfig }) => AdapterViteConfig); +} + +export interface AdapterViteConfig { + /** + * This function overrides the default behavior during Vite's dev and preview modes + * to convert an `http.IncomingMessage` to a `Request` object. + * To call the original `setRequest` function, import it from `@sveltejs/kit/node`. + * @since 3.0.0 + */ + getRequest?: typeof getRequest; + /** + * This function overrides the default behavior in Vite's dev and preview modes + * to write a `Response` object to a `http.ServerResponse`. + * To call the original `setResponse` function, import it from `@sveltejs/kit/node`. + * @since 3.0.0 + */ + setResponse?: typeof setResponse; + /** + * Vite plugins injected by the adapter. By default, + * they are placed before SvelteKit's plugins. + * @since 3.0.0 + */ + plugins?: + | Plugin[] + | { + /** + * Vite plugins placed before any of SvelteKit's own plugins. + * @since 3.0.0 + */ + pre?: Plugin[]; + /** + * Vite plugins placed after any of SvelteKit's own plugins. + * @since 3.0.0 + */ + post?: Plugin[]; + }; } export type LoadProperties | void> = input extends void diff --git a/packages/kit/src/exports/vite/index.js b/packages/kit/src/exports/vite/index.js index 2cafd83df3e7..235fcf9a3981 100644 --- a/packages/kit/src/exports/vite/index.js +++ b/packages/kit/src/exports/vite/index.js @@ -633,6 +633,12 @@ function kit({ svelte_config }) { /** @type {(() => Promise) | null} */ let finalise = null; + if (Array.isArray(svelte_config.adapter?.vite?.plugins)) { + svelte_config.adapter.vite.plugins = { + pre: svelte_config.adapter.vite.plugins + }; + } + return /** @type {Plugin[]} */ ( [ svelte_config.adapter?.vite?.plugins?.pre, diff --git a/packages/kit/src/types/internal.d.ts b/packages/kit/src/types/internal.d.ts index ab935ae0938a..8a107863f31a 100644 --- a/packages/kit/src/types/internal.d.ts +++ b/packages/kit/src/types/internal.d.ts @@ -8,7 +8,9 @@ import { Actions, RequestEvent, Emulator, - HttpError + HttpError, + Adapter, + AdapterViteConfig } from '@sveltejs/kit'; import { RemoteFormIssue, RemoteQuery, RemoteLiveQuery } from '$app/server'; import { Config } from '@sveltejs/kit/vite'; @@ -592,7 +594,8 @@ export interface Uses { search_params: Set; } -export type ValidatedConfig = RecursiveRequired> & { +export type ValidatedConfig = RecursiveRequired> & { + adapter: Adapter & { vite?: AdapterViteConfig }; preprocess: Config['preprocess']; }; diff --git a/packages/kit/types/index.d.ts b/packages/kit/types/index.d.ts index 8c919daac3d3..ad8502f932c4 100644 --- a/packages/kit/types/index.d.ts +++ b/packages/kit/types/index.d.ts @@ -44,34 +44,47 @@ declare module '@sveltejs/kit' { * during dev, build and prerendering. */ emulate?: () => MaybePromise; - vite?: { - /** - * This function overrides the default behavior during Vite's dev and preview modes - * to convert an `http.IncomingMessage` to a `Request` object. - * To call the original `setRequest` function, import it from `@sveltejs/kit/node`. - * @since 3.0.0 - */ - getRequest?: typeof getRequest; - /** - * This function overrides the default behavior in Vite's dev and preview modes - * to write a `Response` object to a `http.ServerResponse`. - * To call the original `setResponse` function, import it from `@sveltejs/kit/node`. - * @since 3.0.0 - */ - setResponse?: typeof setResponse; - plugins?: { - /** - * Vite plugins placed before any of SvelteKit's own plugins. - * @since 3.0.0 - */ - pre?: Plugin[]; - /** - * Vite plugins placed after any of SvelteKit's own plugins. - * @since 3.0.0 - */ - post?: Plugin[]; - }; - }; + /** + * Options for configuring and interacting with Vite + * @since 3.0.0 + */ + vite?: AdapterViteConfig | ((ctx: { config: ValidatedConfig }) => AdapterViteConfig); + } + + export interface AdapterViteConfig { + /** + * This function overrides the default behavior during Vite's dev and preview modes + * to convert an `http.IncomingMessage` to a `Request` object. + * To call the original `setRequest` function, import it from `@sveltejs/kit/node`. + * @since 3.0.0 + */ + getRequest?: typeof getRequest; + /** + * This function overrides the default behavior in Vite's dev and preview modes + * to write a `Response` object to a `http.ServerResponse`. + * To call the original `setResponse` function, import it from `@sveltejs/kit/node`. + * @since 3.0.0 + */ + setResponse?: typeof setResponse; + /** + * Vite plugins injected by the adapter. By default, + * they are placed before SvelteKit's plugins. + * @since 3.0.0 + */ + plugins?: + | Plugin[] + | { + /** + * Vite plugins placed before any of SvelteKit's own plugins. + * @since 3.0.0 + */ + pre?: Plugin[]; + /** + * Vite plugins placed after any of SvelteKit's own plugins. + * @since 3.0.0 + */ + post?: Plugin[]; + }; } export type LoadProperties | void> = input extends void @@ -957,7 +970,8 @@ declare module '@sveltejs/kit' { : T[K]; // Use the exact type for everything else }; - type ValidatedConfig = RecursiveRequired> & { + type ValidatedConfig = RecursiveRequired> & { + adapter: Adapter & { vite?: AdapterViteConfig }; preprocess: Config['preprocess']; }; /**