gpt4 book ai didi

javascript - 如何自动将相对导入路径重写为绝对路径?

转载 作者:行者123 更新时间:2023-12-05 07:17:36 25 4
gpt4 key购买 nike

我正在尝试将我的 javascript/typescript 项目中所有现有的相对路径重写为绝对路径。

例子:

import example from "../../example"

将被重写为

import example from "src/components/example" 

所以我正在寻找一个脚本或类似的脚本来转换所有这些导入。最好可以在预提交或类似的情况下将其作为 npm 脚本运行。

有办法吗?

最佳答案

看起来导入是为了静态分析而完成的,不能真正是动态的(Importing modules using ES6 syntax and dynamic path)。我想知道你是否可以在 tsconfig.json 中做一些事情来完成这个。在“compilerOptions: { ..., "paths": { "@components/": "src/components/"下。我不确定这会解决你的用例,但它可能是值得的试一试。所以你的导入看起来像:

从“@components/example”导入{example}

关于javascript - 如何自动将相对导入路径重写为绝对路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58727944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com