Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/fifty-breads-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@sveltejs/kit": minor
---

feat: allow adapters to receive the Svelte config as a function argument when adding Vite plugins

20 changes: 9 additions & 11 deletions packages/adapter-cloudflare/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
]
}
};
}
Expand Down
6 changes: 6 additions & 0 deletions packages/kit/src/core/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
69 changes: 41 additions & 28 deletions packages/kit/src/exports/public.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,34 +57,47 @@ export interface Adapter {
* during dev, build and prerendering.
*/
emulate?: () => MaybePromise<Emulator>;
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[];
};
Comment on lines +83 to +100

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel a little conflicted about having the single array option default to pre. I kind of liked the explicitness of pre and post.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think pre and post are only needed in very rare situations; it would be confusing to adapter authors if we required the specificity when it actually doesn't matter most of the time.

}

export type LoadProperties<input extends Record<string, any> | void> = input extends void
Expand Down
6 changes: 6 additions & 0 deletions packages/kit/src/exports/vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,12 @@ function kit({ svelte_config }) {
/** @type {(() => Promise<void>) | 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,
Expand Down
7 changes: 5 additions & 2 deletions packages/kit/src/types/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -592,7 +594,8 @@ export interface Uses {
search_params: Set<string>;
}

export type ValidatedConfig = RecursiveRequired<Omit<Config, 'preprocess'>> & {
export type ValidatedConfig = RecursiveRequired<Omit<Config, 'preprocess' | 'adapter'>> & {
adapter: Adapter & { vite?: AdapterViteConfig };
preprocess: Config['preprocess'];
};

Expand Down
72 changes: 43 additions & 29 deletions packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,34 +44,47 @@ declare module '@sveltejs/kit' {
* during dev, build and prerendering.
*/
emulate?: () => MaybePromise<Emulator>;
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<input extends Record<string, any> | void> = input extends void
Expand Down Expand Up @@ -957,7 +970,8 @@ declare module '@sveltejs/kit' {
: T[K]; // Use the exact type for everything else
};

type ValidatedConfig = RecursiveRequired<Omit<Config, 'preprocess'>> & {
type ValidatedConfig = RecursiveRequired<Omit<Config, 'preprocess' | 'adapter'>> & {
adapter: Adapter & { vite?: AdapterViteConfig };
preprocess: Config['preprocess'];
};
/**
Expand Down
Loading