craco-alias https://github.com/risenforces/craco-alias#examples
yarn add craco-alias -D
- 项目根目录创建 tsconfig.extend.json
配置文件如下
{
"compilerOptions": {
"baseUrl": "./src",
"paths": {
// file aliases 使用index.tsx的写法
"@baz": ["./baz.ts"],
"@boo": ["./boo.tsx"],
// folder aliases 使用别名下的模块的写法
"@root": ["./"],
"@root/*": ["./*"],
"@lib": ["./lib"],
"@lib/*": ["./lib/*"],
// package aliases
"@my-react-select": [
"../node_modules/react-select",
"../node_modules/@types/react-select"
],
"@my-react-select/*": [
"../node_modules/react-select/*",
"../node_modules/@types/react-select"
]
}
}
}
tsconfig.json,extends ```json {
- “extends”: “./tsconfig.extend.json”, “compilerOptions”: { … }, } ```
- craco.config.js ```json const CracoAlias = require(“craco-alias”);
module.exports = { plugins: [ { plugin: CracoAlias, options: { source: “tsconfig”, // baseUrl SHOULD be specified // plugin does not take it from tsconfig baseUrl: “./src”, // tsConfigPath should point to the file where “baseUrl” and “paths” are specified tsConfigPath: “./tsconfig.extend.json” } } ] } ```