TypeScript: TypeScript Compiler Ep.1

Compilation Context


타입스크립트를 자바스크립트로 변환하는 프로그램이 타입스크립트 컴파일러이다.
이 타입스크립트 컴파일러가 어떤 파일과 어떤 방식으로 변환할 것인지 규명해 둔 맥락이 compilation context이다.
이러한 것들은 tsconfig.json파일에 적혀있다.

tsconfig schema


tsconfig.json의 전체적인 스키마(구조)를 보는 사이트 링크

최상위 프로퍼티

  • compileOnSave
  • extends
  • compileOptions → 어떤 설정으로 컴파일 할 것인지
  • files, include, exclude → 어떤 파일을 컴파일 할 것인지 결정
  • reference
  • typeAcquisition
  • tsNode

compileOnSave


파일을 저장하면 컴파일 하겠다는 설정이다.

"compileOnSaveDefinition": {
	"properties": {
		"compileOnSave": {
			"description": "Enable Compile-on-Save for this project.",
			"type": "boolean"
		}
	}
}
  • true 혹은 false로 default값은 false이다.
  • true로 켰을때 누가 컴파일을 해주냐? → Visual Studio 2015 with TypeScript 1.8.4 이상 혹은 atom-typescript 플러그인
  • vsc의 경우 ‘저장할때 뭐를 한다’라는 기능이 추가적으로 들어있기 때문에 그닥 중요한 옵션은 아니다.

extends


상속할 때 사용하는 키워드로 tsconfig 파일도 다른 파일을 상속 받고 그 안에 추가를 해서 사용하는 방법이 있다.

"extendsDefinition": {
	"properties": {
		"extends": {
			"description": "Path to base configuration file to inherit from. Requires TypeScript version 2.1 or later.",
			"type": "string"
		}
	}
}
  • 파일 (상대) 경로명: string
  • TypeScript 2.1 이상에서만 사용이 가능
    • in Project/base.json
      {
        "compilerOptions": {
          "strict": true
        }
      }
      
    • in project/tsconfig.json
      {
        "extends": "./base.json"
      }
      

files, include, exclude


어떤 파일을 컴파일 할 것인지 결정한다.

"filesDefinition": {
	"properties": {
		"files": {
			"description": "If no 'files' or 'include' property is present in a tsconfig.json, the compiler defaults to including all files in the containing directory and subdirectories except those specified by 'exclude'. When a 'files' property is specified, only those files and those specified by 'include' are included.",
			"type": "array",
			"uniqueItems": true,
			"items": {
				"type": "string"
			}
		}
	}
}
"excludeDefinition": {
	"properties": {
		"exclude": {
			"description": "Specifies a list of files to be excluded from compilation. The 'exclude' property only affects the files included via the 'include' property and not the 'files' property. Glob patterns require TypeScript version 2.0 or later.",
			"type": "array",
			"uniqueItems": true,
			"items": {
				"type": "string"
			}
		}
	}
}
"includeDefinition": {
	"properties": {
		"include": {
			"description": "Specifies a list of glob patterns that match files to be included in compilation. If no 'files' or 'include' property is present in a tsconfig.json, the compiler defaults to including all files in the containing directory and subdirectories except those specified by 'exclude'. Requires TypeScript version 2.0 or later.",
			"type": "array",
			"uniqueItems": true,
			"items": {
				"type": "string"
			}
		}
	}
}
  • 셋 다 설정이 없으면, 전부다 컴파일한다.
  • files
    • 상대 혹은 절대 경로의 리스트 배열이다.
    • exclude 보다 강하다. 그렇기에 적혀있다면 무조건 포함된다.
  • include, exclued
    • glob 패턴 (.gitignore 같은 형식)
    • include
      • exclude 보다 약하다. (include 안에 있는 것 중에 exclude에 해당하는 것이 있으면 제외된다.)
      • “*” 같은 걸 사용하면 .ts/ .tsx/ .d.ts 만 include한다. 만약 해당 파일들 말고도 js파일도 같이 하고 싶으면 allowJS 옵션을 켜야된다.
    • exclude
      • 설정을 안 하면 4가지(node_modules, bower_components, jspm_packages, )를 default로 제외한다.
      • 은 include가 있어도 항상 제외한다.

Comments