---
title: "本機開發"
description: "在本機啟動 Calends 開發環境、常用指令、程式碼結構與提交 PR 前的檢查清單。"
lang: "zh-TW"
version: "1.84.0"
url: "https://docs.calends.date/zh-tw/self-host/development/"
markdown_url: "https://docs.calends.date/zh-tw/self-host/development.md"
section: "自託管"
llms_index: "https://docs.calends.date/llms.txt"
llms_full: "https://docs.calends.date/llms-full.txt"
agents_guide: "https://calends.date/AGENTS.md"
---

# 本機開發

> 在本機啟動 Calends 開發環境、常用指令、程式碼結構與提交 PR 前的檢查清單。

- Canonical page: https://docs.calends.date/zh-tw/self-host/development/
- Markdown source: https://docs.calends.date/zh-tw/self-host/development.md
- Calends version: v1.84.0

準備修改 Calends 原始碼時，請依本頁步驟啟動開發環境並執行相關檢查。

1. 安裝 Node.js 24，複製儲存庫並執行下方初始化指令。
2. 開啟本機應用；若開發過程需略過登入驗證，請依下文說明調整 `.dev.vars`。
3. 完成修改後執行對應檢查；發起 Pull Request 前請跑完下方完整清單。

## 啟動

環境需求為 Node.js 24。

```bash
git clone https://github.com/imetn/calends && cd calends
npm ci
npm run db:migrate:local
npm run dev
```

若欲在本機略過登入驗證，請於 `.dev.vars` 補上 `DEV_BYPASS_AUTH=1`，並保留檔案內既有內容。

`npm run dev` 會同時啟動兩個行程：Vite（前端熱重載，監聽 `0.0.0.0:5173`，支援區域網路與 Tailscale `*.ts.net` 存取）與 wrangler（Worker 與本機 D1，監聽 `:8787`），並內建 Worker 健康監控，埠號失聯時會自動重啟 Worker 而無須重啟 Vite。本機瀏覽器請開啟 `http://127.0.0.1:5173`。

本機資料庫儲存在儲存庫根目錄的 `.wrangler/state`。

> **DANGER**: `DEV_BYPASS_AUTH=1` 會完全略過登入驗證，只能寫在本機的 `.dev.vars` 中，絕對不可配置於正式環境。

## 常用指令

| 指令 | 作用 |
| --- | --- |
| `npm test` | 執行單元測試（計費、階段、指標、共享方案、分類等） |
| `npm run typecheck` | TypeScript 型別檢查 |
| `npm run lint` | oxlint 程式碼檢查 |
| `npm run format` / `npm run format:check` | Prettier 格式化 / 格式檢查 |
| `npm run i18n:check` | 檢查每種語言是否齊備所有字串鍵 |
| `npm run build` | 正式環境建置 |
| `npm run email:preview` | 使用範例資料將各種提醒郵件渲染為 HTML 輸出至 `.preview/email/`，不實際寄出 |

## 程式碼結構

```text
app/src/    應用介面與公開頁
shared/     計費、階段、指標、匯率、遮蔽、多語言，瀏覽器與 Worker 共用
worker/     API、驗證（密碼與 Passkey）、MCP、提醒與通知管道、備份、ICS
worker/migrations/ D1 / SQLite 資料庫結構
docker/     自託管映像檔入口
```

技術架構：應用前端採用 React 19（開啟 React Compiler）與 Vite，彈窗、選單與開關等互動元件基於 HeroUI / React Aria；公開頁採用 shadcn/ui；伺服器端為 Cloudflare Workers + D1。

## 提交之前

```bash
npm run typecheck && npm run lint && npm run format:check && npm run i18n:check && npm test && npm run build
```

幾項基本約定（詳見儲存庫內的 `CONTRIBUTING.md`）：

- **領域邏輯統一放在 `shared/`**，必須能同時於瀏覽器與 Worker 執行，不可依賴 DOM 或 Node API。若修改了計費、階段或指標邏輯，請在 `shared/*.test.ts` 補上單元測試。
- **所有使用者可見文字一律透過 `t()` 取得**，切勿於模組頂層呼叫 `t()`。新增字串至少須同時補齊 `en` 與 `zh-CN`。
- **維持歷史資料相容性。** 舊紀錄必須持續正常讀取；資料表結構變更須於 `worker/migrations/` 新增遷移檔；新資料欄位須能透過[備份](https://docs.calends.date/zh-tw/data/backup.md)完整往返還原。
- **隱私邊界**：任何足以辨識個人或帳號的資訊都不可出現在公開頁，`shared/mask.ts` 以白名單嚴格控制公開欄位。
- 維持介面與既有元件風格一致，並確認手機寬度（390px）、桌面版、淺色與深色主題皆顯示正常。

測試匯入、恢復與遷移時，請使用隔離的 `.wrangler/state` 與虛構資料。線上示範站為獨立部署，並非某道 `npm run demo` 灌入資料指令。更動子系統前，請先閱讀產品儲存庫的 `AGENTS.md`、`ARCHITECTURE.md` 與 `CONTRIBUTING.md`。

---

## Related Pages & Global AI Discovery

To explore broader context beyond this page, fetch any of the Markdown or plain-text resources below directly:

### Same Section (自託管)
- [Cloudflare Workers + D1](https://docs.calends.date/zh-tw/self-host/cloudflare.md)
- [Docker](https://docs.calends.date/zh-tw/self-host/docker.md)
- [設定項](https://docs.calends.date/zh-tw/self-host/config.md)
- [自訂網域與 HTTPS](https://docs.calends.date/zh-tw/self-host/custom-domain.md)
- [升級](https://docs.calends.date/zh-tw/self-host/upgrade.md)
- [本機開發](https://docs.calends.date/zh-tw/self-host/development.md) *(current)*
- [參與翻譯](https://docs.calends.date/zh-tw/self-host/translate.md)

### Reading Order
- Previous: [升級](https://docs.calends.date/zh-tw/self-host/upgrade.md)
- Next: [參與翻譯](https://docs.calends.date/zh-tw/self-host/translate.md)

### Global Calends Resources
- Read-only Docs MCP Server (zero auth Streamable HTTP): https://docs.calends.date/mcp
- Complete Docs Index (all 37 topics with `.md` links): https://docs.calends.date/llms.txt
- Complete English Docs Bundle (single file): https://docs.calends.date/llms-full.txt
- Complete Simplified Chinese Docs Bundle: https://docs.calends.date/llms-zh.txt
- Docs AI Agent Guide: https://docs.calends.date/AGENTS.md
- Product Website Overview (Markdown): https://calends.date/zh-tw/index.md
- Product LLMs Index & Full Reference: https://calends.date/llms.txt | https://calends.date/llms-full.txt
- Deployment & MCP Agent Guide (AGENTS.md): https://calends.date/AGENTS.md
- FAQ & Calends vs Wallos Comparison (Markdown): https://calends.date/en/faq.md | https://calends.date/en/compare.md
- Complete Changelog (Markdown): https://calends.date/en/releases.md
