skills/bun-bundler-minifier/SKILL.md
Reduce bundle sizes with Bun's JavaScript and TypeScript minifier
npx skillsauth add jarle/bun-skills Bun MinifierInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Reduce bundle sizes with Bun's JavaScript and TypeScript minifier
Bun includes a fast JavaScript and TypeScript minifier that can reduce bundle sizes by 80% or more (depending on the codebase) and make output code run faster. The minifier performs dozens of optimizations including constant folding, dead code elimination, and syntax transformations. Unlike other minifiers, Bun's minifier makes bun build run faster since there's less code to print.
Use the --minify flag to enable all minification modes:
bun build ./index.ts --minify --outfile=out.js
The --minify flag automatically enables:
The --production flag automatically enables minification:
bun build ./index.ts --production --outfile=out.js
The --production flag also:
process.env.NODE_ENV to productionYou can enable specific minification modes individually:
# Only remove whitespace
bun build ./index.ts --minify-whitespace --outfile=out.js
# Only minify syntax
bun build ./index.ts --minify-syntax --outfile=out.js
# Only minify identifiers
bun build ./index.ts --minify-identifiers --outfile=out.js
# Combine specific modes
bun build ./index.ts --minify-whitespace --minify-syntax --outfile=out.js
When using Bun's bundler programmatically, configure minification through the minify option:
await Bun.build({
entrypoints: ["./index.ts"],
outdir: "./out",
minify: true, // Enable all minification modes
});
For granular control, pass an object:
await Bun.build({
entrypoints: ["./index.ts"],
outdir: "./out",
minify: {
whitespace: true,
syntax: true,
identifiers: true,
},
});
Bun's minifier has three independent modes that can be enabled separately or combined.
--minify-whitespace)Removes all unnecessary whitespace, newlines, and formatting from the output.
--minify-syntax)Rewrites JavaScript syntax to shorter equivalent forms and performs constant folding, dead code elimination, and other optimizations.
--minify-identifiers)Renames local variables and function names to shorter identifiers using frequency-based optimization.
Mode: --minify-syntax
Converts boolean literals to shorter expressions.
true
false
!0
!1
Mode: --minify-syntax
Simplifies boolean expressions using logical rules.
!!x
x === true
x && true
x || false
!true
!false
x
x
x
x
!1
!0
Mode: --minify-syntax
Replaces undefined with shorter equivalent.
undefined
let x = undefined;
void 0
let x=void 0;
Mode: --minify-syntax
Optimizes loose equality checks with undefined.
x == undefined
x != undefined
x == null
x != null
Mode: --minify-syntax
Converts Infinity to mathematical expressions.
Infinity
-Infinity
1/0
-1/0
Mode: --minify-syntax
Optimizes typeof comparisons and evaluates constant typeof expressions.
typeof x === 'undefined'
typeof x !== 'undefined'
typeof require
typeof null
typeof true
typeof 123
typeof "str"
typeof 123n
typeof x>'u'
typeof x<'u'
"function"
"object"
"boolean"
"number"
"string"
"bigint"
Mode: --minify-syntax
Formats numbers in the most compact representation.
10000
100000
1000000
1.0
-42.0
1e4
1e5
1e6
1
-42
Mode: --minify-syntax
Evaluates arithmetic operations at compile time.
1 + 2
10 - 5
3 * 4
10 / 2
10 % 3
2 ** 3
3
5
12
5
1
8
Mode: --minify-syntax
Evaluates bitwise operations at compile time.
5 & 3
5 | 3
5 ^ 3
8 << 2
32 >> 2
~5
1
7
6
32
8
-6
Mode: --minify-syntax
Combines string literals at compile time.
"a" + "b"
"x" + 123
"foo" + "bar" + "baz"
"ab"
"x123"
"foobarbaz"
Mode: --minify-syntax
Evaluates string character access at compile time.
"foo"[2]
"hello"[0]
"o"
"h"
Mode: --minify-syntax
Evaluates template literals with constant expressions.
`a${123}b`
`result: ${5 + 10}`
"a123b"
"result: 15"
Mode: --minify-syntax
Converts simple template literals to regular strings.
`Hello World`
`Line 1
Line 2`
"Hello World"
"Line 1\nLine 2"
Mode: --minify-syntax
Chooses the optimal quote character to minimize escapes.
"It's a string"
'He said "hello"'
`Simple string`
"It's a string"
'He said "hello"'
"Simple string"
Mode: --minify-syntax
Inlines array spread operations with constant arrays.
[1, ...[2, 3], 4]
[...[a, b]]
[1,2,3,4]
[a,b]
Mode: --minify-syntax
Evaluates constant array access at compile time.
[x][0]
['a', 'b', 'c'][1]
['a', , 'c'][1]
x
'b'
void 0
Mode: --minify-syntax
Converts bracket notation to dot notation when possible.
obj["property"]
obj["validName"]
obj["123"]
obj["invalid-name"]
obj.property
obj.validName
obj["123"]
obj["invalid-name"]
Mode: --minify-syntax
Evaluates constant comparisons at compile time.
3 < 5
5 > 3
3 <= 3
5 >= 6
"a" < "b"
!0
!0
!0
!1
!0
Mode: --minify-syntax
Simplifies logical operations with constant values.
true && x
false && x
true || x
false || x
x
!1
!0
x
Mode: --minify-syntax
Evaluates nullish coalescing with known values.
null ?? x
undefined ?? x
42 ?? x
x
x
42
Mode: --minify-syntax
Removes side-effect-free expressions from comma sequences.
(0, x)
(123, "str", x)
x
x
Mode: --minify-syntax
Evaluates conditional expressions with constant conditions.
true ? a : b
false ? a : b
x ? true : false
x ? false : true
a
b
x ? !0 : !1
x ? !1 : !0
Mode: --minify-syntax
Simplifies unary operations.
+123
+"123"
-(-x)
~~x
!!x
123
123
123
123
x
~~x
!!x
x
Mode: --minify-syntax
Removes unnecessary double negations.
!!x
!!!x
x
!x
Mode: --minify-syntax
Optimizes if statements with constant conditions.
if (true) x;
if (false) x;
if (x) { a; }
if (x) {} else y;
x;
// removed
if(x)a;
if(!x)y;
Mode: --minify-syntax
Removes unreachable code and code without side effects.
if (false) {
unreachable();
}
function foo() {
return x;
deadCode();
}
function foo(){return x}
Mode: --minify-syntax
Removes branches that can never execute.
while (false) {
neverRuns();
}
// removed entirely
Mode: --minify-syntax
Removes empty blocks and unnecessary braces.
{ }
if (x) { }
;
// removed
Mode: --minify-syntax
Removes unnecessary braces around single statements.
if (condition) {
doSomething();
}
if(condition)doSomething();
Mode: --minify-syntax
Inlines TypeScript enum values at compile time.
enum Color { Red, Green, Blue }
const x = Color.Red;
const x=0;
Mode: Always active
Respects /*@__PURE__*/ annotations for tree shaking.
const x = /*@__PURE__*/ expensive();
// If x is unused...
// removed entirely
Mode: --minify-identifiers
Renames local variables to shorter names based on usage frequency.
function calculateSum(firstNumber, secondNumber) {
const result = firstNumber + secondNumber;
return result;
}
function a(b,c){const d=b+c;return d}
Naming strategy:
Preserved identifiers:
exports, moduleMode: --minify-whitespace
Removes all unnecessary whitespace.
function add(a, b) {
return a + b;
}
let x = 10;
function add(a,b){return a+b;}let x=10;
Mode: --minify-whitespace
Inserts semicolons only when necessary.
let a = 1;
let b = 2;
return a + b;
let a=1;let b=2;return a+b
Mode: --minify-whitespace
Removes spaces around operators.
a + b
x = y * z
foo && bar || baz
a+b
x=y*z
foo&&bar||baz
Mode: --minify-whitespace
Removes comments except important license comments.
// This comment is removed
/* So is this */
/*! But this license comment is kept */
function test() { /* inline comment */ }
/*! But this license comment is kept */
function test(){}
Mode: --minify-whitespace
Removes whitespace in object and array literals.
const obj = {
name: "John",
age: 30
};
const arr = [1, 2, 3];
const obj={name:"John",age:30};const arr=[1,2,3];
Mode: --minify-whitespace
Removes whitespace in control structures.
if (condition) {
doSomething();
}
for (let i = 0; i < 10; i++) {
console.log(i);
}
if(condition)doSomething();for(let i=0;i<10;i++)console.log(i);
Mode: --minify-whitespace
Removes whitespace in function declarations.
function myFunction(param1, param2) {
return param1 + param2;
}
const arrow = (a, b) => a + b;
function myFunction(a,b){return a+b}const arrow=(a,b)=>a+b;
Mode: Always active
Only adds parentheses when necessary for operator precedence.
(a + b) * c
a + (b * c)
((x))
(a+b)*c
a+b*c
x
Mode: --minify-identifiers (with configuration)
Renames object properties to shorter names when configured.
obj.longPropertyName
obj.a
Mode: --minify-syntax
Converts non-string interpolated values to strings and folds them into the template.
`hello ${123}`
`value: ${true}`
`result: ${null}`
`status: ${undefined}`
`big: ${10n}`
"hello 123"
"value: true"
"result: null"
"status: undefined"
"big: 10"
Mode: --minify-syntax
Evaluates .length property on string literals at compile time.
"hello world".length
"test".length
11
4
Mode: --minify-syntax
Simplifies constructor calls for built-in types.
new Object()
new Object(null)
new Object({a: 1})
new Array()
new Array(x, y)
{}
{}
{a:1}
[]
[x,y]
Mode: --minify-syntax
Inlines property access for objects with a single property.
({fn: () => console.log('hi')}).fn()
(() => console.log('hi'))()
Mode: Always active
Evaluates charCodeAt() on string literals for ASCII characters.
"hello".charCodeAt(1)
"A".charCodeAt(0)
101
65
Mode: --minify-syntax
Converts loose equality checks with void 0 to null since they're equivalent.
x == void 0
x != void 0
x == null
x != null
Mode: --minify-syntax
Moves negation operator through comma expressions.
-(a, b)
-(x, y, z)
a,-b
x,y,-z
Mode: Bundle mode
Inlines import.meta properties at build time when values are known.
import.meta.dir
import.meta.file
import.meta.path
import.meta.url
"/path/to/directory"
"filename.js"
"/full/path/to/file.js"
"file:///full/path/to/file.js"
Mode: --minify-syntax
Merges adjacent variable declarations of the same type.
let a = 1;
let b = 2;
const c = 3;
const d = 4;
let a=1,b=2;
const c=3,d=4;
Mode: --minify-syntax
Merges adjacent expression statements using comma operator.
console.log(1);
console.log(2);
console.log(3);
console.log(1),console.log(2),console.log(3);
Mode: --minify-syntax
Merges expressions before return with comma operator.
console.log(x);
return y;
return console.log(x),y;
Mode: --minify-syntax
Merges expressions before throw with comma operator.
console.log(x);
throw new Error();
throw(console.log(x),new Error());
Mode: --minify-syntax (bundle mode)
Inlines enum values across module boundaries.
// lib.ts
export enum Color { Red, Green, Blue }
// Input (main.ts)
import { Color } from './lib';
const x = Color.Red;
const x=0;
Mode: --minify-syntax
Inlines enum values used as computed object properties.
enum Keys { FOO = 'foo' }
const obj = { [Keys.FOO]: value }
const obj={foo:value}
Mode: --minify-syntax
Converts string numeric property access to numeric index.
obj["0"]
arr["5"]
obj[0]
arr[5]
Mode: Always active
Uses expression body syntax when an arrow function only returns a value.
() => { return x; }
(a) => { return a + 1; }
() => x
a => a + 1
Mode: Always active
Uses shorthand syntax when property name and value identifier match.
{ x: x, y: y }
{ name: name, age: age }
{ x, y }
{ name, age }
Mode: Always active
Uses method shorthand syntax in object literals.
{
foo: function() {},
bar: async function() {}
}
{
foo() {},
async bar() {}
}
Mode: --drop=debugger
Removes debugger statements from code.
function test() {
debugger;
return x;
}
function test(){return x}
Mode: --drop=console
Removes all console.* method calls from code.
console.log("debug");
console.warn("warning");
x = console.error("error");
void 0;
void 0;
x=void 0;
Mode: --drop=<name>
Removes calls to specified global functions or methods.
assert(condition);
obj.assert(test);
void 0;
void 0;
When minifying identifiers, you may want to preserve original function and class names for debugging purposes. Use the --keep-names flag:
bun build ./index.ts --minify --keep-names --outfile=out.js
Or in the JavaScript API:
await Bun.build({
entrypoints: ["./index.ts"],
outdir: "./out",
minify: {
identifiers: true,
keepNames: true,
},
});
This preserves the .name property on functions and classes while still minifying the actual identifier names in the code.
Using all three minification modes together:
const myVariable = 42;
const myFunction = () => {
const isValid = true;
const result = undefined;
return isValid ? myVariable : result;
};
const output = myFunction();
// Output with --minify (49 bytes, 69% reduction)
const a=42,b=()=>{const c=!0,d=void 0;return c?a:d},e=b();
Use --minify for:
Use individual modes for:
--minify-whitespace: Quick size reduction without semantic changes--minify-syntax: Smaller output while keeping readable identifiers for debugging--minify-identifiers: Maximum size reduction (combine with --keep-names for better stack traces)Avoid minification for:
development
Using TypeScript with Bun, including type definitions and compiler options
development
Learn how to write tests using Bun's Jest-compatible API with support for async tests, timeouts, and various test modifiers
testing
Learn how to use snapshot testing in Bun to save and compare output between test runs
testing
Learn about Bun test's runtime integration, environment variables, timeouts, and error handling