본문 바로가기
IT/typescript

[typescript] pnpm monorepo에서 paths 설정하기

by 내일은교양왕 2024. 7. 15.

코드

아래의 설명만으로는 부족하다.

https://github.com/insidedw/monorepo-pnpm-ts

 

GitHub - insidedw/monorepo-pnpm-ts

Contribute to insidedw/monorepo-pnpm-ts development by creating an account on GitHub.

github.com


 

packages 구조

project
 - apps
   - calculator
     - src
 - libs
   - shared
     - src

 

root에 tsconfig.json 추가

baseUrl 기준으로 path를 지정

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@S_shared/*": ["libs/shared/src/*"],
      "@cal/*": ["apps/calculator/src/*"]
    }
  }
}

 

calculator sub pacakage에 tsconfig.json 추가

확장을 해서 가져와야 baseUrl이 변경되어 paths에서 선언한 값이 잘 먹힌다 

{
  "extends": "../../tsconfig.json",
}

 

calculator package에서 현 설정 값 확인

npx tsc --showConfig

{
    "compilerOptions": {
        "baseUrl": "../..",
        "paths": {
            "@S_shared/*": [
                "libs/shared/src/*"
            ],
            "@cal/*": [
                "apps/calculator/src/*"
            ]
        }
    },
    "files": [
        "./src/hello.ts",
        "./src/main.ts"
    ]
}

 

 

 

calculator에 tsconfig-paths devDeps 추가

pnpm add -D tsconfig-paths

 

main.ts 추가

import {getLength} from "@cal/hello";
import {add} from '@S_shared/utils'

const a = add(3, 5)
console.log('a', a)


const b = getLength('dddd')
console.log('b', b)

 

실행

ts-node -r tsconfig-paths/register ./src/main.ts

 

결과

a 8
b 4

 

 

https://blog.spiralmoon.dev/entry/Typescript-Module-%EC%A0%88%EB%8C%80%EA%B2%BD%EB%A1%9C-%EC%84%A4%EC%A0%95-%EB%B0%A9%EB%B2%95

 

[Typescript] Module 절대경로 설정 방법

Module 절대경로 설정 방법 타입스크립트 프로젝트에서 소스코드 관리를 위한 절대경로 설정 방법을 알아보자. 절대경로를 사용하는 이유 타입스크립트 프로젝트는 기본적으로 상대경로를 통해

blog.spiralmoon.dev

 

'IT > typescript' 카테고리의 다른 글

[typescript] 함수타입(Function Type) 인자형  (0) 2024.08.14
[typescript] subtype  (0) 2024.08.14
[typescript] is  (1) 2024.06.12
[typescript] in  (0) 2024.06.12
[typescript] as const  (0) 2024.06.03