我有两个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

Ray所述,将导入语句从import { Constants } from '@lbm/enums';更新为import { Constants } from '@lbm/enums/src';后,出现以下错误:

Error: Cannot find module '@lbm/enums/src/index'. Please verify that the package.json has a valid "main" entry

最佳答案

由于BUILD文件位于/ enums目录中,而不位于/ enums / src中,因此正确的TS导入将如下所示:
import { Constants } from '@lbm/enums/src';
或者,也许考虑将这个BUILD文件移动到/ enums / src dir中,然后就可以保留TS导入了。
我从示例中注意到,您使用的是纱线工作区。不幸的是,rules_nodejs目前不支持它们,因此最好在根目录中包含一个package.json。

关于javascript - Bazel Typescript-找不到由module_name引用的ts_library,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59356289/

10-11 13:11