如何使用 bazel/webpack 设置绝对导入?

How to setup absolute imports with bazel/webpack?

我想从我的仓库的一部分导入一个打字稿模块到另一部分,而不是在我的导入中有一堆“../..”以返回到我的 bazel 工作区的根文件夹。我如何为 webpack 设置绝对导入(相对于我的工作区)?我需要在 webpack.config.js 中设置什么吗?如果是,是什么?

说明以下问题的最小设置:

文件夹结构

folder1/
    moduleA.ts
    BUILD
folder2/
    moduleB.ts
    BUILD
WORKSPACE

工作空间

workspace(
    name = "myworkspace",
)

...

* **moduleA.ts ***

// TS COMPILER CAN FIND THIS BUT WEBPACK CAN'T FIND THIS
import { thing } from "myworkspace/folder2/moduleB";  

...

folder1/BUILD

load("@npm_bazel_typescript//:index.bzl", "ts_library")
load("@npm//webpack-cli:index.bzl", webpack = "webpack_cli")

ts_library(
    name = "moduleA",
    srcs = ["moduleA.ts"],
    deps = [
        "//folder2:moduleB",
    ],
)

filegroup(
    name = "moduleA.js",
    srcs = [
        "moduleA",
    ],
    output_group = "es6_sources",
    visibility = ["//visibility:public"],
)

webpack(
    name = "bundle",
    outs = ["app.bundle.js"],
    args = [
        "$(locations :moduleA.js)",
        "--config",
        "$(execpath //:webpack.config.js)",
        "-o",
        "$@",
    ],
    data = [
        ":moduleA.js",
        "//:webpack.config.js",
        "@npm//:node_modules",
    ],
    visibility = ["//visibility:public"],
)

已解决here

It turns out the issue is that filegroup does not infer from moduleA that //folder2:moduleB needs to be included transitively. Adding //folder2:moduleB to the filegroup srcs fixed my issue.