建置和部署
撰寫適配器
在 GitHub 上編輯此頁面如果尚不存在適用於您偏好環境的適配器,您可以自行建置。我們建議查看類似於您平台的適配器原始碼,並將其複製作為起點。
適配器套件實作下列 API,它會建立一個適配器
ts
/** @param {AdapterSpecificOptions} options */export default function (options ) {/** @type {import('@sveltejs/kit').Adapter} */constadapter = {name : 'adapter-package-name',asyncadapt (builder ) {// adapter implementation},asyncemulate () {return {asyncType '({ config, route }: { config: any; route: { id: string; }; }) => void' is not assignable to type '(details: { config: any; route: { id: string; }; }) => boolean'. Type 'void' is not assignable to type 'boolean'.2322Type '({ config, route }: { config: any; route: { id: string; }; }) => void' is not assignable to type '(details: { config: any; route: { id: string; }; }) => boolean'. Type 'void' is not assignable to type 'boolean'.platform ({config ,prerender }) {// the returned object becomes `event.platform` during dev, build and// preview. Its shape is that of `App.Platform`}}},supports : {read : ({config ,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}}};returnadapter ;}
其中,名稱
和適應
是必要的。模擬
和支援
是選用的。
在適應
方法中,適配器應該執行多項工作
- 清除建置目錄
- 使用
builder.writeClient
、builder.writeServer
和builder.writePrerendered
撰寫 SvelteKit 輸出 - 輸出程式碼
- 從
${builder.getServerDirectory()}/index.js
匯入伺服器
- 使用
builder.generateManifest({ relativePath })
產生的清單實例化應用程式 - 偵聽來自平台的請求,必要時將其轉換為標準的請求,呼叫
server.respond(request, { getClientAddress })
函式以產生回應並以它回應 - 透過傳遞給
server.respond
的platform
選項,向 SvelteKit 公開任何平台特定的資訊 - 必要時,在目標平台上全面模擬
fetch
以使其正常運作。SvelteKit 提供一個@sveltejs/kit/node/polyfills
輔助程式,適用於可以使用undici
的平台
- 從
- 必要時,將輸出打包以避免需要在目標平台上安裝相依項
- 將使用者的靜態檔案和產生的 JS/CSS 放置在目標平台的正確位置
如果可行,我們建議將轉接器輸出放在 build/
目錄下,任何中間輸出則放在 .svelte-kit/[轉接器名稱]
下。