티스토리 뷰

문제

npm run build 를 하면 원하지 않는 디렉토리나 파일이 같이 dist 에 export됨

 

 

시도 및 해결

build할 때 exclude나 include 등으로 지정하지 않아서 생긴 것으로 파악

다른 유사한 레포지토리를 참고하여 build용 tsconfig를 따로 만들고 build script를 수정하기

 

 

tsconfig.build.json

빌드할 때 exclude나 include 에 필요한 디렉토리 및 파일만 지정

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "noImplicitAny": false,
    "removeComments": true,
    "noLib": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es6",
    "sourceMap": false,
    "outDir": "./dist",
    "rootDir": "./lib",
    "skipLibCheck": true
  },
  "include": [
    "lib/**/*",
    "../index.ts"
  ],
  "exclude": [
    "node_modules",
    "**/*.spec.ts"
  ]
}

cf. tsconfig.json은 build와 다르게 경로를 지정해줘야 함

 

package.json

script 에 tsc 로 컴파일할 때 --project (-p) 로 tsconfig 파일을 지정해 줌

만약 -p 옵션을 사용하지 않으면 자동으로 tsconfig.json으로 설정 됨

{
  ...
  "scripts": {
    "build": "rimraf dist && tsc -p tsconfig.build.json",
   ...

 

 

 

참고자료

Typescript Compile Option Official Documentation

댓글