What is tsup?
Tsup is a zero-config TypeScript bundler that aims to provide a simple and efficient way to bundle TypeScript projects. It supports various output formats, minification, and other advanced features with minimal configuration.
What are tsup's main functionalities?
Basic Bundling
This command bundles the TypeScript file located at `src/index.ts` into CommonJS and ES Module formats. Tsup automatically handles the TypeScript compilation and output generation.
tsup src/index.ts --format cjs,esm
Minification
This command bundles and minifies the TypeScript file located at `src/index.ts`. Minification reduces the file size by removing unnecessary whitespace and comments, making the output more efficient for production use.
tsup src/index.ts --minify
Watch Mode
This command enables watch mode, which automatically rebundles the TypeScript file located at `src/index.ts` whenever changes are detected. This is useful for development environments where you want to see changes in real-time.
tsup src/index.ts --watch
Custom Entry Points
This command allows you to specify multiple entry points for bundling. In this example, both `src/index.ts` and `src/worker.ts` are bundled into ES Module format. This is useful for projects with multiple entry files.
tsup src/index.ts src/worker.ts --format esm
Environment Variables
This command allows you to define environment variables that can be used within your TypeScript code. In this example, `PROCESS_ENV` is set to `production`, which can be used for conditional logic in the code.
tsup src/index.ts --define.PROCESS_ENV=production
Other packages similar to tsup
esbuild
Esbuild is an extremely fast JavaScript bundler and minifier. It is known for its speed and efficiency, making it a popular choice for large projects. Compared to tsup, esbuild offers more advanced features but may require more configuration.
rollup
Rollup is a module bundler for JavaScript that compiles small pieces of code into something larger and more complex, such as a library or application. It is highly configurable and supports a wide range of plugins. Rollup is more flexible but can be more complex to set up compared to tsup.
webpack
Webpack is a powerful and highly configurable module bundler for JavaScript applications. It supports a wide range of features and plugins, making it suitable for complex projects. However, it can be more challenging to configure and may require more boilerplate compared to tsup.
tsup
Bundle your TypeScript library with no config, powered by esbuild.
What can it bundle?
Anything that's supported by Node.js natively, namely .js
, .json
, .mjs
. And TypeScript .ts
, .tsx
.
This project is designed for bundling Node.js libraries.
Install
Install it locally in your project folder:
npm i tsup -D
yarn add tsup --dev
You can also install it globally but it's not recommended.
Usage
Bundle files
tsup [...files]
Files are written into ./dist
.
You can bundle multiple files in one go:
tsup src/index.ts src/cli.ts
Code splitting is enabled by default and supported in cjs
and esm
format.
Excluding packages
By default tsup bundles all import
-ed modules but dependencies
in your packages.json
are always excluded, you can also use --external <module>
flag to mark other packages as external.
Generate declaration file
tsup index.ts --dts
This will emit ./dist/index.js
and ./dist/index.d.ts
.
Bundle formats
Supported format: esm
, cjs
, (default) and iife
.
You can bundle in multiple format in one go:
tsup src/index.ts --format esm,cjs,iife
That will output files in following folder structure:
dist
├── esm
│ └── index.js
├── iife
│ └── index.js
└── index.js
ES5 support
You can --target es5
or "target": "es5" in
tsconfig.jsonto compile the code down to es5, it's processed by [buble](http://buble.surge.sh/). Some features are supported by this target, namely:
for .. of`.
Watch mode
tsup src/index.ts --watch
What about type checking?
esbuild is fast because it doesn't perform any type checking, you already get type checking from your IDE like VS Code or WebStorm.
Additionally, if you want type checking at build time, you can enable --dts
, which will run a real TypeScript compiler to generate declaration file so you get type checking as well.
For more details:
tsup --help
License
MIT © EGOIST (Kevin Titor)