Cod*_*ern 16 typescript reactjs react-scripts create-react-app
我目前正在使用create-react-app我的一个项目来引导它.基本上,我试图在tsconfig.json中设置路径,方法是将它们添加到create-react-app生成的默认tsconfig.json中:
"baseUrl": "./src",
"paths": {
"interfaces/*": [
"common/interfaces/*",
],
"components/*": [
"common/components/*",
],
},
Run Code Online (Sandbox Code Playgroud)
但是,每次运行yarn start基本上都运行时react-scripts start,它会删除我的更改并再次生成默认配置.
如何告诉create-react-app使用我的自定义配置?
Gle*_*enn 10
放置配置选项,将react脚本喜欢的脚本删除到一个单独的文件(例如,paths.json)中,并通过extends指令从tsconfig.json中引用它。
path.json:
{
"compilerOptions": {
"baseUrl": "./src",
"paths": {
"interfaces/*": [ "common/interfaces/*"],
"components/*": [ "common/components/*"],
}
}
}
Run Code Online (Sandbox Code Playgroud)
tsconfig.json
{
"extends": "./paths.json"
...rest of tsconfig.json
}
Run Code Online (Sandbox Code Playgroud)
你不能,我不确定你什么时候可以。我一直在尝试使用 baseUrl 和路径,这样我就可以避免相对导入,但正如您所看到的,它们是故意删除某些值。“(还)”令人鼓舞,但(叹气)谁知道他们什么时候会正式支持它。我建议订阅这个 github问题,以便在发生变化时/当它发生变化时收到警报。
The following changes are being made to your tsconfig.json file:
- compilerOptions.baseUrl must not be set (absolute imports are not supported (yet))
- compilerOptions.paths must not be set (aliased imports are not supported)
Run Code Online (Sandbox Code Playgroud)
Create React App当前不支持baseUrl。但是,有一种解决方法...要baseUrl同时为webpack和IDE 进行设置,您必须执行以下操作:
.env使用以下代码创建文件:NODE_PATH=./
Run Code Online (Sandbox Code Playgroud)
tsconfig.paths.json包含以下代码的文件:{
"compilerOptions": {
"baseUrl": "src",
"paths": {
"src/*": ["*"]
}
}
}
Run Code Online (Sandbox Code Playgroud)
tsconfig.json{
"extends": "./tsconfig.paths.json",
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5002 次 |
| 最近记录: |