注:
根据教程:《How to Set Up a TypeScript Monorepo》配置[https://earthly.dev/blog/setup-typescript-monorepo/]
添加了 commitizen 功能
-
git commit
with commitizen/cz-cli - declare every folder inside
/packages
with apackage.json
file is considered a local package.(by add"workspaces"
property in/package.json
) - typescript
- ts-node
- eslint
- prettier
使用npm run cz命令替代git commit提交代码
mkdir in /packages:
- cd packages
- mkdir utils
initialize a package.json
file inside utils:
-
npm init --scope @monorepo --workspace ./packages/utils
-
confirm
package.json
own this content{ "name": "@monorepo/utils", "version": "1.0.0", "description": "The package containing some utility functions", "main": "build/index.js", "scripts": { "build": "tsc --build" } }
-
add tsconfig.json inside utils:
{ "compilerOptions": { "target": "es2022", "module": "commonjs", "moduleResolution": "node", "declaration": true, "strict": true, "incremental": true, "esModuleInterop": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true, "rootDir": "./src", "outDir": "./build", "composite": true } }
通过 --workspace
指明命令的工作路径,从而执行对应 package.json
中定义的script
npm run build --workspace ./packages/utils
{
"compilerOptions": {
"incremental": true,
"target": "es2022",
"module": "commonjs",
"declaration": true,
"strict": true,
"moduleResolution": "node",
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"rootDir": "./src",
"outDir": "./build"
},
"files": [],
"references": [
{
"path": "./packages/utils"
}
]
}
在根目录执行 tsc --build
命令时,编译器会通过全局 tsconfig.json
文件中的 references
字段中定义的路径找到所有可编译的独立项目,并编译
在packages/ui下使用packages/utils的库
npm install @monorepo/utils --workspace ./packages/ui
npm install moment --workspace ./packages/ui
npm install @monorepo/utils @monorepo/ui