【问题标题】:Bazel: build js_library with another linked JS dependency (yarn workspaces)Bazel:使用另一个链接的 JS 依赖项(纱线工作区)构建 js_library
【发布时间】:2021-09-02 05:04:29
【问题描述】:

我有一个项目,其中包含多个 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,但它失败了。 我需要在yarn_install 中配置links 或generate_local_modules_build_files 属性吗?或者需要哪些更改才能让这些链接的包协同工作?

【问题讨论】:

  • 你找到这个问题的答案了吗?我被困在同一件事上

标签: bazel bazel-rules-nodejs


【解决方案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 文件对于顶级package.json 只有一个yarn_install 规则,因为嵌套工作空间被视为self-managed:

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

【讨论】:

  • 谢谢 Koorosh jan!你是使用 CRA 来运行你的 React 应用还是从头开始创建你的 React 应用。另外,有两个package.json 文件有什么需要? stackoverflow.com/questions/70522738/…
  • - 它仅使用 webpack 构建(无 CRA)- 有树 package.json 文件:一个顶级 - 主要用于 Yarn 工作区(root/package.json),然后在每个工作区(root/workspaces/foo/package.json 和 @ 987654334@)
猜你喜欢
  • 2018-07-18
  • 2020-02-01
  • 2021-04-13
  • 2021-09-06
  • 2018-04-06
  • 2018-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多