TypeScript - Turborepo インポートエイリアス
私はいくつかの作業中のTypeScriptプロジェクトのためにモノレポを設定しており、インポートエイリアスに問題があります。私のディレクトリ構造は次の通りです
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ファイルに集約されます。これはかなり面倒で、インポートには2行が必要です。
import {Clients} from 'shared-utils'
import Kafka = Clients.Kafka
現時点では、これをTypeScript、pnpm、あるいはTurborepoのどれを通じて設定すべきかさえ分かりません。誰かが上記のようにインポートできるようにするために、どの設定を変更する必要があるか教えてくれることを願っています。ありがとうございます!