tsconfig.json の incremental
概要
incremental を true にするとコンパイル時間を短縮できることです。
incremental を有効化すると、以前のコンパイルから変更があった差分のみを今回のコンパイル対象とします。差分を把握するために必要な情報を、指定された出力フォルダに .tsbuildinfo ファイルが作成されます。JavaScript 実行時には、.tsbuildinfo は使用されないため、削除しても問題ありません。
注意点として、tsconfig.json に変更があった場合は、前回差分だけでは最新の tsconfig.json に準拠しない可能性があります。よって、tsconfig.json に変更があった場合は、.tsbuildinfo を削除します。
Next.js の設定考察
create next-app で作成した Next.js プロジェクトで incremental は true が設定されています。Next.js で型チェックの時間を短縮化させるために incremental を有効化することをお勧めします。
{
  "compilerOptions": {
    "incremental": true,
  },
}
参考
公式の説明はこちらです。
以下が作業リポジトリです。
この記事の内容
この記事では incremental が false の場合と true の場合で動作を確認します。Node.js & TypeScript のプロジェクトと Next.js のプロジェクトで動作確認を行います。
Node.js & TypeScriptのプロジェクトで動作確認
TypeScript の簡易プロジェクトを作成し、incremental が false の場合と true の場合で動作を確認します。
事前環境の構築
動作を作業するための Node.js & TypeScript のプロジェクトを作成します。長いので、折り畳んでおきます。
新規プロジェクト作成と初期環境構築の手順詳細
TypeScript の簡易プロジェクトを作成します。
まず、package.json を作成します。
$ mkdir -p tsconfig-incremental
$ cd tsconfig-incremental
$ pnpm init
下記で package.json を上書きします。ポイントは scripts に 3 つのスクリプトを追加しています。typecheck で型をチェックし、dev でローカルで動作確認、build でトランスパイルします。
{
  "name": "tsconfig-incremental",
  "version": "1.0.0",
  "description": "",
  "main": "index.ts",
  "scripts": {
    "typecheck": "tsc --noEmit",
    "dev": "ts-node index.ts",
    "build": "tsc"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}
TypeScript をインストールします。
$ pnpm install -D typescript ts-node
tsconfig.json を作成します。
$ npx tsc --init
tsconfig.json を上書きします。
{
  "compilerOptions": {
    "target": "es2015",
    "module": "commonjs",
    "sourceMap": true,
    "outDir": "./dist",
    "strict": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "esModuleInterop":true
  },
  "include": ["**/*.ts","**/*.js"],
  "exclude": ["node_modules", "dist"]
}
git を初期化します。
$ git init
.gitignore を作成します。
$ touch .gitignore
node_modules
コミットします。
$ git add .
$ git commit -m "feat:初期コミット"
 incremental が false の場合
tsconfig.json を作成します。incremental を無効化します。
{
  "compilerOptions": {
    "target": "es2015",
    "module": "commonjs",
    "sourceMap": true,
    "outDir": "./dist",
    "strict": true,
    "skipLibCheck": true,
+   "incremental": false,
    "forceConsistentCasingInFileNames": true,
    "esModuleInterop":true
  },
  "include": ["**/*.ts","**/*.js"],
  "exclude": ["node_modules", "dist"]
}
コードを作成します。
$ touch index.ts
const message: string = "Hello, TypeScript!";
console.log(message);
ローカルで動作確認します。
$ pnpm run dev
Hello, TypeScript!
型チェックをします。エラーは出ません。time コマンドを利用し、typecheck スクリプトを実行することで、incremental が有効化されている場合の処理時間を計測します。
$ time pnpm run typecheck
pnpm run typecheck  1.52s user 0.15s system 127% cpu 1.304 total
.tsbuildinfo は作成されていません。
$ ls .tsbuildinfo
ls: .tsbuildinfo: No such file or directory
コミットします。
$ git add .
$ git commit -m "feat: incrementalを無効化"
 incremental が true の場合
incremental を有効化すると、以前のコンパイルから変更があった差分のみを今回のコンパイル対象とします。差分を把握するために必要な情報を、指定された出力フォルダに .tsbuildinfo ファイルが作成されます。JavaScript 実行時には、.tsbuildinfo は使用されないため、削除しても問題ありません。
incremental を有効化します。
{
  "compilerOptions": {
    "target": "es2015",
    "module": "commonjs",
    "sourceMap": true,
    "outDir": "./dist",
    "strict": true,
    "skipLibCheck": true,
-   "incremental": false,
+   "incremental": true,
    "forceConsistentCasingInFileNames": true,
    "esModuleInterop":true
  },
  "include": ["**/*.ts","**/*.js"],
  "exclude": ["node_modules", "dist"]
}
型チェックします。time コマンドを利用し、typecheck スクリプトを実行することで、incremental が有効化されている場合の処理時間を計測します。すると、.tsbuildinfo が作成されます。
$ time pnpm run typecheck
pnpm run typecheck  1.59s user 0.16s system 123% cpu 1.411 total
$ ls dist/tsconfig.tsbuildinfo
dist/tsconfig.tsbuildinfo
改めて型チェックします。.tsbuildinfo が作成されているため、処理時間が短縮されていることが確認できます。
$ time pnpm run typecheck
pnpm run typecheck  1.44s user 0.13s system 130% cpu 1.204 total
1 回目は 1.411 秒、2 回目は 1.204 秒でした。incremental が有効化されている場合、処理時間が短縮されていることが確認できました。
コミットします。
$ git add .
$ git commit -m "feat: incrementalを有効化"
 incremental が true の場合に注意する点
incremental が true の場合の注意点があります。注意点として、tsconfig.json に変更があった場合は、前回差分だけでは最新の tsconfig.json に準拠しない可能性があります。よって、tsconfig.json に変更があった場合は、.tsbuildinfo を削除します。
.tsbuildinfo を削除せずに、tsconfig.json を変更してみます。
{
  "compilerOptions": {
    "target": "es2015",
    "module": "commonjs",
    "sourceMap": true,
    "outDir": "./dist",
    "strict": true,
    "skipLibCheck": true,
    "incremental": true,
+   "noEmit": false,
    "forceConsistentCasingInFileNames": true,
    "esModuleInterop":true
  },
  "include": ["**/*.ts","**/*.js"],
  "exclude": ["node_modules", "dist"]
}
一旦、設定を記録した .tsbuildinfo を削除します。
$ rm -rf dist/tsconfig.tsbuildinfo
ビルドします。dist に tsconfig.tsbuildinfo が作成されました。noEmit を false に設定しているので、トランスパイルの結果として index.js は dist に出力されます。
$ pnpm run build
$ tree dist -I node_modules
dist
├── index.js
├── index.js.map
└── tsconfig.tsbuildinfo
1 directory, 3 files
コミットします。
$ git add .
$ git commit -m "feat: noEmitをfalseに変更"
noEmit を true に変更します。
{
  "compilerOptions": {
    "target": "es2015",
    "module": "commonjs",
    "sourceMap": true,
    "outDir": "./dist",
    "strict": true,
    "skipLibCheck": true,
    "incremental": true,
-   "noEmit": false,
+   "noEmit": true,
    "forceConsistentCasingInFileNames": true,
    "esModuleInterop":true
  },
  "include": ["**/*.ts","**/*.js"],
  "exclude": ["node_modules", "dist"]
}
index.js を削除します。
$ rm -rf dist/index.js dist/index.js.map
ビルドします。noEmit を true に設定しているので、index.js は出力されません。
$ pnpm run build
$ tree dist
dist
└── tsconfig.tsbuildinfo
1 directory, 1 file
コミットします。
$ git add .
$ git commit -m "feat: noEmitをtrueに変更"
noEmit を false に変更してみます。
{
  "compilerOptions": {
    "target": "es2015",
    "module": "commonjs",
    "sourceMap": true,
    "outDir": "./dist",
    "strict": true,
    "skipLibCheck": true,
    "incremental": true,
-   "noEmit": true,
+   "noEmit": false,
    "forceConsistentCasingInFileNames": true,
    "esModuleInterop":true
  },
  "include": ["**/*.ts","**/*.js"],
  "exclude": ["node_modules", "dist"]
}
ビルドします。noEmit を false に設定していますが、index.js は出力されません。incremental を有効化すると、このように期待せぬ動作が発生します。
$ pnpm run build
$ tree dist
dist
└── tsconfig.tsbuildinfo
1 directory, 1 file
.tsbuildinfo を削除し、ビルドします。するとしっかりと index.js と index.js.map が出力されました。
$ rm -rf dist/tsconfig.tsbuildinfo
$ pnpm run build
$ tree dist
dist
├── index.js
├── index.js.map
└── tsconfig.tsbuildinfo
1 directory, 3 files
コミットします。
$ git add .
$ git commit -m "feat: noEmitをfalseに変更"
Next.jsのプロジェクトで動作確認
Next.js のプロジェクトを作成し、incremental が false の場合と true の場合で動作を確認します。
事前環境の構築
動作を作業するための Next.js プロジェクトを作成します。長いので、折り畳んでおきます。
新規プロジェクト作成と初期環境構築の手順詳細
プロジェクトを作成
create next-app@latestでプロジェクトを作成します。
$ pnpm create next-app@latest next-tsconfig-incremental --typescript --eslint --import-alias "@/*" --src-dir --use-pnpm --tailwind --app
$ cd next-tsconfig-incremental
Peer Dependenciesの警告を解消
Peer dependenciesの警告が出ている場合は、pnpm installを実行し、警告を解消します。
 WARN  Issues with peer dependencies found
.
├─┬ autoprefixer 10.0.1
│ └── ✕ unmet peer postcss@^8.1.0: found 8.0.0
├─┬ tailwindcss 3.3.0
│ ├── ✕ unmet peer postcss@^8.0.9: found 8.0.0
│ ├─┬ postcss-js 4.0.1
│ │ └── ✕ unmet peer postcss@^8.4.21: found 8.0.0
│ ├─┬ postcss-load-config 3.1.4
│ │ └── ✕ unmet peer postcss@>=8.0.9: found 8.0.0
│ └─┬ postcss-nested 6.0.0
│   └── ✕ unmet peer postcss@^8.2.14: found 8.0.0
└─┬ next 14.0.4
  ├── ✕ unmet peer react@^18.2.0: found 18.0.0
  └── ✕ unmet peer react-dom@^18.2.0: found 18.0.0
以下を実行することで警告が解消されます。
$ pnpm i -D postcss@latest react@^18.2.0 react-dom@^18.2.0
不要な設定を削除し、プロジェクトを初期化します。
styles
CSSなどを管理するstylesディレクトリを作成します。globals.cssを移動します。
$ mkdir -p src/styles
$ mv src/app/globals.css src/styles/globals.css
globals.cssの内容を以下のように上書きします。
@tailwind base;
@tailwind components;
@tailwind utilities;
初期ページ
app/page.tsxを上書きします。
import { type FC } from "react";
const Home: FC = () => {
  return (
    <div className="">
      <div className="text-lg font-bold">Home</div>
      <div>
        <span className="text-blue-500">Hello</span>
        <span className="text-red-500">World</span>
      </div>
    </div>
  );
};
export default Home;
レイアウト
app/layout.tsxを上書きします。
import "@/styles/globals.css";
import { type FC } from "react";
type RootLayoutProps = {
  children: React.ReactNode;
};
export const metadata = {
  title: "Sample",
  description: "Generated by create next app",
};
const RootLayout: FC<RootLayoutProps> = (props) => {
  return (
    <html lang="ja">
      <body className="">{props.children}</body>
    </html>
  );
};
export default RootLayout;
TailwindCSSの設定
TailwindCSSの設定(tailwind.config.ts)を上書きします。
import type { Config } from 'tailwindcss'
const config: Config = {
  content: [
    './src/pages/**/*.{js,ts,jsx,tsx,mdx}',
    './src/components/**/*.{js,ts,jsx,tsx,mdx}',
    './src/app/**/*.{js,ts,jsx,tsx,mdx}',
  ],
  plugins: [],
}
export default config
TypeScriptの設定
TypeScriptの初期設定はこちらです。
{
  "compilerOptions": {
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "bundler",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true,
    "plugins": [
      {
        "name": "next"
      }
    ],
    "paths": {
      "@/*": ["./src/*"]
    }
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
  "exclude": ["node_modules"]
}
スクリプトを追加
型チェックのスクリプトを追加します。
{
  "name": "next-tsconfig-resolvejsonmodule",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint",
+   "typecheck": "tsc"
  },
  "dependencies": {
    "next": "14.1.4"
  },
  "devDependencies": {
    "@types/node": "^20",
    "@types/react": "^18",
    "@types/react-dom": "^18",
    "autoprefixer": "^10.0.1",
    "eslint": "^8",
    "eslint-config-next": "14.1.4",
    "postcss": "^8.4.38",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "tailwindcss": "^3.3.0",
    "typescript": "^5"
  }
}
動作確認
ローカルで動作確認します。
$ pnpm run dev

コミットして作業結果を保存しておきます。
$ git add .
$ git commit -m "feat:新規にプロジェクトを作成し, 作業環境を構築"
 incremental が false の場合
tsconfig.json を作成します。incremental を無効化します。
{
  "compilerOptions": {
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "bundler",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
-   "incremental": true,
+   "incremental": false,
    "plugins": [
      {
        "name": "next"
      }
    ],
    "paths": {
      "@/*": ["./src/*"]
    }
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
  "exclude": ["node_modules"]
}
型チェックをします。エラーは出ません。
$ pnpm run typecheck
time コマンドを利用し、typecheck スクリプトを実行することで、incremental が有効化されている場合の処理時間を計測します。
$ time pnpm run typecheck
pnpm run typecheck  3.56s user 0.28s system 169% cpu 2.271 total
tsconfig.tsbuildinfo は作成されていません。
$ ls tsconfig.tsbuildinfo
ls: tsconfig.tsbuildinfo: No such file or directory
コミットします。
$ git add .
$ git commit -m "feat: incrementalを無効化"
 incremental が true の場合
incremental を有効化すると、以前のコンパイルから変更があった差分のみを今回のコンパイル対象とします。差分を把握するために必要な情報を、指定された出力フォルダに .tsbuildinfo ファイルが作成されます。JavaScript 実行時には、.tsbuildinfo は使用されないため、削除しても問題ありません。
incremental を有効化します。.tsbuildinfo の保存先を outDir にて指定します。
{
  "compilerOptions": {
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "bundler",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
-   "incremental": false,
+   "incremental": true,
+   "outDir": "./dist",
    "plugins": [
      {
        "name": "next"
      }
    ],
    "paths": {
      "@/*": ["./src/*"]
    }
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
  "exclude": ["node_modules"]
}
型チェックします。すると、tsconfig.tsbuildinfo が作成されます。time で処理時間を計測します。
$ time pnpm run typecheck
pnpm run typecheck  3.59s user 0.28s system 168% cpu 2.304 total
$ ls dist/tsconfig.tsbuildinfo
tsconfig.tsbuildinfo
あらためて型チェックを行います。.tsbuildinfo が作成されているため、処理時間が短縮されていることが確認できます。
$ time pnpm run typecheck
pnpm run typecheck  3.36s user 0.28s system 169% cpu 2.145 total
1 回目は 2.304 秒、2 回目は 2.145 秒でした。incremental が有効化されている場合、処理時間が短縮されていることが確認できました。
コミットします。
$ git add .
$ git commit -m "feat: incrementalを有効化し処理時間が短縮化されたことを確認"
 incremental が true の場合に注意する点
incremental が true の場合の注意点があります。注意点として、tsconfig.json に変更があった場合は、前回差分だけでは最新の tsconfig.json に準拠しない可能性があります。よって、tsconfig.json に変更があった場合は、.tsbuildinfo を削除します。
.tsbuildinfo を削除せずに、tsconfig.json を変更してみます。
{
  "compilerOptions": {
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
-   "noEmit": true,
+   "noEmit": false,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "bundler",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "outDir": "./dist",
    "incremental": true,
    "plugins": [
      {
        "name": "next"
      }
    ],
    "paths": {
      "@/*": ["./src/*"]
    }
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
  "exclude": ["node_modules"]
}
一旦、設定を記録した .tsbuildinfo を削除します。
$ rm -rf dist/tsconfig.tsbuildinfo
ビルドします。dist に tsconfig.tsbuildinfo が作成されました。noEmit を false に設定しているので、トランスパイルの結果が dist に出力されます。
$ pnpm run typecheck
$ tree dist -I node_modules
dist
├── src
│   └── app
│       ├── layout.jsx
│       └── page.jsx
├── tailwind.config.js
└── tsconfig.tsbuildinfo
コミットします。
$ git add .
$ git commit -m "feat: noEmitをfalseに変更しトランスパイルの結果を出力"
noEmit を true に変更します。
{
  "compilerOptions": {
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
-   "noEmit": false,
+   "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "bundler",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "outDir": "./dist",
    "incremental": true,
    "plugins": [
      {
        "name": "next"
      }
    ],
    "paths": {
      "@/*": ["./src/*"]
    }
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
  "exclude": ["node_modules"]
}
トランスパイルの結果を削除します。
$ rm -rf dist/.next dist/src dist/tailwind.config.js 
$ tree dist
dist
└── tsconfig.tsbuildinfo
1 directory, 1 file
ビルドします。noEmit を true に設定しているので、トランスパイルの結果は出力されません。
$ pnpm run typecheck
$ tree dist
dist
└── tsconfig.tsbuildinfo
1 directory, 1 file
コミットします。
$ git add .
$ git commit -m "feat: noEmitをtrueに変更しトランスパイルの結果を出力"
noEmit を false に変更してみます。
{
  "compilerOptions": {
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
-   "noEmit": true,
+   "noEmit": false,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "bundler",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "outDir": "./dist",
    "incremental": true,
    "plugins": [
      {
        "name": "next"
      }
    ],
    "paths": {
      "@/*": ["./src/*"]
    }
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
  "exclude": ["node_modules"]
}
ビルドします。noEmit を false に設定していますが、トランスパイルの結果は出力されません。incremental を有効化すると、このように期待せぬ動作が発生します。
$ pnpm run typecheck
$ tree dist
dist
└── tsconfig.tsbuildinfo
1 directory, 1 file
.tsbuildinfo を削除し、ビルドします。するとしっかりとトランスパイルの結果が出力されます。
$ rm -rf dist/tsconfig.tsbuildinfo
$ pnpm run typecheck
$ tree dist
dist
├── src
│   └── app
│       ├── layout.jsx
│       └── page.jsx
├── tailwind.config.js
└── tsconfig.tsbuildinfo
3 directories, 4 files
コミットします。
$ git add .
$ git commit -m "feat: noEmitをfalseに変更し挙動を確認"
Next.jsの設定考察
create next-app で作成した Next.js プロジェクトで incremental は true が設定されています。Next.js で型チェックの時間を短縮化させるために incremental を有効化することをお勧めします。が、incremental が有効化されている場合、tsconfig.json に変更があった場合は、前回差分だけでは最新の tsconfig.json に準拠しない可能性があります。よって、tsconfig.json に変更があった場合は、.tsbuildinfo を削除します。
まとめ
この記事では incremental が false の場合と true の場合で動作を確認しました。Node.js & TypeScript のプロジェクトと Next.js のプロジェクトで動作確認を行いました。
Discussion