作者:文艺犯 | 来源:互联网 | 2022-11-24 22:53
我目前正在使用create-react-app
我的一个项目来引导它.基本上,我试图在tsconfig.json中设置路径,方法是将它们添加到create-react-app生成的默认tsconfig.json中:
"baseUrl": "./src",
"paths": {
"interfaces/*": [
"common/interfaces/*",
],
"components/*": [
"common/components/*",
],
},
但是,每次运行yarn start
基本上都运行时react-scripts start
,它会删除我的更改并再次生成默认配置.
如何告诉create-react-app使用我的自定义配置?
1> Glenn..:
通过使用来自本期的建议,我能够做到这一点。
放置配置选项,将react脚本喜欢的脚本删除到一个单独的文件(例如,paths.json)中,并通过extends指令从tsconfig.json中引用它。
path.json:
{
"compilerOptions": {
"baseUrl": "./src",
"paths": {
"interfaces/*": [ "common/interfaces/*"],
"components/*": [ "common/components/*"],
}
}
}
tsconfig.json
{
"extends": "./paths.json"
...rest of tsconfig.json
}
2> Microcipcip..:
Create React App当前不支持baseUrl
。但是,有一种解决方法...要baseUrl
同时为webpack和IDE 进行设置,您必须执行以下操作:
.env
使用以下代码创建文件:
NODE_PATH=./
创建一个tsconfig.paths.json
包含以下代码的文件:
{
"compilerOptions": {
"baseUrl": "src",
"paths": {
"src/*": ["*"]
}
}
}
将以下行添加到 tsconfig.json
{
"extends": "./tsconfig.paths.json",
...
}