iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🏖️

[TypeScript / Next.js] How to exclude tests and unnecessary files from builds using a custom tsconfig.json

に公開

Goal

As a premise, this is a discussion about developing applications configured with TypeScript/Next.js.

In TypeScript, you can specify the paths of files to be included or excluded by the compiler in tsconfig.json.

Therefore, the purpose of this post is to explore whether it's possible to include all files during development and exclude files unnecessary for the build—such as tests or Storybook files—during the build process to make it faster and lighter.

By the way, when I asked ChatGPT about excluding unnecessary files during the build, I got a response like this:
https://chat.openai.com/share/58a50f18-e943-48f2-84fc-78f9d9408077

Strategy and Research

I thought that if I could prepare a tsconfig.json containing everything and a tsconfig.build.json that excludes unnecessary files via exclude, and then load tsconfig.build.json only during the build, I could achieve this goal.

If you are just running the TypeScript compiler, you can specify it with the --project option like this:

Terminal
tsc --project tsconfig.build.json

Also, in the case of webpack, it can be achieved as shown in the following article:
https://mizchi.dev/202006101314-switch-tsconfig-on-webpack

However, when I searched for a way to switch tsconfig.json in Next.js, surprisingly few results came up...

According to the official Next.js documentation, it seems possible to configure TypeScript settings in next.config.js options.
However, only the ignoreBuildErrors setting is mentioned.
https://nextjs.org/docs/pages/api-reference/next-config-js/typescript

So, I looked into the Next.js source code to see if there are other TypeScript-related settings, and I found that it loads a specified tsconfigPath in the following section:
https://github.com/vercel/next.js/blob/canary/packages/next/src/build/load-jsconfig.ts#L55-L58

In other words, it seems like we can build with an arbitrary tsconfig.json by specifying tsconfigPath in next.config.js!

As a side note, if you change github.com to github.dev in the repository URL, you can open VS Code in your browser, which makes searching much easier:
https://github.dev/vercel/next.js/blob/canary/packages/next/src/build/load-jsconfig.ts#L55-L58

Implementation

First, prepare a tsconfig.json to compile everything except node_modules.
Since tsconfig.json is loaded by default, I haven't specified a path to refer to this file separately.

tsconfig.json
{
  "compilerOptions": {
    // Omitted
  },
  "include": [
    // Omitted
  ],
  "exclude": [
    "node_modules"
  ]
}

Next, create tsconfig.build.json by adding tests and Storybook files to the exclude list of tsconfig.json.
In this case, I'm excluding *.test.tsx and *.storybook.tsx, but please change this according to the files you want to exclude.
Since everything except the exclude part is the same, we'll use extends to inherit from tsconfig.json.

tsconfig.build.json
{
  "extends": "./tsconfig.json",
  "exclude": [
    "node_modules",
    "**/*.test.tsx",
    "**/*.storybook.tsx"
  ]
}

Finally, just configure next.config.js to load tsconfig.build.json.

next.config.js
module.exports = {
  typescript: {
    tsconfigPath: 'tsconfig.build.json',
  },
}

Verification

When running next build, the following message appeared, confirming that tsconfig.build.json is being referenced!

Using tsconfig file: tsconfig.build.json

Conclusion

Maybe I'll try submitting a PR to add a description about tsconfigPath to the official documentation.

Discussion