Bazel 与 lerna 和纱线工作区一起使用

Bazel using with lerna and yarn workspace

许多人都在使用 lerna and/or yarn workspace。

我想要么从它们迁移到 Bazel,要么将它们与 Bazel 一起使用,最好用一个示例项目来指导。

例如,目前,我有这样的目录结构,其中 foo 是一个快速服务器,bar 是 foo 使用的库,两者都基于打字稿。

<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 中有一些与此有点相似的回购结构示例。这表明(在本例中为 Angular 应用程序)具有共享库并使用它们,但这里的原理是相同的。

通常,您的项目根目录中只有一个 WORKSPACE 文件。虽然不同的应用程序和库可以有多个 package.json 文件,但它会为 ts_library 规则增加一些额外的复杂性,对于入门来说,最好避免这种情况。 This example repo 显示多个 package.json 个文件,但没有 Typescript。

对于 BUILD(或 BUILD.bazel)文件,您在这里至少需要一个在 foo 中,一个在 bar 中(在根目录中一个).您拥有的 BUILD 文件越多,您为源代码拆分的编译单元就越多,因此增加了增量。

然后将 ts_library 规则添加到那些 BUILD 文件,文档可以找到 here,它们还显示了直接使用 tscts_library。然后,您可以定义 foobar 之间的源依赖关系,下面是一个快速示例:

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",
  ],
)