构建你的应用
构建 SvelteKit 应用分为两个阶段,这两个阶段都在你运行 vite build
(通常通过 npm run build
)时发生。
¥Building a SvelteKit app happens in two stages, which both happen when you run vite build
(usually via npm run build
).
首先,Vite 会为你的服务器代码、浏览器代码和服务工作线程(如果有)创建一个优化的生产版本。如果合适,预渲染 在此阶段执行。
¥Firstly, Vite creates an optimized production build of your server code, your browser code, and your service worker (if you have one). Prerendering is executed at this stage, if appropriate.
其次,适配器会采用此生产版本并针对你的目标环境进行调整 - 有关更多信息,请参阅以下页面。
¥Secondly, an adapter takes this production build and tunes it for your target environment — more on this on the following pages.
构建期间(During the build)
¥During the build
SvelteKit 将在构建期间加载你的 +page/layout(.server).js
文件(以及它们导入的所有文件)以供分析。与 Svelte 集成的工具所需的任何其他选项。
¥SvelteKit will load your +page/layout(.server).js
files (and all files they import) for analysis during the build. Any code that should not be executed at this stage must check that building
from $app/environment
is false
:
import { const building: boolean
SvelteKit analyses your app during the build
step by running it. During this process, building
is true
. This also applies during prerendering.
building } from '$app/environment';
import { import setupMyDatabase
setupMyDatabase } from '$lib/server/database';
if (!const building: boolean
SvelteKit analyses your app during the build
step by running it. During this process, building
is true
. This also applies during prerendering.
building) {
import setupMyDatabase
setupMyDatabase();
}
export function function load(): void
load() {
// ...
}
预览你的应用(Preview your app)
¥Preview your app
构建后,你可以使用 vite preview
(通过 npm run preview
)在本地查看生产构建。请注意,这将在 Node 中运行应用,因此不是你部署的应用的完美复制品 - 适配器特定的调整(如 platform
对象)不适用于预览。
¥After building, you can view your production build locally with vite preview
(via npm run preview
). Note that this will run the app in Node, and so is not a perfect reproduction of your deployed app — adapter-specific adjustments like the platform
object do not apply to previews.