很多人都在使用 lerna 和/或 yarn workspace。
我想要么从它们迁移到 Bazel,要么将它们与 Bazel 一起使用,最好通过示例项目进行指导。
例如,目前,我有一个这样的目录结构,其中 foo 是一个快速服务器,而 bar 是一个由 foo 使用的库,两者都基于 typescript 。
<project root>
├── jest.config.js
├── lerna.json
├── package.json
├── packages
│ ├── bar
│ │ ├── jest.config.js
│ │ ├── package.json
│ │ ├── src
│ │ │ └── index.ts
│ │ ├── test
│ │ │ └── unit
│ │ │ └── index.test.ts
│ │ ├── tsconfig.build.json
│ │ └── tsconfig.json
│ └── foo
│ ├── jest.config.js
│ ├── package.json
│ ├── src
│ │ ├── hello.ts
│ │ └── index.ts
│ ├── test
│ │ ├── integration
│ │ │ └── index.test.ts
│ │ └── unit
│ │ └── index.test.ts
│ ├── tsconfig.build.json
│ └── tsconfig.json
├── tsconfig.build.json
├── tsconfig.json
└── yarn.lock
我应该如何将它与 Bazel 对齐,如您所知,WORKSPACE、BUILD 及其内容?
有什么提示或示例吗?
谢谢!
最佳答案
在 rules_nodejs examples directory 中有一些与此有点相似的 repo 结构示例。 .这表明(在本例中为 Angular 应用程序)共享库并使用它们,但这里的原理是相同的。
通常,您的项目根目录中只有一个 WORKSPACE 文件。虽然不同的应用程序和库可以有多个 package.json
文件,但它会为 ts_library
规则增加一些额外的复杂性,对于入门来说,最好避免这种情况。 This example repo显示多个 package.json
文件,但没有 Typescript。
对于 BUILD
(或 BUILD.bazel
)文件,您在这里至少需要一个 foo
和一个 bar
(根部有一个)。您拥有的 BUILD 文件越多,您为源代码拆分的编译单元就越多,因此增加了增量。
然后将ts_library
规则添加到那些BUILD
文件中,可以找到其文档here , 它们还显示了直接使用 tsc
和 ts_library
之间的区别。然后,您可以定义 foo
和 bar
之间的源依赖关系,下面是一个快速示例:
packages/foo/BUILD
:
ts_libaray(
name = "foo",
srcs = glob(["src/**/*.ts"]),
deps = [
"//packages/bar", <-- this is the source dep for bar
"@npm//some-package",
],
)
packages/bar/BUILD
:
ts_libaray(
name = "bar",
srcs = glob(["src/**/*.ts"]),
deps = [
"@npm//some-other-package",
],
)
关于node.js - Bazel 与 lerna 和 yarn 工作区一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60352128/