typescript - Turborepo 导入别名
我正在为我正在开发的几个 TypeScript 项目设置一个 monorepo,并且在导入别名方面遇到了问题。我的目录结构如下
project/
apps/
streaming/
package.json
tsconfig.json
src/
handlers/
KafkaHandler.ts
packages/
shared-utils/
package.json
tsconfig.json
src/
clients/
Kafka.ts
tsconfig/
base.json
project/packages/tsconfig/base.json{
"$schema": "https://json.schemastore.org/tsconfig",
"display": "Default",
"compilerOptions": {
"baseUrl": ".",
"declaration": true,
"declarationMap": true,
"esModuleInterop": true,
"module": "CommonJS",
"resolveJsonModule": true,
"skipLibCheck": true,
"sourceMap": true,
"target": "ES2019",
"types": ["node"]
}
}
project/packages/shared-utils/tsconfig.json{
"extends": "tsconfig/base.json",
"compilerOptions": {
"isolatedModules": true,
"baseUrl": "dist/src",
"outDir": "dist"
},
"include": [
"src"
],
"exclude": [
"node_modules"
]
}
project/apps/streaming/tsconfig.json{
"extends": "tsconfig/base.json",
"compilerOptions": {
"outDir": "dist"
},
"exclude": [
"node_modules"
],
"include": [
"./src"
],
}
project/apps/streaming/package.json{
"author": "",
"dependencies": {
"dotenv": "^16.3.1",
"kafkajs": "^2.2.4",
"lodash": "^4.17.21",
"minimist": "^1.2.8",
"prompts": "^2.4.2",
"shared-utils": "workspace:*",
"tiny-typed-emitter": "^2.1.0",
"tsconfig": "workspace:*"
},
"description": "",
"devDependencies": {
"@types/lodash": "^4.14.199",
"@types/node": "^20.8.10",
"@types/prompts": "^2.4.6",
"typescript": "^5.2.2"
},
"license": "ISC",
"main": "./dist/index.ts",
"name": "streaming",
"packageManager": "[email ",
"scripts": {
"build": "tsc",
"clean": "rm -rf dist && rm -rf node_modules",
"clean_build": "pnpm run clean && pnpm i && pnpm run build",
"test": "echo \"Error: no test specified\" && exit 1",
"compile": "tsc"
},
"version": "1.0.0"
}
projects/packages/shared-utils/src/clients/Kafka.ts 有一个默认导出的模块,其中包含我用来与 Kafka 交互的方法。我希望能够在 project/apps/streaming 中使用如下路径导入该模块
import {Kafka} from 'shared-utils/clients'
/* OR */
import Kafka from 'shared-utils/clients/Kafka'
我已经看过关于 TypeScript 导入别名的几个来源,但很难得到一个明确的答案。目前,我在 src 的每个目录中都有 index.ts 文件,它们汇总到根目录的 index.ts 文件。这相当麻烦,因为导入需要两行代码
import {Clients} from 'shared-utils'
import Kafka = Clients.Kafka
在这一点上,我甚至不确定自己是否应该通过 Typescript、pnpm 或 Turborepo 来配置。我希望有人能指出需要更改哪些配置,以便让我以上述方式导入。谢谢!