简体   繁体   English

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

[英]How to setup absolute imports with bazel/webpack?

I want to import a typescript module from one part of my repo at another part without having a bunch of "../.." in my imports to get back to the root folder of my bazel workspace.我想从我的回购的一部分在另一部分导入一个 typescript 模块,而在我的导入中没有一堆“../..”以返回我的 bazel 工作区的根文件夹。 How can I setup absolute imports (relative to my workspace) for webpack?如何为 webpack 设置绝对导入(相对于我的工作区)? Do I need to set something in webpack.config.js?我需要在 webpack.config.js 中设置一些东西吗? If so, what?如果是这样,是什么?

Minimal setup to illustrate the issue below:用于说明以下问题的最小设置:

Folder structure文件夹结构

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

WORKSPACE工作空间

workspace(
    name = "myworkspace",
)

...

* ** moduleA.ts *** * **模块A.ts ***

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

...

folder1/BUILD文件夹 1/构建

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"],
)

Solved here .在这里解决。

It turns out the issue is that filegroup does not infer from moduleA that //folder2:moduleB needs to be included transitively.事实证明,问题在于文件组不会从 moduleA 推断出 //folder2:moduleB 需要被包含传递。 Adding //folder2:moduleB to the filegroup srcs fixed my issue.将 //folder2:moduleB 添加到文件组 srcs 解决了我的问题。

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

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