typescript 자동으로 컴파일 시키기

김민석·2021년 6월 25일
0

typescript

목록 보기
1/4
post-thumbnail

우선 tsconfig.json에 아래와 같이 include와 dist 설정을 해준다.

{
  "compilerOptions": {
    "target": "ES2021", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', or 'ESNEXT'. */
    "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
    "outDir": "./dist", /* Redirect output structure to the directory. */
    "strict": true, /* Enable all strict type-checking options. */
    "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
    "skipLibCheck": true, /* Skip type checking of declaration files. */
    "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
  },
  "include": [
    "src/**/*"
],
}

그 다음 이런식으로 package.json에 설정하면 된다.

  • tsc -w : watch option을 준 것이다. 이렇게 하면 include 안에 있는 디렉토리의 파일에 변경이 생길 때마다 js로 재변환한다. 👍
  • nodemon으로 dist/app.js를 실행하면 된다. (앞의 tsc는 없어도 될 것 같다.)

추가

  • include 설정 대신 outDir 같은 rootDir 설정이 존재한다.
  • ts-node 모듈을 이용하여 더 쉽게 할 수 있다.

0개의 댓글