【发布时间】:2020-04-20 00:47:33
【问题描述】:
我正在使用create-react-app + typescript,我想添加绝对路径。
我试图达到我可以使用绝对路径的目的,如下所示:
而不是import x from '../../../components/shell/shell'
使用import x from '@components/shell/shell';
这里是 tsconfig.json 文件:
{
"extends": "./paths.json",
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"baseUrl": "src",
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react"
},
"include": [
"src"
]
}
我使用扩展文件作为路径,因为出于某种原因npm start 会覆盖该文件。
paths.json 文件也是如此:
{
"compilerOptions": {
"baseUrl": "./src",
"paths": {
"@": ["./src"],
"@components/*": ["components/*]"
}
}
}
我还有一个 .env 文件:
NODE_PATH=src
我安装了react-app-rewire,所以我可以配置路径,
我的 config-ovverrides.js 文件看起来像这样:
module.exports = {
paths: function(paths, env) {
// ...add your paths config
return paths;
}
};
我坚持连接所有点,它不起作用,我仍然看不到我需要做什么才能配置 webpack 路径对象;
如何在 cra、ts 和 rewire 中实现路径?
【问题讨论】: