5

我有一个项目,其中包含几个 JS 包并使用 Yarn 工作区进行组织:

<root>
├── WORKSPACE
├── package.json
├── workspaces
│   ├── foo
│   │   ├── package.json
│   │   ├── BUILD.bazel
│   │   ├── src
    │
    ├── bar
    │   ├── package.json
    │   ├── BUILD.bazel
    │   ├── src

FOO包取决于BAR包,它定义在FOO/package.json

workspaces/foo/package.json

{
  "name": "FOO",
  "dependencies": {
     "BAR": "link:../bar",
  }

workspaces/bar/BUILD.bazel看起来像这样

load("@build_bazel_rules_nodejs//:index.bzl", "js_library")
js_library(
    name = "bar",
    package_name = "BAR",
    srcs = [
        "package.json",
        "index.js",
    ],
    visibility = ["//visibility:public"],
)

和这里workspaces/foo/BUILD.bazel

load("@build_bazel_rules_nodejs//:index.bzl", "js_library")

js_library(
    name = "foo",
    package_name = "FOO",
    srcs = [
        "package.json",
        "index.js",
    ],
    deps = [
         "//workspaces/bar:bar", 
         "@npm//:node_modules"    # <-- this line causes an error because linked package couldn't be found in node_modules
    ], 
    visibility = ["//visibility:public"],
)

文件WORKSPACE包括:

yarn_install(
    name = "npm_foo",
    package_json = "//workspaces/foo:package.json",
    yarn_lock = "//workspaces/foo:yarn.lock",
    package_path = "workspaces/foo",
    strict_visibility = False,
    # links = {
    #     "bar": "//workspaces/bar",
    # },
    # generate_local_modules_build_files = True,
)

yarn_install(
    name = "npm",
    package_json = "//:package.json",
    yarn_lock = "//:yarn.lock",
)

yarn_install(
    name = "npm_bar",
    package_json = "//workspaces/bar:package.json",
    yarn_lock = "//workspaces/bar:yarn.lock",
    package_path = "workspaces/bar",
)

通过所有这些设置,我运行bazel build //workspaces/foo:foo但它失败了。我需要配置linksgenerate_local_modules_build_files属性yarn_install吗?或者让这个链接的包一起工作需要什么改变?

4

1 回答 1

1

至于纱线工作区不受支持rules_nodejs,以下解决方法对我有用:

这里修改workspaces/foo/BUILD.bazel

load("@build_bazel_rules_nodejs//:index.bzl", "js_library")

# this list contains all dependencies from the top level `package.json` file
# do not include linked `bar` package!
DEPENDENCIES = [
  "@npm//react",
  "@npm//react-dom",
  ...
]

# node_modules filegroup includes all dependencies from workspaces/foo/node_modules
filegroup(
    name = "node_modules",
    srcs = glob(
        include = [
            "node_modules/**",
        ],
    ),
)

js_library(
    name = "foo",
    package_name = "FOO",
    srcs = [
        "package.json",
        "index.js",
    ],
    deps = [
         "//workspaces/bar:bar", 
         ":node_modules"
    ] + DEPENDENCIES, 
    visibility = ["//visibility:public"],
)

并且WORKSPACE文件只有一个yarn_install顶级规则,package.json因为嵌套工作空间被视为self-managed

yarn_install(
    name = "npm",
    package_json = "//:package.json",
    yarn_lock = "//:yarn.lock",
)
于 2022-01-02T07:39:09.970 回答