繁体   English   中英

Bazel Typescript - 找不到 module_name 引用的 ts_library

[英]Bazel Typescript - Cannot find ts_library Referenced by module_name

我有两个 Bazel BUILD文件:

  • 服务器/构建
  • 枚举/构建

我的目标是将ts_library从枚举导入服务器作为服务器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 语句 from 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。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM