koo*_*osh 6 bazel bazel-rules-nodejs
我有一个项目,其中包含多个 JS 包并使用 Yarn 工作区进行组织:
\n<root>\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 WORKSPACE\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 package.json\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 workspaces\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 foo\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\x9c\xe2\x94\x80\xe2\x94\x80 BUILD.bazel\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 src\n \xe2\x94\x82\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 bar\n \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 package.json\n \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 BUILD.bazel\n \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 src\n\nRun Code Online (Sandbox Code Playgroud)\nFOO包依赖于BAR包,它的定义在FOO/package.json:
<root>\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 WORKSPACE\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 package.json\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 workspaces\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 foo\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\x9c\xe2\x94\x80\xe2\x94\x80 BUILD.bazel\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 src\n \xe2\x94\x82\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 bar\n \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 package.json\n \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 BUILD.bazel\n \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 src\n\nRun Code Online (Sandbox Code Playgroud)\nworkspaces/bar/BUILD.bazel看起来像这样
workspaces/foo/package.json\n\n{\n "name": "FOO",\n "dependencies": {\n "BAR": "link:../bar",\n }\nRun Code Online (Sandbox Code Playgroud)\n和这里workspaces/foo/BUILD.bazel
load("@build_bazel_rules_nodejs//:index.bzl", "js_library")\njs_library(\n name = "bar",\n package_name = "BAR",\n srcs = [\n "package.json",\n "index.js",\n ],\n visibility = ["//visibility:public"],\n)\nRun Code Online (Sandbox Code Playgroud)\n文件WORKSPACE包括:
yarn_install(\n name = "npm_foo",\n package_json = "//workspaces/foo:package.json",\n yarn_lock = "//workspaces/foo:yarn.lock",\n package_path = "workspaces/foo",\n strict_visibility = False,\n # links = {\n # "bar": "//workspaces/bar",\n # },\n # generate_local_modules_build_files = True,\n)\n\nyarn_install(\n name = "npm",\n package_json = "//:package.json",\n yarn_lock = "//:yarn.lock",\n)\n\nyarn_install(\n name = "npm_bar",\n package_json = "//workspaces/bar:package.json",\n yarn_lock = "//workspaces/bar:yarn.lock",\n package_path = "workspaces/bar",\n)\nRun Code Online (Sandbox Code Playgroud)\n完成所有这些设置后,我运行bazel build //workspaces/foo:foo但失败。\n我需要在 中配置links或generate_local_modules_build_files属性吗yarn_install?或者需要进行哪些更改才能使这些链接的包协同工作?
至于纱线工作区不支持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"],
)
Run Code Online (Sandbox Code Playgroud)
并且WORKSPACE文件只有一个yarn_install顶级规则,package.json因为嵌套工作区被视为self-managed:
yarn_install(
name = "npm",
package_json = "//:package.json",
yarn_lock = "//:yarn.lock",
)
Run Code Online (Sandbox Code Playgroud)