簡體   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