> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/oven-sh/bun/llms.txt
> Use this file to discover all available pages before exploring further.

# Minifier

> Reduce bundle size and improve startup time with Bun's fast JavaScript and TypeScript minifier. Supports whitespace removal, syntax optimization, identifier shortening, and bytecode compilation.

Bun includes a fast JavaScript and TypeScript minifier that can reduce bundle sizes by 80% or more. It performs constant folding, dead code elimination, syntax transformations, and identifier shortening. Because Bun minifies during bundling (not as a separate pass), there is less code to print — making `bun build` faster than running an external minifier.

## Enabling minification

Use `--minify` to enable all minification modes at once:

<Tabs>
  <Tab title="CLI">
    ```bash theme={null}
    bun build ./index.ts --outdir ./out --minify
    ```
  </Tab>

  <Tab title="JavaScript API">
    ```typescript theme={null}
    await Bun.build({
      entrypoints: ["./index.ts"],
      outdir: "./out",
      minify: true,
    });
    ```
  </Tab>
</Tabs>

`--minify` enables all three modes: whitespace removal, syntax optimization, and identifier shortening.

### Production mode

The `--production` flag also enables full minification:

```bash theme={null}
bun build ./index.ts --outdir ./out --production
```

`--production` additionally sets `process.env.NODE_ENV` to `"production"` and enables the production JSX transform.

### Granular control

Enable individual modes separately:

<Tabs>
  <Tab title="CLI">
    ```bash theme={null}
    # Whitespace only
    bun build ./index.ts --outdir ./out --minify-whitespace

    # Syntax only
    bun build ./index.ts --outdir ./out --minify-syntax

    # Identifiers only
    bun build ./index.ts --outdir ./out --minify-identifiers

    # Combine specific modes
    bun build ./index.ts --outdir ./out --minify-whitespace --minify-syntax
    ```
  </Tab>

  <Tab title="JavaScript API">
    ```typescript theme={null}
    await Bun.build({
      entrypoints: ["./index.ts"],
      outdir: "./out",
      minify: {
        whitespace: true,
        syntax: true,
        identifiers: true,
      },
    });
    ```
  </Tab>
</Tabs>

## Minification modes

### `--minify-whitespace`

Removes all unnecessary whitespace, newlines, comments, and formatting.

<CodeGroup>
  ```typescript input theme={null}
  function add(a: number, b: number) {
      // Add two numbers
      return a + b;
  }

  const result = add(1, 2);
  ```

  ```javascript output theme={null}
  function add(a,b){return a+b}const result=add(1,2);
  ```
</CodeGroup>

License comments (`/*! ... */`) are preserved.

***

### `--minify-syntax`

Rewrites JavaScript syntax to shorter equivalent forms. Performs constant folding, dead code elimination, and dozens of other optimizations.

<CodeGroup>
  ```typescript input theme={null}
  // Constant folding
  const x = 1 + 2;
  const s = "foo" + "bar";
  const t = `hello ${123}`;

  // Dead code elimination
  if (false) {
    unreachable();
  }

  // Boolean shortening
  const a = true;
  const b = false;

  // Logical folding
  const c = true && getValue();
  const d = false || getDefault();
  ```

  ```javascript output theme={null}
  const x = 3;
  const s = "foobar";
  const t = "hello 123";
  const a = !0;
  const b = !1;
  const c = getValue();
  const d = getDefault();
  ```
</CodeGroup>

***

### `--minify-identifiers`

Renames local variables and function names to shorter identifiers using frequency-based optimization. The most-used identifiers get the shortest names.

<CodeGroup>
  ```typescript input theme={null}
  function calculateSum(firstNumber: number, secondNumber: number) {
    const result = firstNumber + secondNumber;
    return result;
  }
  ```

  ```javascript output theme={null}
  function a(b,c){const d=b+c;return d}
  ```
</CodeGroup>

Named exports, `exports`, `module`, JavaScript keywords, and global identifiers are never renamed.

## Combined example

Using all three modes together:

<CodeGroup>
  ```typescript input.ts (158 bytes) theme={null}
  const myVariable = 42;

  const myFunction = () => {
    const isValid = true;
    const result = undefined;
    return isValid ? myVariable : result;
  };

  const output = myFunction();
  ```

  ```javascript output.js (49 bytes — 69% reduction) theme={null}
  const a=42,b=()=>{const c=!0,d=void 0;return c?a:d},e=b();
  ```
</CodeGroup>

## Syntax transformations reference

