メインコンテンツにスキップ

アダプターの作成

ご希望の環境向けのアダプターがまだ存在しない場合、独自に構築することができます。ご希望のプラットフォームに似たアダプターのソースを参照し、開始時点としてコピーすることをお勧めします。

アダプターパッケージは、Adapter を作成する以下の API を実装します。

/** @param {AdapterSpecificOptions} options */
export default function (options: any
@paramoptions
options
) {
/** @type {import('@sveltejs/kit').Adapter} */ const const adapter: Adapter
@type{import('@sveltejs/kit').Adapter}
adapter
= {
Adapter.name: string

The name of the adapter, using for logging. Will typically correspond to the package name.

name
: 'adapter-package-name',
async Adapter.adapt(builder: Builder): MaybePromise<void>

This function is called after SvelteKit has built your app.

@parambuilder An object provided by SvelteKit that contains methods for adapting the app
adapt
(builder: Builderbuilder) {
// adapter implementation }, async Adapter.emulate?(): MaybePromise<Emulator>

Creates an Emulator, which allows the adapter to influence the environment during dev, build and prerendering

emulate
() {
return { async
Emulator.platform?(details: {
    config: any;
    prerender: PrerenderOption;
}): MaybePromise<App.Platform>

A function that is called with the current route config and prerender option and returns an App.Platform object

platform
({ config: anyconfig, prerender: PrerenderOptionprerender }) {
// the returned object becomes `event.platform` during dev, build and // preview. Its shape is that of `App.Platform` } } },
Adapter.supports?: {
    read?: (details: {
        config: any;
        route: {
            id: string;
        };
    }) => boolean;
} | undefined

Checks called during dev and build to determine whether specific features will work in production with this adapter

supports
: {
read?: ((details: {
    config: any;
    route: {
        id: string;
    };
}) => boolean) | undefined

Test support for read from $app/server

@paramconfig The merged route config
read
: ({ config: anyconfig,
route: {
    id: string;
}
route
}) => {
// Return `true` if the route with the given `config` can use `read` // from `$app/server` in production, return `false` if it can't. // Or throw a descriptive error describing how to configure the deployment } } }; return const adapter: Adapter
@type{import('@sveltejs/kit').Adapter}
adapter
;
}

これらの中で、nameadapt は必須です。 emulatesupports はオプションです。

adapt メソッド内では、アダプターが必要とする次の操作を行います。

  • ビルドディレクトリのクリア
  • builder.writeClientbuilder.writeServerbuilder.writePrerendered により、SvelteKit 出力を書き込む
  • 次のコードを出力する
    • ${builder.getServerDirectory()}/index.js から Server をインポート
    • builder.generateManifest({ relativePath }) で生成したマニフェストを使用してアプリケーションをインスタンス化
    • プラットフォームからのリクエストをリッスンし、必要に応じて標準の Request に変換し、server.respond(request, { getClientAddress }) 関数を呼び出して Response を生成し、それに対して応答
    • server.respond に渡される platform オプションを通じて、プラットフォーム固有の情報を SvelteKit に公開
    • 必要に応じて、fetch をグローバルにシムし、ターゲットのプラットフォームで動作させる。SvelteKit は、undici を使用できるプラットフォーム用の @sveltejs/kit/node/polyfills ヘルパーを提供しています。
  • 必要に応じて、依存関係のインストールなしでターゲットのプラットフォームで動作するように出力をバンドル
  • ターゲットプラットフォーム用の、ユーザーの静的ファイルと生成された JS/CSS を正しい場所に配置します

可能な場合は、アダプター出力を build/ ディレクトリに配置し、中間出力を .svelte-kit/[adapter-name] に配置することをおすすめします。

GitHub でこのページを編集する