Skip to content

tsup: Excluding Files From the Build


Created: Nov 04, 2023 – Last Updated: Nov 04, 2023

Tags: Tooling

Digital Garden

tsup is a popular bundler powered by esbuild. You can bundle, transform, and minify JavaScript and TypeScript with it.

One common problem I ran into was that I needed to exclude files from the build output, namely test files. Because my Jest/Vitest files are most often inside a __tests__ folder inside src or alongside the source files with a postfix of <filename>.test.<ext>.

You can use the CLI to give tsup its inputs:

shell
tsup src

This would build all files inside the src directory. In a configuration file it would look like this:

ts
import { defineConfig } from 'tsup'
export default defineConfig({
entry: ['src'],
})

tsup uses globby under the hood for its pattern matching inside entry so you can also use ! to notate a negative pattern.

To exclude files inside __tests__ or <filename>.test.<ext> files, you can modify the entry to this:

ts
import { defineConfig } from 'tsup'
export default defineConfig({
entry: ['src', '!src/**/__tests__/**', '!src/**/*.test.*'],
})

Feel free to use a less verbose configuration (since you can combine this logic into one pattern) and test it with e.g. globster.xyz.


Want to learn more? Browse my Digital Garden