使用 sv create my-app 通過 SvelteKit CLI 創建一個 SvelteKit 項目。根據提示選擇模板並設置開發環境。
sh
bunx sv create my-apptxt
┌ Welcome to the Svelte CLI! (v0.5.7)
│
◇ Which template would you like?
│ SvelteKit demo
│
◇ Add type checking with Typescript?
│ Yes, using Typescript syntax
│
◆ Project created
│
◇ What would you like to add to your project?
│ none
│
◇ Which package manager do you want to install dependencies with?
│ bun
│
◇ Successfully installed dependencies
│
◇ Project next steps ─────────────────────────────────────────────────────╮
│ │
│ 1: cd my-app │
│ 2: git init && git add -A && git commit -m "Initial commit" (optional) │
│ 3: bun run dev -- --open │
│ │
│ To close the dev server, hit Ctrl-C │
│ │
│ Stuck? Visit us at https://svelte.dev/chat │
│ │
├──────────────────────────────────────────────────────────────────────────╯
│
└ You're all set!項目初始化後,cd 進入新項目。你不需要運行 bun install,因為依賴已經安裝。
然後使用 bun --bun run dev 啟動開發服務器。
要使用 Node.js 而不是 Bun 運行開發服務器,你可以省略 --bun 標志。
sh
cd my-app
bun --bun run devtxt
$ vite dev
Forced re-optimization of dependencies
VITE v5.4.10 ready in 424 ms
➜ Local: http://localhost:5173/
➜ Network: use --host to expose
➜ press h + enter to show help在瀏覽器中訪問 http://localhost:5173 查看模板應用。
如果你編輯並保存 src/routes/+page.svelte,你應該會在瀏覽器中看到你的更改熱重載。
要為生產構建,你需要添加正確的 SvelteKit 適配器。目前我們推薦使用
bun add -D svelte-adapter-bun。
現在,對 svelte.config.js 進行以下更改。
js
import adapter from "@sveltejs/adapter-auto";
import adapter from "svelte-adapter-bun";
import { vitePreprocess } from "@sveltejs/vite-plugin-svelte";
/** @type {import('@sveltejs/kit').Config} */
const config = {
// 查閱 https://svelte.dev/docs/kit/integrations#preprocessors
// 獲取有關預處理器的更多信息
preprocess: vitePreprocess(),
kit: {
// adapter-auto 僅支持某些環境,請參閱 https://svelte.dev/docs/kit/adapter-auto 獲取列表。
// 如果你的環境不受支持,或者你已確定特定環境,請切換適配器。
// 請參閱 https://svelte.dev/docs/kit/adapters 獲取有關適配器的更多信息。
adapter: adapter(),
},
};
export default config;要構建生產包:
sh
bun --bun run buildtxt
$ vite build
vite v5.4.10 building SSR bundle for production...
"confetti" is imported from external module "@neoconfetti/svelte" but never used in "src/routes/sverdle/+page.svelte".
✓ 130 modules transformed.
vite v5.4.10 building for production...
✓ 148 modules transformed.
...
✓ built in 231ms
...
✓ built in 899ms
Run npm run preview to preview your production build locally.
> Using svelte-adapter-bun
✔ Start server with: bun ./build/index.js
✔ done