Minify

Toggle code minification for production builds

Minify

minify is a simple boolean toggle that turns minification on or off. When enabled, the active transformer (esbuild, swc, or oxc — whichever you configured via the transformer option) minifies the emitted output. Packem does not ship a separate minifier or a minifier-selection mechanism: the minifier is the transformer.

minify?: boolean

There is no string form ('esbuild', 'swc', …), no object form, and no function form. minify is either true or false.

Default behavior

minify defaults to true when NODE_ENV === 'production', and false otherwise. You usually don't need to set it at all — building in production mode enables minification automatically.

import { defineConfig } from '@visulima/packem/config'
import transformer from '@visulima/packem/transformer/esbuild'

export default defineConfig({
  transformer,
  // minify is implicitly true when NODE_ENV === 'production'
})

Enabling minification explicitly

import { defineConfig } from '@visulima/packem/config'
import transformer from '@visulima/packem/transformer/esbuild'

export default defineConfig({
  transformer,
  minify: true,
})

Conditional minification

Drive the toggle from the environment when you want a single config to behave differently across builds:

import { defineConfig } from '@visulima/packem/config'
import transformer from '@visulima/packem/transformer/esbuild'

export default defineConfig({
  transformer,
  minify: process.env.NODE_ENV === 'production',
})

The minifier is your transformer

Because minification is performed by the active transformer, switching the transformer also switches which engine minifies your output. To minify with esbuild:

import { defineConfig } from '@visulima/packem/config'
import transformer from '@visulima/packem/transformer/esbuild'

export default defineConfig({
  transformer,
  minify: true,
})

To minify with swc instead, just import a different transformer:

import { defineConfig } from '@visulima/packem/config'
import transformer from '@visulima/packem/transformer/swc'

export default defineConfig({
  transformer,
  minify: true,
})

The minify: true flag is unchanged — only the imported transformer decides which engine does the work.

Preserving function & class names (keepNames)

When using the esbuild transformer, esbuild can preserve original function/class names through minification (so fn.name and stack traces stay readable). Packem leaves this off by default — matching esbuild's own default — and lets you opt in via the transformer's rollup config. keepNames nests under rollup.esbuild:

import { defineConfig } from '@visulima/packem/config'
import transformer from '@visulima/packem/transformer/esbuild'

export default defineConfig({
  transformer,
  minify: true,
  rollup: {
    esbuild: {
      keepNames: true, // opt in when you rely on runtime name reflection
    },
  },
})

keepNames wraps every name-inferred function/closure in __name(fn, "name") (an Object.defineProperty call). For a named closure created inside a hot function (recursion, per-element, per-call), that defineProperty runs on every invocation — which can make a closure-heavy published (minified) build several times slower than its dev build, where minification (and thus keepNames) is off. Only enable it for packages that genuinely need runtime fn.name/Class.name; stack-trace preservation after minification is usually the consuming app bundler's concern. See issue #207.

HTML / CSS template-literal minification

Minification of HTML and CSS embedded in tagged template literals is a separate, opt-in feature configured under rollup.minifyHTMLLiterals — not via minify:

import { defineConfig } from '@visulima/packem/config'
import transformer from '@visulima/packem/transformer/esbuild'

export default defineConfig({
  transformer,
  rollup: {
    minifyHTMLLiterals: {},
  },
})

See the Minify HTML literals example for details.

CSS minification

CSS minification is not controlled by minify. It is configured through the css option's minifier. See the CSS guide for the full configuration.


Support

Contribute to our work and keep us going

Community is the heart of open source. The success of our packages wouldn't be possible without the incredible contributions of users, testers, and developers who collaborate with us every day.Want to get involved? Here are some tips on how you can make a meaningful impact on our open source projects.

Ready to help us out?

Be sure to check out the package's contribution guidelines first. They'll walk you through the process on how to properly submit an issue or pull request to our repositories.

Submit a pull request

Found something to improve? Fork the repo, make your changes, and open a PR. We review every contribution and provide feedback to help you get merged.

Good first issues

Simple issues suited for people new to open source development, and often a good place to start working on a package.
View good first issues