antfu
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCoding Practices
编码实践
Code Organization
代码组织
- Single responsibility: Each source file should have a clear, focused scope/purpose
- Split large files: Break files when they become large or handle too many concerns
- Type separation: Always separate types and interfaces into or
types.tstypes/*.ts - Constants extraction: Move constants to a dedicated file
constants.ts
- 单一职责:每个源文件应具备清晰、明确的范围与用途
- 拆分大文件:当文件过大或涵盖过多职责时进行拆分
- 类型分离:始终将类型与接口分离到或
types.ts文件中types/*.ts - 常量提取:将常量移至专用的文件
constants.ts
Runtime Environment
运行时环境
- Prefer isomorphic code: Write runtime-agnostic code that works in Node, browser, and workers whenever possible
- Clear runtime indicators: When code is environment-specific, add a comment at the top of the file:
ts
// @env node
// @env browser- 优先同构代码:尽可能编写可在 Node、浏览器和 Worker 中运行的与运行时无关的代码
- 清晰的运行时标识:当代码针对特定环境时,在文件顶部添加注释:
ts
// @env node
// @env browserTypeScript
TypeScript
- Explicit return types: Declare return types explicitly when possible
- Avoid complex inline types: Extract complex types into dedicated or
typedeclarationsinterface
- 显式返回类型:尽可能显式声明返回类型
- 避免复杂内联类型:将复杂类型提取为专用的或
type声明interface
Comments
注释
- Avoid unnecessary comments: Code should be self-explanatory
- Explain "why" not "how": Comments should describe the reasoning or intent, not what the code does
- 避免不必要的注释:代码应具备自解释性
- 解释“原因”而非“方式”:注释应描述推理或意图,而非代码的执行逻辑
Testing (Vitest)
测试(Vitest)
- Test files: →
foo.ts(same directory)foo.test.ts - Use /
describeAPI (notit)test - Use for complex outputs
toMatchSnapshot - Use with explicit path for language-specific snapshots
toMatchFileSnapshot
- 测试文件:→
foo.ts(同一目录)foo.test.ts - 使用/
describeAPI(而非it)test - 对复杂输出使用
toMatchSnapshot - 对特定语言的快照,使用带明确路径的
toMatchFileSnapshot
Tooling Choices
工具选择
@antfu/ni Commands
@antfu/ni 命令
| Command | Description |
|---|---|
| Install dependencies |
| Add dependency / dev dependency |
| Run script |
| Upgrade dependencies |
| Uninstall dependency |
| Clean install ( |
| Execute package ( |
| 命令 | 描述 |
|---|---|
| 安装依赖 |
| 添加依赖 / 开发依赖 |
| 运行脚本 |
| 升级依赖 |
| 卸载依赖 |
| 干净安装( |
| 执行包( |
TypeScript Config
TypeScript 配置
json
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "bundler",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true
}
}json
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "bundler",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true
}
}ESLint Setup
ESLint 配置
js
// eslint.config.mjs
import antfu from '@antfu/eslint-config'
export default antfu()When completing tasks, run to format the code and fix coding style.
pnpm run lint --fixFor detailed configuration options: antfu-eslint-config
js
// eslint.config.mjs
import antfu from '@antfu/eslint-config'
export default antfu()完成任务后,运行来格式化代码并修复编码风格。
pnpm run lint --fix如需详细配置选项:antfu-eslint-config
Git Hooks
Git 钩子
json
{
"simple-git-hooks": {
"pre-commit": "pnpm i --frozen-lockfile --ignore-scripts --offline && npx lint-staged"
},
"lint-staged": { "*": "eslint --fix" },
"scripts": {
"prepare": "npx simple-git-hooks"
}
}json
{
"simple-git-hooks": {
"pre-commit": "pnpm i --frozen-lockfile --ignore-scripts --offline && npx lint-staged"
},
"lint-staged": { "*": "eslint --fix" },
"scripts": {
"prepare": "npx simple-git-hooks"
}
}pnpm Catalogs
pnpm 目录
Use named catalogs in for version management:
pnpm-workspace.yaml| Catalog | Purpose |
|---|---|
| Production dependencies |
| Bundler-inlined dependencies |
| Dev tools (linter, bundler, testing) |
| Frontend libraries |
Avoid the default catalog. Catalog names can be adjusted per project needs.
使用中的命名目录进行版本管理:
pnpm-workspace.yaml| 目录 | 用途 |
|---|---|
| 生产依赖 |
| 打包器内联依赖 |
| 开发工具(代码检查器、打包器、测试工具) |
| 前端库 |
避免使用默认目录。可根据项目需求调整目录名称。
References
参考资料
| Topic | Description | Reference |
|---|---|---|
| ESLint Config | Framework support, formatters, rule overrides, VS Code settings | antfu-eslint-config |
| Project Setup | .gitignore, GitHub Actions, VS Code extensions | setting-up |
| App Development | Vue/Nuxt/UnoCSS conventions and patterns | app-development |
| Library Development | tsdown bundling, pure ESM publishing | library-development |
| Monorepo | pnpm workspaces, centralized alias, Turborepo | monorepo |
| 主题 | 描述 | 参考链接 |
|---|---|---|
| ESLint 配置 | 框架支持、格式化工具、规则覆盖、VS Code 设置 | antfu-eslint-config |
| 项目搭建 | .gitignore、GitHub Actions、VS Code 扩展 | setting-up |
| 应用开发 | Vue/Nuxt/UnoCSS 规范与模式 | app-development |
| 库开发 | tsdown 打包、纯 ESM 发布 | library-development |
| 单仓多包(Monorepo) | pnpm 工作区、集中式别名、Turborepo | monorepo |