3.5 KiB
3.5 KiB
Minification
Compress code to reduce bundle size.
Overview
Minification removes unnecessary characters (whitespace, comments) and optimizes code for production, reducing bundle size and improving load times.
Note: Uses Oxc minifier internally. The minifier is currently in alpha.
Type
minify?: boolean | 'dce-only' | MinifyOptions
true— Enable full minification (whitespace removal, mangling, compression)false— Disable minification (default)'dce-only'— Only perform dead code elimination without full minificationMinifyOptions— Pass detailed options to the Oxc minifier
Basic Usage
CLI
# Enable minification
tsdown --minify
# Disable minification
tsdown --no-minify
Note: The CLI --minify flag is a boolean toggle. For 'dce-only' mode or advanced options, use the config file.
Config File
export default defineConfig({
entry: ['src/index.ts'],
minify: true,
});
DCE-Only Mode
Remove dead code without full minification (keeps readable output):
export default defineConfig({
entry: ['src/index.ts'],
minify: 'dce-only',
});
Example Output
Without Minification
// dist/index.mjs
const x = 1;
function hello(x$1) {
console.log('Hello World');
console.log(x$1);
}
hello(x);
With Minification
// dist/index.mjs
const e = 1;
function t(e) {
(console.log(`Hello World`), console.log(e));
}
t(e);
Common Patterns
Production Build
export default defineConfig({
entry: ['src/index.ts'],
format: ['esm', 'cjs'],
minify: true,
clean: true,
});
Conditional Minification
export default defineConfig((options) => ({
entry: ['src/index.ts'],
format: ['esm'],
minify: !options.watch, // Only minify in production
}));
Browser Library
export default defineConfig({
entry: ['src/index.ts'],
format: ['iife'],
platform: 'browser',
globalName: 'MyLib',
minify: true,
});
Multiple Builds
export default defineConfig([
// Development build
{
entry: ['src/index.ts'],
format: ['esm'],
minify: false,
outDir: 'dist/dev',
},
// Production build
{
entry: ['src/index.ts'],
format: ['esm'],
minify: true,
outDir: 'dist/prod',
},
]);
CLI Examples
# Production build with minification
tsdown --minify --clean
# Multiple formats with minification
tsdown --format esm --format cjs --minify
# Conditional minification (only when not watching)
tsdown --minify # Or omit --watch
Tips
- Use
minify: truefor production builds - Use
'dce-only'to remove dead code while keeping output readable - Skip minification during development for faster rebuilds
- Combine with tree shaking for best results
- Test minified output thoroughly (Oxc minifier is in alpha)
Troubleshooting
Minified Code Has Bugs
Oxc minifier is in alpha and may have issues:
- Use DCE-only mode:
minify: 'dce-only' - Report bug to Oxc project
- Disable minification:
minify: false
Unexpected Output
- Test unminified first to isolate issue
- Check source maps for debugging
- Verify target compatibility
Related Options
- Tree Shaking - Remove unused code
- Target - Syntax transformations
- Output Format - Module formats
- Sourcemap - Debug information