Combined minification example
Example showing all three minification modes combined: Input (158 bytes) becomes output (49 bytes, 69% reduction). Input code: const myVariable = 42; const myFunction = () => { const isValid = true; const result = undefined; return isValid ? myVariable : result; }; const output = myFunction(); Output with --minify: const a=42,b=()=>{const c=!0,d=void 0;return c?a:d},e=b();
Minification mode: Infinity shortening
Infinity shortening is enabled with --minify-syntax. It converts Infinity to 1/0 and -Infinity to -1/0.
Minifier overview
Bun includes a fast JavaScript and TypeScript minifier that can reduce bundle sizes by 80% or more depending on the codebase. 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.
minify option in Bun.build API
When using Bun's bundler programmatically, configure minification through the minify option. Set minify: true to enable all minification modes. For granular control, pass an object with properties whitespace, syntax, and identifiers each set to true or false.
Minification mode: Boolean literal shortening
Boolean literal shortening is enabled with --minify-syntax. It converts true to !0 and false to !1.
Minification mode: Boolean algebra optimizations
Boolean algebra optimizations are enabled with --minify-syntax. Examples: !!x becomes x, x === true becomes x, x && true becomes x, x || false becomes x, !true becomes !1, !false becomes !0.
Minification mode: Undefined shortening
Undefined shortening is enabled with --minify-syntax. It replaces undefined with void 0. Example: let x = undefined becomes let x=void 0.
Minification mode: Undefined equality optimization
Undefined equality optimization is enabled with --minify-syntax. It optimizes loose equality checks with undefined: x == undefined becomes x == null and x != undefined becomes x != null.
Minification mode: Typeof optimizations
Typeof optimizations are enabled with --minify-syntax. Examples: typeof x === 'undefined' becomes typeof x>'u', typeof x !== 'undefined' becomes typeof x<'u', typeof null becomes 'object', typeof true becomes 'boolean', typeof 123 becomes 'number', typeof "str" becomes 'string', typeof 123n becomes 'bigint'.
Minification mode: Number formatting
Number formatting is enabled with --minify-syntax. It formats numbers in the most compact representation. Examples: 10000 becomes 1e4, 100000 becomes 1e5, 1000000 becomes 1e6, 1.0 becomes 1, -42.0 becomes -42.
Minification mode: Arithmetic constant folding
Arithmetic constant folding is enabled with --minify-syntax. It evaluates arithmetic operations at compile time. Examples: 1 + 2 becomes 3, 10 - 5 becomes 5, 3 * 4 becomes 12, 10 / 2 becomes 5, 10 % 3 becomes 1, 2 ** 3 becomes 8.
Minification mode: Bitwise constant folding
Bitwise constant folding is enabled with --minify-syntax. It evaluates bitwise operations at compile time. Examples: 5 & 3 becomes 1, 5 | 3 becomes 7, 5 ^ 3 becomes 6, 8 << 2 becomes 32, 32 >> 2 becomes 8, ~5 becomes -6.
Minification mode: String concatenation
String concatenation is enabled with --minify-syntax. It combines string literals at compile time. Examples: "a" + "b" becomes "ab", "x" + 123 becomes "x123", "foo" + "bar" + "baz" becomes "foobarbaz".
Minification mode: String indexing
String indexing is enabled with --minify-syntax. It evaluates string character access at compile time. Examples: "foo"[2] becomes "o", "hello"[0] becomes "h".
Minification mode: Template literal folding
Template literal folding is enabled with --minify-syntax. It evaluates template literals with constant expressions. Examples: `a${123}b` becomes "a123b", `result: ${5 + 10}` becomes "result: 15".
Minification mode: Template literal to string conversion
Template literal to string conversion is enabled with --minify-syntax. It converts template literals with no substitutions to regular strings. Examples: `Hello World` becomes "Hello World", `Line 1\nLine 2` becomes "Line 1\nLine 2".
Minification mode: String quote optimization
String quote optimization is enabled with --minify-syntax. It chooses the optimal quote character to minimize escapes. Example: `Simple string` becomes "Simple string".
Minification mode: Array spread inlining
Array spread inlining is enabled with --minify-syntax. It inlines array spread operations with constant arrays. Examples: [1, ...[2, 3], 4] becomes [1,2,3,4], [...[a, b]] becomes [a,b].
Minification mode: Array indexing
Array indexing is enabled with --minify-syntax. It evaluates constant array access at compile time. Examples: [x][0] becomes x, ['a', 'b', 'c'][1] becomes 'b', ['a', , 'c'][1] becomes void 0.
Minification mode: Property access optimization
Property access optimization is enabled with --minify-syntax. It converts bracket notation to dot notation when possible. Examples: obj["property"] becomes obj.property, obj["validName"] becomes obj.validName, obj["123"] remains obj["123"], obj["invalid-name"] remains obj["invalid-name"].
Minification mode: Comparison folding
Comparison folding is enabled with --minify-syntax. It evaluates constant comparisons at compile time. Examples: 3 < 5 becomes !0, 5 > 3 becomes !0, 3 <= 3 becomes !0, 5 >= 6 becomes !1, "a" < "b" becomes !0.
Minification mode: Logical operation folding
Logical operation folding is enabled with --minify-syntax. It simplifies logical operations with constant values. Examples: true && x becomes x, false && x becomes !1, true || x becomes !0, false || x becomes x.
Minification mode: Nullish coalescing folding
Nullish coalescing folding is enabled with --minify-syntax. It evaluates nullish coalescing with known values. Examples: null ?? x becomes x, undefined ?? x becomes x, 42 ?? x becomes 42.
Minification mode: Comma expression simplification
Comma expression simplification is enabled with --minify-syntax. It removes side-effect-free expressions from comma sequences. Examples: (0, x) becomes x, (123, "str", x) becomes x.
Minification mode: Ternary conditional folding
Ternary conditional folding is enabled with --minify-syntax. It evaluates conditional expressions with constant conditions. Examples: true ? a : b becomes a, false ? a : b becomes b, x ? true : false becomes x ? !0 : !1.
Minification mode: Unary expression folding
Unary expression folding is enabled with --minify-syntax. Examples: +123 becomes 123, +"123" becomes 123, -(-x) remains - -x (optimizations limited), ~~x remains ~~x (preserved).
Minification mode: Double negation removal
Double negation removal is enabled with --minify-syntax. It removes unnecessary double negations. Examples: !!x becomes x, !!!x becomes !x.
Minification mode: If statement optimization
If statement optimization is enabled with --minify-syntax. It optimizes if statements with constant conditions. Examples: if (true) x; becomes x;, if (false) x; is removed, if (x) { a; } becomes if(x)a;, if (x) {} else y; becomes if(!x)y;.
Minification mode: Dead code elimination
Dead code elimination is enabled with --minify-syntax. It removes unreachable code and code without side effects. Example: unreachable code after return statements and code inside if (false) blocks are removed.
Minification mode: Unreachable branch removal
Unreachable branch removal is enabled with --minify-syntax. It removes branches that can never execute. Example: while (false) { neverRuns(); } is removed entirely.
Minification mode: Empty block removal
Empty block removal is enabled with --minify-syntax. It removes empty blocks and unnecessary braces. Examples: { } becomes ;, if (x) { } is removed.
Minification mode: Single statement block unwrapping
Single statement block unwrapping is enabled with --minify-syntax. It removes unnecessary braces around single statements. Example: if (condition) { doSomething(); } becomes if(condition)doSomething();.
Minification mode: TypeScript enum inlining
TypeScript enum inlining is enabled with --minify-syntax. It inlines TypeScript enum values at compile time. Example: enum Color { Red, Green, Blue } const x = Color.Red; becomes const x=0;.
Pure annotation support
Pure annotation support is always active. It respects /*@__PURE__*/ annotations for tree shaking (removing unused code). If a variable assigned with a pure annotation is unused, it is removed entirely.
Minification mode: Identifier renaming
Identifier renaming is enabled with --minify-identifiers. It renames local variables to shorter names based on usage frequency. Most frequently used identifiers get the shortest names (a, b, c...). Single letters provide 26 names, double letters provide 676 names, triple letters and beyond as needed. Preserved identifiers include JavaScript keywords, global identifiers, named exports, and CommonJS names (exports, module).
Minification mode: Whitespace removal
Whitespace removal is enabled with --minify-whitespace. It removes all unnecessary whitespace from function declarations, variable declarations, and control structures.
Minification mode: Semicolon optimization
Semicolon optimization is enabled with --minify-whitespace. It inserts semicolons only when necessary. Example: let a = 1; let b = 2; return a + b; becomes let a=1;let b=2;return a+b.
Minification mode: Operator spacing removal
Operator spacing removal is enabled with --minify-whitespace. It removes spaces around operators. Examples: a + b becomes a+b, x = y * z becomes x=y*z, foo && bar || baz becomes foo&&bar||baz.
Minification mode: Comment removal
Comment removal is enabled with --minify-whitespace. It removes comments except important license comments marked with /*!. Example: /*! But this license comment is kept */ is preserved while regular comments are removed.
Minification mode: Object and array formatting
Object and array formatting is enabled with --minify-whitespace. It removes whitespace in object and array literals. Example: const obj = { name: "John", age: 30 }; becomes const obj={name:"John",age:30};.
Minification mode: Control flow formatting
Control flow formatting is enabled with --minify-whitespace. It removes whitespace in control structures. Example: if (condition) { doSomething(); } becomes if(condition)doSomething();.
Minification mode: Function formatting
Function formatting is enabled with --minify-whitespace. It removes whitespace in function declarations. Example: function myFunction(param1, param2) { return param1 + param2; } becomes function myFunction(param1,param2){return param1+param2}.
Minification mode: Parentheses minimization
Parentheses minimization is always active. It only adds parentheses when necessary for operator precedence. Examples: (a + b) * c remains (a+b)*c, a + (b * c) becomes a+b*c, ((x)) becomes x.
Minification mode: Template literal value folding
Template literal value folding is enabled with --minify-syntax. It converts non-string interpolated values to strings and folds them into the template. Examples: `hello ${123}` becomes "hello 123", `value: ${true}` becomes "value: true", `result: ${null}` becomes "result: null".