gpt4 book ai didi

swc not resolving import path aliases(SWC未解析导入路径别名)

转载 作者:bug小助手 更新时间:2023-10-25 23:06:49 46 4
gpt4 key购买 nike



I have a typescript project which I'm transpiling with swc. I'm having trouble getting swc to resolve path aliases defined in either tsconfig.json or .swcrc. The module aliases are resolved properly by eslint and when running ts-node when swc is not specified as the compiler.

我有一个打字项目,我正在与SWC交换。我在让SWC解析tsfig.json或.swcrc中定义的路径别名时遇到了问题。当未将SWC指定为编译器时,通过eslint以及在运行ts-node时正确解析模块别名。


Given this pull request, it seems that swc is both supposed to resolve tsconfig.json and has support for path aliases defined in .swcrc. However, it seems people experienced similar issues to mine following this feature, though it was two years ago and I can't imagine there hasn't been a solution since.

对于这个拉入请求,SWC似乎既应该解析tsconfig.json,又支持在.swcrc中定义的路径别名。然而,似乎人们在使用这个功能后遇到了类似的问题,尽管这是两年前的事情了,我无法想象从那以后就没有解决方案了。


Also on a weird sort of side-note the author of the PR which implements TSConfigResolver that I linked earlier mentions in an issue a year after his PR was merged that swc "doesn't look at tsconfig". Though this shouldn't be the source of my problem though because I'm specifying the same path aliases in .swcrc

还有一个奇怪的侧面--注意到实现TSConfigResolver的PR的作者在他的PR合并一年后的一个问题上提到,SWC“不考虑tsconfig”。尽管这不应该是我问题的根源,因为我在.swcrc中指定了相同的路径别名


The swc documentation is horribly vague and doesn't mention anything about module aliases or resolving tsconfig, besides that jsc.baseUrl and jsc.paths are compilation options and they match tsconfig syntax.

SWC文档含糊其辞,没有提到任何关于模块别名或解析tsconfig的内容,此外,jsc.base Url和jsc.Path是编译选项,它们与tsconfig语法匹配。


Any help or guidance would be appreciated, thank you!

如有任何帮助或指导,我们将不胜感激,谢谢!


tsconfig.json

Tsconfig.json


{
"compilerOptions": {
"target": "es2020",
"module": "ESNext",
"moduleResolution": "node",
"baseUrl": "./",
"paths": {
"$lib/*": ["src/lib/*"]
},
"outDir": "./dist",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
},
"ts-node": {
"swc": true
},
"exclude": ["node_modules", "dist"]
}

.swcrc

.swcrc


{
"$schema": "https://json.schemastore.org/swcrc",
"jsc": {
"baseUrl": "./",
"paths": {
"$lib/*": ["src/lib/*"]
}
},
"module": {
"type": "es6"
}
}

package.json

Package.json


{
"type": "module",
"devDependencies": {
"@swc/cli": "^0.1.62",
"@swc/core": "^1.3.35",
"ts-node": "^10.9.1",
"typescript": "^4.9.5"
}
}

I receive the following error when running swc . -d ./dist from the project root directory:

运行SWC时收到以下错误。-d./dist从项目根目录:


CustomError: Cannot find package '$lib' imported from /root/projects/example/src/index.ts

CustomError:找不到从/ROOT/Projects/Example/src/index.ts导入的包‘$lib’


更多回答

did you have any luck finding an answer to this? I’m experiencing the same issue

你找到这个问题的答案了吗?我也遇到了同样的问题

nothing yet unfortunately. I suspect it's a bug with swc's module resolution, or maybe I'm just misusing the config but the docs are not helpful atm. Apparently people have been experiencing similar issues for a long time. It might be worth a new github issue.

遗憾的是,目前还什么都没有。我怀疑这是SWC模块解析的错误,或者可能我只是误用了配置,但文档对ATM没有帮助。显然,人们经历类似的问题已经很长一段时间了。这可能值得一次新的GitHub发行。

I experienced a similar issue. I also have my code inside src and transpiling into dist. In my tsconfig.json file im using this config: "baseUrl": "./src", "paths": { "@cli/*": ["./cli/*"],}. In .swcrc on the other side i need to specify the baseUrl where the transpilen code goes to. In my case dist. This gives me following config for .swcrc: "baseUrl": "dist", "paths": { "@cli/*": ["./cli/*"],}. I hope this helps you in any case.

我也遇到过类似的问题。我还将我的代码放在src中,并将其转换为dist。在我的tsconfig.json文件中,im使用这个配置:“base Url”:“./src”,“路径”:{“@cli/*”:[“./cli/*”],}。在另一端的.swcrc中,我需要指定Transpilen代码要转到的BasUrl。在我的情况下,迪斯特。这为我提供了以下.swcrc的配置:“base Url”:“dist”,“路径”:{“@cli/*”:[“./cli/*”],}。我希望这对你有任何帮助。

github.com/swc-project/swc-node/pull/666 apparently tsconfig paths is supported hrm

Gihub.com/swc-project/swc-node/Pull/666显然支持tsconfig路径

优秀答案推荐

It was fixed in recent pull request.
Make sure you specified your paths in both tsconfig.json and .swcrc
https://github.com/swc-project/swc-node/pull/723

它已在最近的拉取请求中修复。确保您在tsfig.json和.swcrc https://github.com/swc-project/swc-node/pull/723中都指定了路径



To resolve Cannot find module when building typescript projects with SWC, you will have to add your baseUrl and paths to your .swcrc file as well.

要解决在使用SWC构建TypeScrip项目时找不到模块的问题,您还必须将BasUrl和路径添加到.swcrc文件中。


For eg:

例如:


tsconfig.json

Tsconfig.json


"baseUrl": "./",
"paths": {
"*": ["node_modules", "src/*"]
},

This configuration from the tsconfig.json file should be also added to your swcrc file's jsc block.

来自tsconfig.json文件的此配置也应添加到SWCRC文件的JSC块中。


jwcrc

JWCRC


"jsc": {
"parser": {
"syntax": "typescript"
},
"baseUrl": "./",
"paths": {
"*": ["node_modules", "src/*"]
}
},

更多回答

This helped me. I set ./src in my .swcrc file and then same in tsconfig.json ,"baseUrl": "./src". It is workign when I run the app. It shows some terminal erros, but works fine. But If I try to npm run build, it fails and throws a large number of error. One of them is failed to handle: base_dir(./src) must be absolute. Please ensure that 'jsc.baseUrl' is specified correctly. This cannot be deduced by SWC itself because SWC is a transpiler and it does not try to resolve project details. In other works, SWC does not know which directory should be used as a base directory. Help!.

这对我很有帮助。我在我的.swcrc文件中设置了./src,然后在tsfig.json中设置了./src,“BasUrl”:“./src”。当我运行这个应用程序时,它是有效的。它显示了一些终端错误,但工作正常。但是,如果我尝试NPM运行构建,它将失败并抛出大量错误。其中一个无法处理:base_dir(./src)必须是绝对的。请确保正确指定了‘jsc.base Url’。这不能由SWC本身推断,因为SWC是一个转移器,它不试图解决项目细节。在其他工作中,SWC不知道应该将哪个目录用作基目录。救命!。

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