tscで特定ファイルのみ除外してビルドする方法
目次
背景
jestでjest-mock-extendedを使っているのですが、v2.0.7で変なエラーが出ることがありました。
https://github.com/marchaos/jest-mock-extended/issues/97
以下のようなエラー。
node_modules/jest-mock-extended/lib/Mock.d.ts:27:222
 - error TS2536: Type 'K_1' cannot be used to index type 'T[K]'.
以下略
v2.0.6では問題なく使えるそうです。
なので一つの方法としてv2.0.6にダウングレードするというのがあります。
ただ、今回はそもそも通常のビルド時にはテストファイルをビルドしないというやり方で解決することにしました。
やり方
tsconfigとtsconfig-exclude-test.jsonを作成
tsconfigのみでも問題なくできますが、諸事情でファイルを分けてます。
ファイルを分けるといっても、tsconfigをベースに追加したい項目をtsconfig-exclude-test.jsonに書いていくと言う流れになります。
extendsでベースになるtsconfig.jsonを指定します。
今回の目的である特定ファイルを含めないための項目がexcludeとなります。
テストファイルは、すべてspec.tsが含まれているので[“*/.spec.ts”]となっています。
カンマ区切りにすれば複数指定できます。
tsc -pとすることで、指定した設定ファイルを読み込みにいきます。
tsconfig.json
{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es6",
    "outDir": "dist",
    "rootDir": ".",
    "sourceMap": true,
    "strict": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "resolveJsonModule": true
  }
}
tsconfig-exclude-test.json
{
  "extends": "./tsconfig",
  "exclude": ["**/*.spec.ts"]
}
package.jsonのscriptsに-p tsconfig-exclude-test.jsonオプションをつける
  "scripts": {
    "build": "tsc -p tsconfig-exclude-test.json",
    "watch": "tsc -w",
    "prestart": "npm run build",
    "start": "concurrently npm:start:*",
    "start:tsc": "tsc -w --preserveWatchOutput -p tsconfig-exclude-test.json",
    "start:func": "nodemon --watch dist --delay 1 --exec \"func start -p 7072\"",
    "test": "jest"
  },




ディスカッション
コメント一覧
まだ、コメントがありません