在 monorepo 中共享打字稿库

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)

到目前为止我已经尝试过:

  • TypeScript项目引用,但似乎没有办法在shared-ts项目中具有依赖关系
  • "shared-ts": "../shared-ts"在 package.json 中,但它将共享 ts 复制到每个包的 node_modules 中,因此每次进行更改时我都必须重新运行yarn
  • yarn linkpostinstallerror 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 中运行它时遇到了麻烦。

有没有好的方法可以做到这一点?关于我可以尝试的其他事情有什么想法吗?

Muh*_*man 8

解决方案一:

\n

与勒纳

\n

您可以使用工作区和 Lerna

\n
yarn workspace & lerna\n
Run 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\n
Run Code Online (Sandbox Code Playgroud)\n

这是一个示例回购协议

\n

在这里你可以看到x-cli正在共享x-core

\n

解决方案2:

\n

没有勒纳

\n

您可以使用mtsl包,它使我们能够创建有形的符号链接。你可以全局安装这个包

\n
npm install -g mtsl\n
Run Code Online (Sandbox Code Playgroud)\n

然后你只需要开始在终端中分离这三个命令。

\n
yarn workspace & lerna\n
Run Code Online (Sandbox Code Playgroud)\n

注意不要阻止这三个观察者。package.json测试后,您可以从脚本中发出单个命令

\n


Say*_*Pal 6

您的用例可以使用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您都可以将其添加到dependenciesdevDependencies仅通过版本号而不是相对路径进行引用。

包括工作空间在内的所有节点模块都被提升到根目录,node_modules这就是为什么模块解析应该毫无摩擦地工作。


Liu*_*Lei -2

使用yarn workspace & lerna

这是一个monorepo-template示例