Turborepo Import Aliases

230 views Asked by At

I'm setting up a monorepo for a couple Typescript projects I'm working on and I'm having issues with import aliasing. My directory structure is as follows

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 protected]",
  "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 has a default exported module which contains the methods I use to interact with Kafka. I'd like to be able to import the module in project/apps/streaming using a path like so

import {Kafka} from 'shared-utils/clients'

/* OR */

import Kafka from 'shared-utils/clients/Kafka'

I've seen several sources on typescript import aliasing, but it's difficult to get a straight answer on this. Currently I have index.ts files in each directory in src that bubble up to a root index.ts file. This is pretty painful because imports require 2 lines

import {Clients} from 'shared-utils'
import Kafka = Clients.Kafka

At this point I'm not even sure if I should be trying to configure this through Typescript, pnpm, or Turborepo. I'm hoping someone can point out what configuration needs to be changed to allow me to import in the way described above. Thanks!

0

There are 0 answers