Bazel Typescript - 找不到 ts_library 被 module_name 引用

Bazel Typescript - Cannot find ts_library Referenced by module_name

我有两个 Bazel BUILD 文件:

我的目标是将 ts_library 从枚举导入服务器作为服务器 ts_library 的依赖项。因此,我使用了此处描述的方法:https://dev.to/lewish/building-a-typescript-monorepo-with-bazel-4o7n(通过 module_namedeps

枚举 BUILD 文件:

package(default_visibility = ["//visibility:public"])

load("@io_bazel_rules_docker//nodejs:image.bzl", "nodejs_image")
load("@npm_bazel_typescript//:index.bzl", "ts_library")

ts_library(
    name = "enums",
    srcs = glob(["src/index.ts"]),
    module_name = "@lbm/enums",
)

服务器BUILD文件:

load("@io_bazel_rules_docker//nodejs:image.bzl", "nodejs_image")
load("@npm_bazel_typescript//:index.bzl", "ts_library")

ts_library(
    name = "lib",
    srcs = glob(
        include = [ "**/*.ts" ],
        exclude = [ "src/index.ts" ],
    ),
    deps = ["//enums:enums"]
)

ts_library(
    name = "main",
    srcs = [ "src/index.ts" ],
    deps = ["//enums:enums", ":lib"],
)

filegroup(
    name = "libfiles",
    srcs = ["lib"],
    output_group = "es5_sources",
)

filegroup(
    name = "mainfile",
    srcs = ["main"],
    output_group = "es5_sources",
)

nodejs_image(
    name = "server",
    data = [
        ":libfiles",
        ":mainfile",
    ],
    entry_point = ":mainfile",
)

但是当运行

bazel run //server:server

尽管我设置了 module_name = "@lbm/enums" 并将 //enums 添加到 deps,但我收到此错误:

INFO: Analyzed target //server:server (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
ERROR: /minimal-bazel-monorepo/server/BUILD:13:1: Compiling TypeScript (devmode) //server:main failed (Exit 1)
server/src/index.ts:2:27 - error TS2307: Cannot find module '@lbm/enums'.

2 import { Constants } from '@lbm/enums';
                            ~~~~~~~~~~~~

Target //server:server failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 0.207s, Critical Path: 0.09s
INFO: 0 processes.

你可以自己试试:https://github.com/flolude/minimal-bazel-monorepo

编辑 #1

将导入语句从 import { Constants } from '@lbm/enums'; 更新为 import { Constants } from '@lbm/enums/src'; 后,如 Ray 所述,我收到此错误:

Error: Cannot find module '@lbm/enums/src/index'. Please verify that the package.json has a valid "main" entry
  1. 因为 BUILD 文件位于 /enums 目录而不是 /enums/src,正确的 TS 导入应该是这样的: import { Constants } from '@lbm/enums/src';
    或者考虑将此 BUILD 文件移动到 /enums/src 目录,然后 TS 导入现在可以保留。
  2. 我从示例中注意到您使用纱线工作区。不幸的是,rules_nodejs 目前不支持它们,因此最好在根目录中添加一个 package.json。