英文:
Using non '.ts' / '.tsx' files in 'ts_library' as dependencies
问题
I'm trying to import a json file from typescript (yes, I'm using the resolveJsonModule
flag in my tsconfig). The issue is, I don't know how can I give this json file to ts_library
(this is also valid for any other non .ts
& .tsx
files, like a .env
). ts_library
is always telling me that he cannot find my json file.
For example if I have this ts_library
in a BUILD.bazel
:
ts_library(
name = "src",
srcs = glob(["*.ts"]),
deps = [
"@npm//:node_modules",
]
)
with this index.ts
:
import test from './test.json';
console.log(test);
and this test.json
:
{
"foo": "bar"
}
it will throw me this:
index.ts:1:18 - error TS2307: Cannot find module './test.json'.
I think I need to somehow add the json file in the deps
of my rule. But I don't know how to do it, because deps doesn't accept direct files like //:test.json
.
英文:
I'm trying to import a json file from typescript (yes, I'm using the resolveJsonModule
flag in my tsconfig). The issue is, I don't know how can I give this json file to ts_library
(this is also valid for any other non .ts
& .tsx
files, like a .env
). ts_library
is always telling me that he cannot find my json file.
For example if I have this ts_library
in a BUILD.bazel
:
ts_library(
name = "src",
srcs = glob(["*.ts"]),
deps = [
"@npm//:node_modules",
]
)
with this index.ts
:
import test from './test.json';
console.log(test);
and this test.json
:
{
"foo": "bar"
}
it will throw me this:
index.ts:1:18 - error TS2307: Cannot find module './test.json'.
I think I need to somehow add the json file in the deps
of my rule. But I don't know how to do it, because deps doesn't accept direct files like //:test.json
.
答案1
得分: 6
你可以通过 data
属性向 ts_library
规则添加文件(https://www.npmjs.com/package/@bazel/typescript#data)。这是您可以添加对 JSON 文件的引用的地方。
然而,ts_library
覆盖了输出模块格式,并且 resolveJsonModule
只能与 commonjs、amd、es2015 或 esNext 一起使用(ts_library
将其覆盖为 UMD)。
选项 '--resolveJsonModule' 只能在模块代码生成为 'commonjs'、'amd'、'es2015' 或 'esNext' 时指定。
如果您直接通过 Bazel 使用 TypeScript 编译器,而不是通过 ts_library
,您可以这样做:
load("@npm//typescript:index.bzl", "tsc")
srcs = glob(["*.ts"])
deps = ["@npm//@types/node"]
data = ["test.json"]
tsc(
name = "src",
data = srcs + deps + data,
outs = [s.replace(".ts", ext) for ext in [".js", ".d.ts"] for s in srcs],
args = [
"--outDir",
"$(RULEDIR)",
"--lib",
"es2017,dom",
"--downlevelIteration",
"--declaration",
"--resolveJsonModule",
] + [
"$(location %s)" % s
for s in srcs
],
)
您可以使用 nodejs_binary
来测试它是否有效:
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary")
nodejs_binary(
name = "src_bin",
data = [
":src",
] + data,
entry_point = "//src:index.ts",
)
有关使用 tsc
而不是 ts_library
的更多信息,请参阅此链接:
https://github.com/bazelbuild/rules_nodejs/blob/master/packages/typescript/docs/install.md
英文:
You can add files to the ts_library
rule via the data
attribute (https://www.npmjs.com/package/@bazel/typescript#data). This is where you can add the reference to the JSON file.
However, ts_library overrides the output module format, and resolveJsonModule
can only be used with commonjs, amd, es2015 or esNext (ts_library overrides it to UMD)
Option '--resolveJsonModule' can only be specified when module code generation is 'commonjs', 'amd', 'es2015' or 'esNext'.
You can do this though if you use the typescript compiler directly via Bazel, rather than through ts_library:
load("@npm//typescript:index.bzl", "tsc")
srcs = glob(["*.ts"])
deps = ["@npm//@types/node"]
data = ["test.json"]
tsc(
name = "src",
data = srcs + deps + data,
outs = [s.replace(".ts", ext) for ext in [".js", ".d.ts"] for s in srcs],
args = [
"--outDir",
"$(RULEDIR)",
"--lib",
"es2017,dom",
"--downlevelIteration",
"--declaration",
"--resolveJsonModule",
] + [
"$(location %s)" % s
for s in srcs
],
)
You can test that it works by using nodejs_binary
:
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary")
nodejs_binary(
name = "src_bin",
data = [
":src",
] + data,
entry_point = "//src:index.ts",
)
More information about this using tsc
rather than ts_library
here:
https://github.com/bazelbuild/rules_nodejs/blob/master/packages/typescript/docs/install.md
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论