@
superhot 我的项目架构 (
https://ppresume.com) 大概是这样的:
- ppresume
- packages/common
- packages/api
- packages/client
```sh
$ ls -l packages package.json pnpm-workspace.yaml pnpm-lock.yaml
-rw-r--r-- 1 hanyu staff 1485 Jan 14 21:09 package.json
-rw-r--r-- 1 hanyu staff 744184 Dec 30 10:49 pnpm-lock.yaml
-rw-r--r-- 1 hanyu staff 27 Jan 22 2024 pnpm-workspace.yaml
packages:
total 0
drwxr-xr-x 16 hanyu staff 512 Jan 14 21:09 api
drwxr-xr-x 24 hanyu staff 768 Jan 14 21:09 client
drwxr-xr-x 12 hanyu staff 384 Jan 14 21:09 common
```
- api: 后端 API
- client: 前端 next.js 实现
- common: 前后端共享的 data model/schema validation/utils 等等,不依赖 node.js 系统 API ,所以可以同时跑在两端上。
回复你的问题:
1. tsconfig 我没有特别配置过,common package 的 tsconfig:
```json
{
"compilerOptions": {
"esModuleInterop": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"types": ["node", "jest"],
"importHelpers": true,
"composite": true,
"target": "es5",
"rootDir": "src",
"outDir": "dist",
"baseUrl": "."
},
"include": ["**/*.ts", "**/*.json"]
}
```
2. 把 common package 当成一个单独的 npm/pnpm 包引入 api/client ,不需要用嵌套路径。npm/pnpm/yarn 都有相应的 workspace 特性,我最开始用的是 yarn ,后来转到了 pnpm ,参考:
https://pnpm.io/workspaces我的 pnpm-workspace 配置:
```
packages:
- 'packages/*'
```
顶层 `package.json` 配置:
```
{
"name": "ppresume",
"private": true,
"version": "0.7.0",
"dependencies": {
"concurrently": "^8.2.1",
"husky": "^8.0.2",
"lint-staged": "^13.1.0",
"prettier": "^2.8.1"
},
"devDependencies": {
"@commitlint/cli": "^19.3.0",
"@commitlint/config-conventional": "^19.2.2",
"@trivago/prettier-plugin-sort-imports": "^4.0.0",
"commit-and-tag-version": "^12.4.0"
},
"lint-staged": {
"**/*": "prettier --write --ignore-unknown"
},
"resolutions": {
"jackspeak": "2.1.1"
},
"scripts": {
"api": "pnpm --filter api",
"common": "pnpm --filter common",
"coverage": "pnpm common build && concurrently --kill-others-on-fail \"pnpm common coverage\" \"pnpm client coverage\" \"pnpm api coverage\"",
"client": "pnpm --filter client",
"build": "pnpm common build && concurrently --kill-others-on-fail \"pnpm client build\" \"pnpm api build\"",
"dev": "pnpm common build && concurrently --kill-others-on-fail \"pnpm client dev\" \"pnpm api dev\"",
"start": "pnpm common build && concurrently --kill-others-on-fail \"pnpm client start\" \"pnpm api start\"",
"test": "pnpm common build && concurrently --kill-others-on-fail \"pnpm common test\" \"pnpm client test\" \"pnpm api test\"",
"release": "commit-and-tag-version --bumpFiles package.json packages/*/package.json",
"commitlint": "commitlint --edit"
},
"commit-and-tag-version": {
"writerOpts": {
"commitsSort": false
}
}
}
```
`common/package.json` 的配置:
```
{
"name": "common",
"version": "0.7.0",
"description": "common code shared by ppresume project",
"author": "Xiao Hanyu",
"private": true,
"devDependencies": {
"@types/jest": "^29.5.5",
"@types/lodash": "^4.14.202",
"@types/node": "^20.5.7",
"jest": "^29.7.0",
"ts-jest": "^29.1.1",
"typescript": "^5.2.2"
},
"main": "dist/index",
"types": "dist/index",
"scripts": {
"build": "tsc --build --force",
"coverage": "jest --coverage",
"test": "jest"
},
"dependencies": {
"escape-latex": "^1.2.0",
"tslib": "^2.6.2"
}
}
```
调用 `pnpm common build` 来完成 `common` package 的 build ,然后在 api/client 里直接 `import xxx from 'common'` 就可以了
3. mono-repo 有 mono-repo 的好处的,最大的好处是出了 regression 问题可以用 `git bisect` 找 bug ,commit log 的维护重点还是靠个人/团队守规则。
我个人项目的概况:
https://x.com/hanyuxiao1988/status/1878625079829172510以上