Skip to main content

写入适配器

如果你首选环境的适配器尚不存在,你可以构建自己的适配器。我们向类似于你的平台推荐 查看适配器的来源 并将其复制为起点。

¥If an adapter for your preferred environment doesn’t yet exist, you can build your own. We recommend looking at the source for an adapter to a platform similar to yours and copying it as a starting point.

适配器包实现以下 API,它创建了一个 Adapter

¥Adapter packages implement the following API, which creates an Adapter:

/** @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: ({ 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 是可选的。

¥Of these, name and adapt are required. emulate and supports are optional.

adapt 方法中,适配器应该执行许多操作:

¥Within the adapt method, there are a number of things that an adapter should do:

  • 清除构建目录

    ¥Clear out the build directory

  • 使用 builder.writeClientbuilder.writeServerbuilder.writePrerendered 写入 SvelteKit 输出

    ¥Write SvelteKit output with builder.writeClient, builder.writeServer, and builder.writePrerendered

  • 输出代码:

    ¥Output code that:

    • ${builder.getServerDirectory()}/index.js 导入 Server

      ¥Imports Server from ${builder.getServerDirectory()}/index.js

    • 使用 builder.generateManifest({ relativePath }) 生成的清单实例化应用

      ¥Instantiates the app with a manifest generated with builder.generateManifest({ relativePath })

    • 监听来自平台的请求,如有必要,将其转换为标准 请求,调用 server.respond(request, { getClientAddress }) 函数生成 响应 并做出响应

      ¥Listens for requests from the platform, converts them to a standard Request if necessary, calls the server.respond(request, { getClientAddress }) function to generate a Response and responds with it

    • 通过传递给 server.respondplatform 选项向 SvelteKit 公开任何特定于平台的信息

      ¥expose any platform-specific information to SvelteKit via the platform option passed to server.respond

    • 如有必要,全局填充 fetch 以在目标平台上工作。SvelteKit 为可以使用 undici 的平台提供了 @sveltejs/kit/node/polyfills 辅助程序

      ¥Globally shims fetch to work on the target platform, if necessary. SvelteKit provides a @sveltejs/kit/node/polyfills helper for platforms that can use undici

  • 打包输出以避免在必要时需要在目标平台上安装依赖

    ¥Bundle the output to avoid needing to install dependencies on the target platform, if necessary

  • 将用户的静态文件和生成的 JS/CSS 放在目标平台的正确位置

    ¥Put the user’s static files and the generated JS/CSS in the correct location for the target platform

如果可能,我们建议将适配器输出放在 build/ 目录下,并将任何中间输出放在 .svelte-kit/[adapter-name] 下。

¥Where possible, we recommend putting the adapter output under the build/ directory with any intermediate output placed under .svelte-kit/[adapter-name].

上一页 下一页