You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bazel/NodeJS问题:nodejs_binary引用其他规则未按预期生效

解决Bazel中Parcel引用ts_project共享代码的问题

问题原因

ts_project的composite模式目标默认仅暴露编译后的产物(JS、d.ts、.tsbuildinfo文件),而Parcel打包时可能需要访问原始TS源码或完整的文件结构,直接将//common:common加入parcel的data依赖时,Bazel不会自动把common的源码文件复制到沙箱,导致Parcel找不到文件。

解决方案

方案1:通过filegroup暴露完整的common文件

在common目录的BUILD文件中添加一个filegroup,包含TS源码和ts_project的编译产物,让Parcel能访问到所有需要的文件:

# common/BUILD
ts_project(
    name = "common",
    srcs = glob(
        ["**/*.ts"],
        exclude = ["**/*.spec.ts"],
    ),
    composite = True,
    tsconfig = "//:tsconfig_node",
    visibility = ["//visibility:public"],
    deps = [
        "@npm//@types/node",
    ],
)

# 新增filegroup,暴露源码和编译产物
filegroup(
    name = "common_files",
    srcs = glob(["**/*.ts"], exclude = ["**/*.spec.ts"]) + [":common"],
    visibility = ["//visibility:public"],
)

然后修改parcel规则的data依赖,引用这个filegroup:

load("@npm//parcel:index.bzl", "parcel")

parcel(
    name = "build",
    data = ui_srcs + [
        "//:tsconfig.json",
        "//common:common_files",  # 替换为filegroup目标
        "@npm//:node_modules",
        ".parcelrc",
        ".terserrc",
    ],
    output_dir = True,
)

方案2:确保TS配置的路径映射正确

在项目根目录的tsconfig.json中添加路径映射,让Parcel能正确解析common模块的导入路径:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@common/*": ["common/*"]
    }
  },
  "include": ["common/**/*", "ui/**/*"]
}

这样Parcel在编译时能通过路径映射找到common的文件,配合方案1的filegroup依赖,就能确保沙箱中存在所需文件。

方案3:验证ts_project的输出文件

如果Parcel仅需要编译后的JS产物,可以检查ts_project的输出是否包含所需文件。执行以下命令查看该目标的输出文件:

bazel query //common:common --output=files

确认是否包含Parcel需要的JS文件,如果缺少,可能需要调整ts_project的out_dir或root_dir属性,确保产物路径符合Parcel的预期。

内容的提问来源于stack exchange,提问作者user910046

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 05:45:29