我想为我的导入添加一些前缀,就像你在下面的代码中看到的那样:
"paths": {
"~/*": ["../../libs/*"],
"@/*": ["./*"]
}
然而,当我尝试导入任何import User from "@/config.ts"
时,我总是得到一个relative import path "@/config.ts" not prefixed with / or ./ or ../ts(10001)
您可以使用导入映射对导入说明符进行别名。来自Deno手册:
您可以使用带有
--import-map=<FILE>
CLI标志的导入映射。示例:
import_map.json
{ "imports": { "fmt/": "https://deno.land/std@0.125.0/fmt/" } }
颜色.ts
import { red } from "fmt/colors.ts"; console.log(red("hello world"));
然后:
$ deno run --import-map=import_map.json color.ts
更新:以下是使用导入映射说明符导入本地模块的演示(正如Kamafheet在评论中所要求的(:
% ls -AF
import_map.json main.ts path/
% cat import_map.json
{
"imports": {
"local/": "./path/to/local/modules/"
}
}
% cat main.ts
import { shout } from "local/example.ts";
shout("hello world");
% ls -AF path/to/local/modules
example.ts
% cat path/to/local/modules/example.ts
export function shout(text: string): void {
console.log(text.toUpperCase());
}
% deno --version
deno 1.34.3 (release, aarch64-apple-darwin)
v8 11.5.150.2
typescript 5.0.4
% deno run --import-map=import_map.json main.ts
HELLO WORLD