Cor*_* S. 32 javascript npm typescript react-native monorepo
我正在尝试设置一个包含 3 个共享一些库代码的服务的 monorepo。
这是目前的情况:
repo: web
pdf/
package.json
reference to shared-ts using github url
tsconfig.json
frontend/
package.json
reference to shared-ts using github url
tsconfig.json
Run Code Online (Sandbox Code Playgroud)
repo: mobile (react-native)
package.json
reference to shared-ts using github url
tsconfig.json
Run Code Online (Sandbox Code Playgroud)
repo: shared-ts
package.json
tsconfig.json
Run Code Online (Sandbox Code Playgroud)
这可行,但提交shared-ts、构建、更改哈希package.json并再次提交是一件痛苦的事情。
这就是我想要实现的目标:
repo: monorepo
pdf/
package.json
reference to ../shared-ts
tsconfig.json
frontend/
package.json
reference to ../shared-ts
tsconfig.json
mobile/
package.json
reference to ../shared-ts
tsconfig.json
shared-ts/
package.json
tsconfig.json
Run Code Online (Sandbox Code Playgroud)
到目前为止我已经尝试过:
"shared-ts": "../shared-ts"在 package.json 中,但它将共享 ts 复制到每个包的 node_modules 中,因此每次进行更改时我都必须重新运行yarnyarn link在postinstall:error TS2307: Cannot find module 'shared-ts' or its corresponding type declarations.postinstall直接使用with创建符号链接ln -s ../shared-ts/ node_modules/shared-ts/,但 TypeScript 似乎找不到该模块npm linkinpostinstall似乎是最有前途的,但它真的很慢,而且由于一些权限问题,我在 CI 中运行它时遇到了麻烦。有没有好的方法可以做到这一点?关于我可以尝试的其他事情有什么想法吗?
您可以使用工作区和 Lerna
\nyarn workspace & lerna\nRun Code Online (Sandbox Code Playgroud)\n\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 README.md\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 lerna.json\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 package.json\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 packages\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 pdf\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 package.json /* "shared-ts": "^1.0.0" */\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 src\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 frontend\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 package.json\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 src\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 mobile\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 package.json\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 src\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 shared-ts\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 package.json\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 src\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 tsconfig.json\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 yarn.lock\nRun Code Online (Sandbox Code Playgroud)\n这是一个示例回购协议
\n\n您可以使用mtsl包,它使我们能够创建有形的符号链接。你可以全局安装这个包
\nnpm install -g mtsl\nRun Code Online (Sandbox Code Playgroud)\n然后你只需要开始在终端中分离这三个命令。
\nyarn workspace & lerna\nRun Code Online (Sandbox Code Playgroud)\n注意不要阻止这三个观察者。package.json测试后,您可以从脚本中发出单个命令
您的用例可以使用npm7 工作区来处理。简而言之,您的新 monorepo 结构应如下所示:
repo: monorepo
package.json // <- here you define the workspaces
pdf/
package.json
reference to shared-ts
tsconfig.json
frontend/
package.json
reference to shared-ts
tsconfig.json
mobile/
package.json
reference to shared-ts
tsconfig.json
shared-ts/
package.json
tsconfig.json
Run Code Online (Sandbox Code Playgroud)
您需要在根目录中列出工作空间package.json,可能如下所示:
repo: monorepo
package.json // <- here you define the workspaces
pdf/
package.json
reference to shared-ts
tsconfig.json
frontend/
package.json
reference to shared-ts
tsconfig.json
mobile/
package.json
reference to shared-ts
tsconfig.json
shared-ts/
package.json
tsconfig.json
Run Code Online (Sandbox Code Playgroud)
完成此操作后,无论您决定在 monorepo 中的何处使用,shared-ts您都可以将其添加到dependencies或devDependencies仅通过版本号而不是相对路径进行引用。
包括工作空间在内的所有节点模块都被提升到根目录,node_modules这就是为什么模块解析应该毫无摩擦地工作。
| 归档时间: |
|
| 查看次数: |
14918 次 |
| 最近记录: |