<AccordionGroup>
  <Accordion title="Boolean and logical simplification">
    ```typescript theme={null}
    // Input
    true;   false;
    !!x;    x === true;    x && true;    x || false;

    // Output
    !0;     !1;
    x;      x;             x;            x;
    ```
  </Accordion>

  <Accordion title="Constant folding — arithmetic">
    ```typescript theme={null}
    // Input
    1 + 2;   10 - 5;   3 * 4;   10 / 2;   2 ** 3;

    // Output
    3;       5;        12;      5;        8;
    ```
  </Accordion>

  <Accordion title="Constant folding — strings">
    ```typescript theme={null}
    // Input
    "foo" + "bar";   "hello " + "world";   `result: ${5 + 10}`;

    // Output
    "foobarbaz";     "hello world";         "result: 15";
    ```
  </Accordion>

  <Accordion title="Dead code elimination">
    ```typescript theme={null}
    // Input
    if (false) { unreachable(); }
    while (false) { neverRuns(); }
    function foo() { return x; deadCode(); }

    // Output
    function foo() { return x; }
    ```
  </Accordion>

  <Accordion title="Undefined and Infinity shortening">
    ```typescript theme={null}
    // Input
    undefined;   Infinity;   -Infinity;

    // Output
    void 0;      1/0;        -1/0;
    ```
  </Accordion>

  <Accordion title="Typeof optimizations">
    ```typescript theme={null}
    // Input
    typeof x === 'undefined';
    typeof require;
    typeof null;
    typeof true;

    // Output
    typeof x > 'u';
    "function";
    "object";
    "boolean";
    ```
  </Accordion>

  <Accordion title="Number formatting">
    ```typescript theme={null}
    // Input
    10000;   100000;   1.0;   -42.0;

    // Output
    1e4;     1e5;      1;     -42;
    ```
  </Accordion>

  <Accordion title="TypeScript enum inlining">
    ```typescript theme={null}
    // Input
    enum Color { Red, Green, Blue }
    const x = Color.Red;

    // Output (enum is eliminated)
    const x = 0;
    ```
  </Accordion>

  <Accordion title="Variable declaration merging">
    ```typescript theme={null}
    // Input
    let a = 1;
    let b = 2;
    const c = 3;
    const d = 4;

    // Output
    let a=1,b=2;
    const c=3,d=4;
    ```
  </Accordion>

  <Accordion title="If statement optimization">
    ```typescript theme={null}
    // Input
    if (true) x;
    if (false) x;
    if (x) {} else y;

    // Output
    x;
    // removed
    if (!x) y;
    ```
  </Accordion>
</AccordionGroup>

## Source maps with minification

Always use source maps in production so that error stack traces point to original source:

<Tabs>
  <Tab title="CLI">
    ```bash theme={null}
    bun build ./index.ts --outdir ./out --minify --sourcemap=linked
    ```
  </Tab>

  <Tab title="JavaScript API">
    ```typescript theme={null}
    await Bun.build({
      entrypoints: ["./index.ts"],
      outdir: "./out",
      minify: true,
      sourcemap: "linked",
    });
    ```
  </Tab>
</Tabs>

See [Bundler Overview — sourcemaps](/bundler/overview#sourcemaps) for all sourcemap options.

## Bytecode compilation

Combine minification with `--bytecode` to also reduce startup time:

<Tabs>
  <Tab title="CLI">
    ```bash theme={null}
    bun build ./index.ts --outdir ./out --minify --sourcemap --bytecode --target=bun
    ```
  </Tab>

  <Tab title="JavaScript API">
    ```typescript theme={null}
    await Bun.build({
      entrypoints: ["./index.ts"],
      outdir: "./out",
      minify: true,
      sourcemap: "linked",
      bytecode: true,
      target: "bun",
    });
    ```
  </Tab>
</Tabs>

Bytecode pre-compiles JavaScript to JavaScriptCore bytecode at build time, moving parsing overhead out of the critical startup path. Typical improvement: 1.5–4x faster startup depending on application size.

Bytecode caching generates a `.jsc` file alongside each `.js` bundle. Bun automatically uses it at runtime:

```bash theme={null}
ls dist/
# index.js   (245 KB)
# index.jsc  (1.1 MB)

bun ./dist/index.js  # uses index.jsc automatically
```

<Note>
  Bytecode is not portable across Bun versions. The `.jsc` file is tied to the JavaScriptCore version embedded in the Bun binary. Regenerate bytecode after updating Bun.
</Note>

## Keep names

When minifying identifiers, preserve original function and class names for better stack traces:

<Tabs>
  <Tab title="CLI">
    ```bash theme={null}
    bun build ./index.ts --outdir ./out --minify --keep-names
    ```
  </Tab>

  <Tab title="JavaScript API">
    ```typescript theme={null}
    await Bun.build({
      entrypoints: ["./index.ts"],
      outdir: "./out",
      minify: {
        identifiers: true,
        keepNames: true,
      },
    });
    ```
  </Tab>
</Tabs>

This preserves the `.name` property on functions and classes while still shortening identifier names in the code.

## Drop specific calls

Remove `console.*` calls or `debugger` statements from production builds:

<Tabs>
  <Tab title="CLI">
    ```bash theme={null}
    bun build ./index.ts --outdir ./out --drop=console --drop=debugger
    ```
  </Tab>

  <Tab title="JavaScript API">
    ```typescript theme={null}
    await Bun.build({
      entrypoints: ["./index.ts"],
      outdir: "./out",
      drop: ["console", "debugger"],
    });
    ```
  </Tab>
</Tabs>

You can also drop custom function calls by name:

```bash theme={null}
bun build ./index.ts --outdir ./out --drop=assert
```

## When to use each mode

<CardGroup cols={3}>
  <Card title="--minify-whitespace" icon="minimize">
    Quick size reduction without any semantic changes. Safe to use on its own without full syntax minification.
  </Card>

  <Card title="--minify-syntax" icon="code">
    Smaller output while keeping readable identifier names. Good for debugging production issues.
  </Card>

  <Card title="--minify-identifiers" icon="tag">
    Maximum compression. Combine with `--keep-names` for better stack traces.
  </Card>
</CardGroup>

<Tip>
  For production CLI tools and long-running servers, use `--minify --sourcemap=linked --bytecode` together for the best combination of small output and fast startup.
</Tip>

<Note>
  Avoid minification for development builds — it makes errors harder to debug and provides no benefit during development.
</Note>
