| 方案 | 適用編輯器 | 配置複雜度 | 特點 |
|---|---|---|---|
| Prettier (推薦) | VS Code, WebStorm, Sublime, Vim, CLI | 低 (零配置可用) | 有觀點、統一風格、支援 HTML/CSS/JS/TS/JSON/MD/GraphQL/Vue/Svelte/Astro |
| VS Code 內建格式化 | VS Code | 零 | 無需安裝、但選項少、不支援模板語法 (Vue/Svelte) |
| HTMLHint + Stylelint | 所有 | 中 | 側重 **檢查規則** 而非格式化,配合 Prettier 使用 |
| Brackets Beautify / 擴展管理器 | Brackets | 低 | 圖形化安裝、但 Brackets 已停止維護 (2021 年歸檔) |
// .prettierrc.json (項目根目錄)
{
"printWidth": 120, // 單行最大字符數
"tabWidth": 2, // 縮進 2 空格
"useTabs": false,
"singleQuote": true, // JS 單引號,HTML 屬性雙引號 (Prettier 固定)
"bracketSameLine": false, // > 換行
"htmlWhitespaceSensitivity": "css", // 嚴格保留 CSS white-space 影響的空白
"endOfLine": "lf", // 換行符 LF
"embeddedLanguageFormatting": "auto" // 格式化內嵌 CSS/JS
}
// .prettierignore
dist/
build/
*.min.html
public/
// .vscode/settings.json (提交倉庫,團隊共享)
{
"editor.formatOnSave": true, // 保存自動格式化
"editor.defaultFormatter": "esbenp.prettier-vscode", // 指定 Prettier
"[html]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"html.format.wrapLineLength": 120, // 內建格式化備選
"html.format.enable": false // 關閉內建,避免衝突
}
# .github/workflows/format.yml
name: Format Check
on: [push, pull_request]
jobs:
prettier:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with: { node-version: '20' }
- run: npm ci
- run: npx prettier --check .
遷移建議:Brackets 官方已歸檔,建議遷移至 VS Code + Prettier,生態更活躍、長期維護有保障。
**Prettier + VS Code formatOnSave + CI 攔截** 是現代前端項目的標準化鐵三角。拋棄手動格式化、拋棄各自為政的編輯器配置,用一份 `.prettierrc` 治理全項目 HTML/CSS/JS/TS/模板語言格式。