该项目使用Yarn,React Native,Lerna和Typescript。它被构造为monorepo
这里是结构:
project
|- packages
| - mobile
| - src
| - packages.json
| - tsconfig.json
| - cloud-functions
| - src
| - packages.json
| - tsconfig.json
| - common1
| - lib
| - src
| - packages.json
| - tsconfig.json
| - common2
| - lib
| - src
| - packages.json
| - tsconfig.json
| - packages.json
| - tsconfig.json
| - lerna.json
lerna.json看起来像这样:
{
"packages": [
"packages/*"
],
"npmClient": "yarn",
"version": "0.0.7",
}
根packages.json看起来像这样:
{
"name": "project",
"private": true,
"scripts": {
...
},
"devDependencies": {
"@types/node": "^14.0.27",
"lerna": "^3.22.1",
"ts-node": "^8.10.2",
"typescript": "^3.9.7"
}
}
tsts.json根目录如下:
{
"compilerOptions": {
"noImplicitAny": true,
"noUnusedLocals": true,
"removeComments": true,
"noLib": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"baseUrl": "./",
"paths": {
"@project/common1": ["packages/common1/lib"],
"@project/common2": ["packages/common2/lib"],
"@project/mobile": ["packages/mobile/src"],
"@project/cloud-functions": ["packages/cloud-functions/src"],
}
},
"exclude": ["node_modules", "**/*.spec.ts", "**/__tests__/*", "babel.config.js", "metro.config.js", "jest.config.js"]
}
典型的packages / common / packages.json看起来像这样:
{
"name": "@project/common1",
"version": "0.0.7",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"files": [
"lib/**/*"
],
"private": true,
"devDependencies": {
"@project/common2": "latest", //for common1 only
"@types/node": "^14.0.27",
"ts-node": "^8.10.2",
"typescript": "^3.9.7"
},
"dependencies": {
...
}
}
典型的packages / common / tsconfig.json看起来像这样:
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "lib",
"strict": true,
"target": "es6"
},
"compileOnSave": true,
"include": ["src"]
}
React Native文件package / mobile / packages.json看起来像这样:
{
"name": "@project/mobile",
"version": "0.0.7",
"private": true,
"dependencies": {
"@project/common1": "latest",
"@project/common2": "latest",
...
},
"devDependencies": {
...
"ts-node": "^8.10.2",
"typescript": "^3.8.3"
},
}
我首先遇到:
lerna ERR! yarn install --mutex network:42424 --non-interactive stderr:
warning Waiting for the other yarn instance to finish (19560)
warning Waiting for the other yarn instance to finish (21568)
error An unexpected error occurred: "https://registry.yarnpkg.com/@project%2fcommon1: Not found".
很明显,Yarn试图从其包中提取依赖项。这会失败。
然后,我尝试在程序包的依赖项中删除对@ project / common1和@ project / common2的引用。
在源代码中,VS Code用红色和打印件在导入内容下划线:
Cannot find module '@project/common1' or its corresponding type declarations.ts(2307)
我还尝试使用Yarn Workspace,但是遇到了React Native提升模块的问题。我不想创建所有可能不兼容的软件包的列表,因为它似乎很难维护。
"workspaces": {
"nohoist": ["react-native", "react-native/**", "@react-native-community/checkbox", "@react-navigation/native"]
}
有一个简单的解决方案吗?
或者这个使用案例放弃Lerna并使用基于GitHub的通用存储库是否更简单?
答案 0 :(得分:0)
我不知道这是否是最简单的方法,但是我可以通过添加Yarn工作区来使其工作。
在主packages.json中,我添加了:
"workspaces": {
"packages": [
"packages/*"
],
"nohoist": [
"**mobile**",
"**react**",
"**react-native**",
...
]
},
在Lerna.json中,我添加了:
"useWorkspaces": true,
从根tsconfig.json中删除:
"baseUrl": "./",
"paths": {
"@project/common1": ["packages/common1/lib"],
"@project/common2": ["packages/common2/lib"],
"@project/mobile": ["packages/mobile/src"],
"@project/cloud-functions": ["packages/cloud-functions/src"],
}