---
title: "本機開發"
description: "在本機啟動 Calends 開發環境、常用指令、程式碼結構及提交前檢查項目。"
lang: "zh-HK"
version: "1.84.0"
url: "https://docs.calends.date/zh-hk/self-host/development/"
markdown_url: "https://docs.calends.date/zh-hk/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 開發環境、常用指令、程式碼結構及提交前檢查項目。

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

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

1. 安裝 Node.js 24，複製儲存庫並執行下方初始化指令。
2. 開啟本機應用；如需略過登入，依下方說明修改 `.dev.vars`。
3. 完成修改後執行對應檢查；提交前跑完下方的完整清單。

## 啟動

需要 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/     介面、登入（密碼與 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-hk/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-hk/self-host/cloudflare.md)
- [Docker](https://docs.calends.date/zh-hk/self-host/docker.md)
- [設定項](https://docs.calends.date/zh-hk/self-host/config.md)
- [自訂域名與 HTTPS](https://docs.calends.date/zh-hk/self-host/custom-domain.md)
- [升級](https://docs.calends.date/zh-hk/self-host/upgrade.md)
- [本機開發](https://docs.calends.date/zh-hk/self-host/development.md) *(current)*
- [參與翻譯](https://docs.calends.date/zh-hk/self-host/translate.md)

### Reading Order
- Previous: [升級](https://docs.calends.date/zh-hk/self-host/upgrade.md)
- Next: [參與翻譯](https://docs.calends.date/zh-hk/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-hk/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
