iTranslated by AI
[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:
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:
tsc --project tsconfig.build.json
Also, in the case of webpack, it can be achieved as shown in the following article:
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.
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:
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:
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.
{
"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.
{
"extends": "./tsconfig.json",
"exclude": [
"node_modules",
"**/*.test.tsx",
"**/*.storybook.tsx"
]
}
Finally, just configure next.config.js to load tsconfig.build.json.
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