bun-types 1.3.2-canary.20251106T140813 → 1.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bun.d.ts +102 -6
- package/docs/bundler/bytecode.mdx +465 -0
- package/docs/bundler/css.mdx +1024 -0
- package/docs/bundler/esbuild.mdx +253 -0
- package/docs/bundler/executables.mdx +535 -0
- package/docs/bundler/fullstack.mdx +1064 -0
- package/docs/bundler/hot-reloading.mdx +229 -0
- package/docs/bundler/html-static.mdx +386 -0
- package/docs/bundler/index.mdx +1499 -0
- package/docs/bundler/loaders.mdx +356 -0
- package/docs/bundler/macros.mdx +328 -0
- package/docs/bundler/minifier.mdx +1306 -0
- package/docs/bundler/plugins.mdx +411 -0
- package/docs/feedback.mdx +85 -0
- package/docs/guides/binary/arraybuffer-to-array.mdx +29 -0
- package/docs/guides/binary/arraybuffer-to-blob.mdx +26 -0
- package/docs/guides/binary/arraybuffer-to-buffer.mdx +27 -0
- package/docs/guides/binary/arraybuffer-to-string.mdx +17 -0
- package/docs/guides/binary/arraybuffer-to-typedarray.mdx +41 -0
- package/docs/guides/binary/blob-to-arraybuffer.mdx +16 -0
- package/docs/guides/binary/blob-to-dataview.mdx +16 -0
- package/docs/guides/binary/blob-to-stream.mdx +16 -0
- package/docs/guides/binary/blob-to-string.mdx +17 -0
- package/docs/guides/binary/blob-to-typedarray.mdx +16 -0
- package/docs/guides/binary/buffer-to-arraybuffer.mdx +16 -0
- package/docs/guides/binary/buffer-to-blob.mdx +16 -0
- package/docs/guides/binary/buffer-to-readablestream.mdx +43 -0
- package/docs/guides/binary/buffer-to-string.mdx +27 -0
- package/docs/guides/binary/buffer-to-typedarray.mdx +16 -0
- package/docs/guides/binary/dataview-to-string.mdx +17 -0
- package/docs/guides/binary/typedarray-to-arraybuffer.mdx +27 -0
- package/docs/guides/binary/typedarray-to-blob.mdx +18 -0
- package/docs/guides/binary/typedarray-to-buffer.mdx +16 -0
- package/docs/guides/binary/typedarray-to-dataview.mdx +16 -0
- package/docs/guides/binary/typedarray-to-readablestream.mdx +43 -0
- package/docs/guides/binary/typedarray-to-string.mdx +18 -0
- package/docs/guides/deployment/aws-lambda.mdx +204 -0
- package/docs/guides/deployment/digital-ocean.mdx +161 -0
- package/docs/guides/deployment/google-cloud-run.mdx +197 -0
- package/docs/guides/deployment/railway.mdx +145 -0
- package/docs/guides/deployment/render.mdx +82 -0
- package/docs/guides/deployment/vercel.mdx +99 -0
- package/docs/guides/ecosystem/astro.mdx +82 -0
- package/docs/guides/ecosystem/discordjs.mdx +80 -0
- package/docs/guides/ecosystem/docker.mdx +151 -0
- package/docs/guides/ecosystem/drizzle.mdx +195 -0
- package/docs/guides/ecosystem/edgedb.mdx +257 -0
- package/docs/guides/ecosystem/elysia.mdx +31 -0
- package/docs/guides/ecosystem/express.mdx +43 -0
- package/docs/guides/ecosystem/hono.mdx +47 -0
- package/docs/guides/ecosystem/mongoose.mdx +92 -0
- package/docs/guides/ecosystem/neon-drizzle.mdx +234 -0
- package/docs/guides/ecosystem/neon-serverless-postgres.mdx +60 -0
- package/docs/guides/ecosystem/nextjs.mdx +57 -0
- package/docs/guides/ecosystem/nuxt.mdx +90 -0
- package/docs/guides/ecosystem/pm2.mdx +55 -0
- package/docs/guides/ecosystem/prisma-postgres.mdx +169 -0
- package/docs/guides/ecosystem/prisma.mdx +164 -0
- package/docs/guides/ecosystem/qwik.mdx +114 -0
- package/docs/guides/ecosystem/react.mdx +52 -0
- package/docs/guides/ecosystem/remix.mdx +97 -0
- package/docs/guides/ecosystem/sentry.mdx +54 -0
- package/docs/guides/ecosystem/solidstart.mdx +66 -0
- package/docs/guides/ecosystem/ssr-react.mdx +49 -0
- package/docs/guides/ecosystem/stric.mdx +54 -0
- package/docs/guides/ecosystem/sveltekit.mdx +138 -0
- package/docs/guides/ecosystem/systemd.mdx +114 -0
- package/docs/guides/ecosystem/upstash.mdx +87 -0
- package/docs/guides/ecosystem/vite.mdx +77 -0
- package/docs/guides/html-rewriter/extract-links.mdx +72 -0
- package/docs/guides/html-rewriter/extract-social-meta.mdx +97 -0
- package/docs/guides/http/cluster.mdx +69 -0
- package/docs/guides/http/fetch-unix.mdx +35 -0
- package/docs/guides/http/fetch.mdx +26 -0
- package/docs/guides/http/file-uploads.mdx +97 -0
- package/docs/guides/http/hot.mdx +28 -0
- package/docs/guides/http/proxy.mdx +26 -0
- package/docs/guides/http/server.mdx +48 -0
- package/docs/guides/http/simple.mdx +20 -0
- package/docs/guides/http/stream-file.mdx +50 -0
- package/docs/guides/http/stream-iterator.mdx +49 -0
- package/docs/guides/http/stream-node-streams-in-bun.mdx +22 -0
- package/docs/guides/http/tls.mdx +32 -0
- package/docs/guides/index.mdx +10 -0
- package/docs/guides/install/add-dev.mdx +28 -0
- package/docs/guides/install/add-git.mdx +38 -0
- package/docs/guides/install/add-optional.mdx +27 -0
- package/docs/guides/install/add-peer.mdx +45 -0
- package/docs/guides/install/add-tarball.mdx +35 -0
- package/docs/guides/install/add.mdx +44 -0
- package/docs/guides/install/azure-artifacts.mdx +76 -0
- package/docs/guides/install/cicd.mdx +43 -0
- package/docs/guides/install/custom-registry.mdx +32 -0
- package/docs/guides/install/from-npm-install-to-bun-install.mdx +230 -0
- package/docs/guides/install/git-diff-bun-lockfile.mdx +47 -0
- package/docs/guides/install/jfrog-artifactory.mdx +28 -0
- package/docs/guides/install/npm-alias.mdx +25 -0
- package/docs/guides/install/registry-scope.mdx +40 -0
- package/docs/guides/install/trusted.mdx +50 -0
- package/docs/guides/install/workspaces.mdx +70 -0
- package/docs/guides/install/yarnlock.mdx +50 -0
- package/docs/guides/process/argv.mdx +66 -0
- package/docs/guides/process/ctrl-c.mdx +18 -0
- package/docs/guides/process/ipc.mdx +69 -0
- package/docs/guides/process/nanoseconds.mdx +15 -0
- package/docs/guides/process/os-signals.mdx +41 -0
- package/docs/guides/process/spawn-stderr.mdx +34 -0
- package/docs/guides/process/spawn-stdout.mdx +28 -0
- package/docs/guides/process/spawn.mdx +43 -0
- package/docs/guides/process/stdin.mdx +62 -0
- package/docs/guides/read-file/arraybuffer.mdx +30 -0
- package/docs/guides/read-file/buffer.mdx +21 -0
- package/docs/guides/read-file/exists.mdx +18 -0
- package/docs/guides/read-file/json.mdx +19 -0
- package/docs/guides/read-file/mime.mdx +22 -0
- package/docs/guides/read-file/stream.mdx +28 -0
- package/docs/guides/read-file/string.mdx +24 -0
- package/docs/guides/read-file/uint8array.mdx +23 -0
- package/docs/guides/read-file/watch.mdx +66 -0
- package/docs/guides/runtime/build-time-constants.mdx +295 -0
- package/docs/guides/runtime/cicd.mdx +45 -0
- package/docs/guides/runtime/codesign-macos-executable.mdx +61 -0
- package/docs/guides/runtime/define-constant.mdx +149 -0
- package/docs/guides/runtime/delete-directory.mdx +39 -0
- package/docs/guides/runtime/delete-file.mdx +21 -0
- package/docs/guides/runtime/heap-snapshot.mdx +28 -0
- package/docs/guides/runtime/import-html.mdx +17 -0
- package/docs/guides/runtime/import-json.mdx +46 -0
- package/docs/guides/runtime/import-toml.mdx +32 -0
- package/docs/guides/runtime/import-yaml.mdx +104 -0
- package/docs/guides/runtime/read-env.mdx +37 -0
- package/docs/guides/runtime/set-env.mdx +51 -0
- package/docs/guides/runtime/shell.mdx +42 -0
- package/docs/guides/runtime/timezone.mdx +38 -0
- package/docs/guides/runtime/tsconfig-paths.mdx +31 -0
- package/docs/guides/runtime/typescript.mdx +51 -0
- package/docs/guides/runtime/vscode-debugger.mdx +48 -0
- package/docs/guides/runtime/web-debugger.mdx +103 -0
- package/docs/guides/streams/node-readable-to-arraybuffer.mdx +13 -0
- package/docs/guides/streams/node-readable-to-blob.mdx +13 -0
- package/docs/guides/streams/node-readable-to-json.mdx +14 -0
- package/docs/guides/streams/node-readable-to-string.mdx +14 -0
- package/docs/guides/streams/node-readable-to-uint8array.mdx +13 -0
- package/docs/guides/streams/to-array.mdx +16 -0
- package/docs/guides/streams/to-arraybuffer.mdx +16 -0
- package/docs/guides/streams/to-blob.mdx +16 -0
- package/docs/guides/streams/to-buffer.mdx +17 -0
- package/docs/guides/streams/to-json.mdx +16 -0
- package/docs/guides/streams/to-string.mdx +16 -0
- package/docs/guides/streams/to-typedarray.mdx +24 -0
- package/docs/guides/test/bail.mdx +24 -0
- package/docs/guides/test/coverage-threshold.mdx +67 -0
- package/docs/guides/test/coverage.mdx +49 -0
- package/docs/guides/test/happy-dom.mdx +73 -0
- package/docs/guides/test/migrate-from-jest.mdx +125 -0
- package/docs/guides/test/mock-clock.mdx +50 -0
- package/docs/guides/test/mock-functions.mdx +70 -0
- package/docs/guides/test/rerun-each.mdx +16 -0
- package/docs/guides/test/run-tests.mdx +116 -0
- package/docs/guides/test/skip-tests.mdx +43 -0
- package/docs/guides/test/snapshot.mdx +102 -0
- package/docs/guides/test/spy-on.mdx +49 -0
- package/docs/guides/test/svelte-test.mdx +113 -0
- package/docs/guides/test/testing-library.mdx +93 -0
- package/docs/guides/test/timeout.mdx +17 -0
- package/docs/guides/test/todo-tests.mdx +74 -0
- package/docs/guides/test/update-snapshots.mdx +49 -0
- package/docs/guides/test/watch-mode.mdx +24 -0
- package/docs/guides/util/base64.mdx +17 -0
- package/docs/guides/util/deep-equals.mdx +41 -0
- package/docs/guides/util/deflate.mdx +20 -0
- package/docs/guides/util/detect-bun.mdx +25 -0
- package/docs/guides/util/entrypoint.mdx +19 -0
- package/docs/guides/util/escape-html.mdx +24 -0
- package/docs/guides/util/file-url-to-path.mdx +16 -0
- package/docs/guides/util/gzip.mdx +20 -0
- package/docs/guides/util/hash-a-password.mdx +56 -0
- package/docs/guides/util/import-meta-dir.mdx +15 -0
- package/docs/guides/util/import-meta-file.mdx +15 -0
- package/docs/guides/util/import-meta-path.mdx +15 -0
- package/docs/guides/util/javascript-uuid.mdx +25 -0
- package/docs/guides/util/main.mdx +43 -0
- package/docs/guides/util/path-to-file-url.mdx +16 -0
- package/docs/guides/util/sleep.mdx +24 -0
- package/docs/guides/util/version.mdx +23 -0
- package/docs/guides/util/which-path-to-executable-bin.mdx +17 -0
- package/docs/guides/websocket/compression.mdx +33 -0
- package/docs/guides/websocket/context.mdx +74 -0
- package/docs/guides/websocket/pubsub.mdx +40 -0
- package/docs/guides/websocket/simple.mdx +35 -0
- package/docs/guides/write-file/append.mdx +54 -0
- package/docs/guides/write-file/basic.mdx +46 -0
- package/docs/guides/write-file/blob.mdx +30 -0
- package/docs/guides/write-file/cat.mdx +19 -0
- package/docs/guides/write-file/file-cp.mdx +18 -0
- package/docs/guides/write-file/filesink.mdx +54 -0
- package/docs/guides/write-file/response.mdx +19 -0
- package/docs/guides/write-file/stdout.mdx +23 -0
- package/docs/guides/write-file/stream.mdx +19 -0
- package/docs/guides/write-file/unlink.mdx +18 -0
- package/docs/index.mdx +133 -0
- package/docs/installation.mdx +365 -0
- package/docs/pm/bunx.mdx +83 -0
- package/docs/pm/catalogs.mdx +292 -0
- package/docs/pm/cli/add.mdx +179 -0
- package/docs/pm/cli/audit.mdx +60 -0
- package/docs/pm/cli/install.mdx +471 -0
- package/docs/pm/cli/link.mdx +48 -0
- package/docs/pm/cli/outdated.mdx +197 -0
- package/docs/pm/cli/patch.mdx +69 -0
- package/docs/pm/cli/pm.mdx +319 -0
- package/docs/pm/cli/publish.mdx +123 -0
- package/docs/pm/cli/remove.mdx +16 -0
- package/docs/pm/cli/update.mdx +140 -0
- package/docs/pm/cli/why.mdx +84 -0
- package/docs/pm/filter.mdx +102 -0
- package/docs/pm/global-cache.mdx +72 -0
- package/docs/pm/isolated-installs.mdx +205 -0
- package/docs/pm/lifecycle.mdx +57 -0
- package/docs/pm/lockfile.mdx +64 -0
- package/docs/pm/npmrc.mdx +111 -0
- package/docs/pm/overrides.mdx +83 -0
- package/docs/pm/scopes-registries.mdx +35 -0
- package/docs/pm/security-scanner-api.mdx +95 -0
- package/docs/pm/workspaces.mdx +109 -0
- package/docs/project/benchmarking.mdx +218 -0
- package/docs/project/bindgen.mdx +223 -0
- package/docs/project/building-windows.mdx +133 -0
- package/docs/project/contributing.mdx +343 -0
- package/docs/project/feedback.mdx +20 -0
- package/docs/project/license.mdx +78 -0
- package/docs/project/roadmap.mdx +8 -0
- package/docs/quickstart.mdx +240 -0
- package/docs/runtime/auto-install.mdx +97 -0
- package/docs/runtime/binary-data.mdx +846 -0
- package/docs/runtime/bun-apis.mdx +59 -0
- package/docs/runtime/bunfig.mdx +642 -0
- package/docs/runtime/c-compiler.mdx +204 -0
- package/docs/runtime/child-process.mdx +532 -0
- package/docs/runtime/color.mdx +267 -0
- package/docs/runtime/console.mdx +67 -0
- package/docs/runtime/cookies.mdx +454 -0
- package/docs/runtime/debugger.mdx +335 -0
- package/docs/runtime/environment-variables.mdx +214 -0
- package/docs/runtime/ffi.mdx +565 -0
- package/docs/runtime/file-io.mdx +306 -0
- package/docs/runtime/file-system-router.mdx +118 -0
- package/docs/runtime/file-types.mdx +354 -0
- package/docs/runtime/glob.mdx +181 -0
- package/docs/runtime/globals.mdx +72 -0
- package/docs/runtime/hashing.mdx +315 -0
- package/docs/runtime/html-rewriter.mdx +340 -0
- package/docs/runtime/http/cookies.mdx +79 -0
- package/docs/runtime/http/error-handling.mdx +40 -0
- package/docs/runtime/http/metrics.mdx +36 -0
- package/docs/runtime/http/routing.mdx +289 -0
- package/docs/runtime/http/server.mdx +647 -0
- package/docs/runtime/http/tls.mdx +101 -0
- package/docs/runtime/http/websockets.mdx +404 -0
- package/docs/runtime/index.mdx +223 -0
- package/docs/runtime/jsx.mdx +115 -0
- package/docs/runtime/module-resolution.mdx +342 -0
- package/docs/runtime/networking/dns.mdx +111 -0
- package/docs/runtime/networking/fetch.mdx +468 -0
- package/docs/runtime/networking/tcp.mdx +239 -0
- package/docs/runtime/networking/udp.mdx +129 -0
- package/docs/runtime/node-api.mdx +19 -0
- package/docs/runtime/nodejs-compat.mdx +468 -0
- package/docs/runtime/plugins.mdx +405 -0
- package/docs/runtime/redis.mdx +582 -0
- package/docs/runtime/s3.mdx +863 -0
- package/docs/runtime/secrets.mdx +336 -0
- package/docs/runtime/semver.mdx +57 -0
- package/docs/runtime/shell.mdx +637 -0
- package/docs/runtime/sql.mdx +1404 -0
- package/docs/runtime/sqlite.mdx +699 -0
- package/docs/runtime/streams.mdx +232 -0
- package/docs/runtime/templating/create.mdx +269 -0
- package/docs/runtime/templating/init.mdx +58 -0
- package/docs/runtime/transpiler.mdx +288 -0
- package/docs/runtime/typescript.mdx +58 -0
- package/docs/runtime/utils.mdx +922 -0
- package/docs/runtime/watch-mode.mdx +161 -0
- package/docs/runtime/web-apis.mdx +29 -0
- package/docs/runtime/workers.mdx +328 -0
- package/docs/runtime/yaml.mdx +469 -0
- package/docs/snippets/cli/add.mdx +166 -0
- package/docs/snippets/cli/build.mdx +196 -0
- package/docs/snippets/cli/feedback.mdx +17 -0
- package/docs/snippets/cli/init.mdx +84 -0
- package/docs/snippets/cli/install.mdx +173 -0
- package/docs/snippets/cli/link.mdx +163 -0
- package/docs/snippets/cli/outdated.mdx +140 -0
- package/docs/snippets/cli/patch.mdx +171 -0
- package/docs/snippets/cli/publish.mdx +198 -0
- package/docs/snippets/cli/remove.mdx +146 -0
- package/docs/snippets/cli/run.mdx +293 -0
- package/docs/snippets/cli/test.mdx +100 -0
- package/docs/snippets/cli/update.mdx +144 -0
- package/docs/snippets/product-card.mdx +32 -0
- package/docs/snippets/product-tiles.mdx +94 -0
- package/docs/test/code-coverage.mdx +409 -0
- package/docs/test/configuration.mdx +467 -0
- package/docs/test/dates-times.mdx +129 -0
- package/docs/test/discovery.mdx +90 -0
- package/docs/test/dom.mdx +226 -0
- package/docs/test/index.mdx +380 -0
- package/docs/test/lifecycle.mdx +348 -0
- package/docs/test/mocks.mdx +637 -0
- package/docs/test/reporters.mdx +117 -0
- package/docs/test/runtime-behavior.mdx +342 -0
- package/docs/test/snapshots.mdx +434 -0
- package/docs/test/writing-tests.mdx +635 -0
- package/docs/typescript.mdx +54 -0
- package/package.json +8 -6
- package/test.d.ts +2 -2
|
@@ -0,0 +1,1306 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Minifier
|
|
3
|
+
description: Reduce bundle sizes with Bun's JavaScript and TypeScript minifier
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
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.
|
|
7
|
+
|
|
8
|
+
## CLI Usage
|
|
9
|
+
|
|
10
|
+
### Enable all minification
|
|
11
|
+
|
|
12
|
+
Use the `--minify` flag to enable all minification modes:
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
bun build ./index.ts --minify --outfile=out.js
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
The `--minify` flag automatically enables:
|
|
19
|
+
|
|
20
|
+
- Whitespace minification
|
|
21
|
+
- Syntax minification
|
|
22
|
+
- Identifier minification
|
|
23
|
+
|
|
24
|
+
### Production mode
|
|
25
|
+
|
|
26
|
+
The `--production` flag automatically enables minification:
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
bun build ./index.ts --production --outfile=out.js
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
The `--production` flag also:
|
|
33
|
+
|
|
34
|
+
- Sets `process.env.NODE_ENV` to `production`
|
|
35
|
+
- Enables the production-mode JSX import & transform
|
|
36
|
+
|
|
37
|
+
### Granular control
|
|
38
|
+
|
|
39
|
+
You can enable specific minification modes individually:
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
# Only remove whitespace
|
|
43
|
+
bun build ./index.ts --minify-whitespace --outfile=out.js
|
|
44
|
+
|
|
45
|
+
# Only minify syntax
|
|
46
|
+
bun build ./index.ts --minify-syntax --outfile=out.js
|
|
47
|
+
|
|
48
|
+
# Only minify identifiers
|
|
49
|
+
bun build ./index.ts --minify-identifiers --outfile=out.js
|
|
50
|
+
|
|
51
|
+
# Combine specific modes
|
|
52
|
+
bun build ./index.ts --minify-whitespace --minify-syntax --outfile=out.js
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## JavaScript API
|
|
56
|
+
|
|
57
|
+
When using Bun's bundler programmatically, configure minification through the `minify` option:
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
await Bun.build({
|
|
61
|
+
entrypoints: ["./index.ts"],
|
|
62
|
+
outdir: "./out",
|
|
63
|
+
minify: true, // Enable all minification modes
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
For granular control, pass an object:
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
await Bun.build({
|
|
71
|
+
entrypoints: ["./index.ts"],
|
|
72
|
+
outdir: "./out",
|
|
73
|
+
minify: {
|
|
74
|
+
whitespace: true,
|
|
75
|
+
syntax: true,
|
|
76
|
+
identifiers: true,
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Minification Modes
|
|
82
|
+
|
|
83
|
+
Bun's minifier has three independent modes that can be enabled separately or combined.
|
|
84
|
+
|
|
85
|
+
### Whitespace minification (`--minify-whitespace`)
|
|
86
|
+
|
|
87
|
+
Removes all unnecessary whitespace, newlines, and formatting from the output.
|
|
88
|
+
|
|
89
|
+
### Syntax minification (`--minify-syntax`)
|
|
90
|
+
|
|
91
|
+
Rewrites JavaScript syntax to shorter equivalent forms and performs constant folding, dead code elimination, and other optimizations.
|
|
92
|
+
|
|
93
|
+
### Identifier minification (`--minify-identifiers`)
|
|
94
|
+
|
|
95
|
+
Renames local variables and function names to shorter identifiers using frequency-based optimization.
|
|
96
|
+
|
|
97
|
+
## All Transformations
|
|
98
|
+
|
|
99
|
+
### Boolean literal shortening
|
|
100
|
+
|
|
101
|
+
**Mode:** `--minify-syntax`
|
|
102
|
+
|
|
103
|
+
Converts boolean literals to shorter expressions.
|
|
104
|
+
|
|
105
|
+
```ts Input
|
|
106
|
+
true;
|
|
107
|
+
false;
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
```ts Output
|
|
111
|
+
!0;
|
|
112
|
+
!1;
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Boolean algebra optimizations
|
|
116
|
+
|
|
117
|
+
**Mode:** `--minify-syntax`
|
|
118
|
+
|
|
119
|
+
Simplifies boolean expressions using logical rules.
|
|
120
|
+
|
|
121
|
+
```ts Input
|
|
122
|
+
!!x;
|
|
123
|
+
x === true;
|
|
124
|
+
x && true;
|
|
125
|
+
x || false;
|
|
126
|
+
!true;
|
|
127
|
+
!false;
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
```ts Output
|
|
131
|
+
x;
|
|
132
|
+
x;
|
|
133
|
+
x;
|
|
134
|
+
x;
|
|
135
|
+
!1;
|
|
136
|
+
!0;
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### Undefined shortening
|
|
140
|
+
|
|
141
|
+
**Mode:** `--minify-syntax`
|
|
142
|
+
|
|
143
|
+
Replaces `undefined` with shorter equivalent.
|
|
144
|
+
|
|
145
|
+
```ts Input
|
|
146
|
+
undefined;
|
|
147
|
+
let x = undefined;
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
```ts Output
|
|
151
|
+
void 0;
|
|
152
|
+
let x = void 0;
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### Undefined equality optimization
|
|
156
|
+
|
|
157
|
+
**Mode:** `--minify-syntax`
|
|
158
|
+
|
|
159
|
+
Optimizes loose equality checks with undefined.
|
|
160
|
+
|
|
161
|
+
```ts Input
|
|
162
|
+
x == undefined;
|
|
163
|
+
x != undefined;
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
```ts Output
|
|
167
|
+
x == null;
|
|
168
|
+
x != null;
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### Infinity shortening
|
|
172
|
+
|
|
173
|
+
**Mode:** `--minify-syntax`
|
|
174
|
+
|
|
175
|
+
Converts Infinity to mathematical expressions.
|
|
176
|
+
|
|
177
|
+
```ts Input
|
|
178
|
+
Infinity - Infinity;
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
```ts Output
|
|
182
|
+
1 / 0 - 1 / 0;
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
### Typeof optimizations
|
|
186
|
+
|
|
187
|
+
**Mode:** `--minify-syntax`
|
|
188
|
+
|
|
189
|
+
Optimizes typeof comparisons and evaluates constant typeof expressions.
|
|
190
|
+
|
|
191
|
+
```ts Input
|
|
192
|
+
typeof x === "undefined";
|
|
193
|
+
typeof x !== "undefined";
|
|
194
|
+
typeof require;
|
|
195
|
+
typeof null;
|
|
196
|
+
typeof true;
|
|
197
|
+
typeof 123;
|
|
198
|
+
typeof "str";
|
|
199
|
+
typeof 123n;
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
```ts Output
|
|
203
|
+
typeof x > "u";
|
|
204
|
+
typeof x < "u";
|
|
205
|
+
("function");
|
|
206
|
+
("object");
|
|
207
|
+
("boolean");
|
|
208
|
+
("number");
|
|
209
|
+
("string");
|
|
210
|
+
("bigint");
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
### Number formatting
|
|
214
|
+
|
|
215
|
+
**Mode:** `--minify-syntax`
|
|
216
|
+
|
|
217
|
+
Formats numbers in the most compact representation.
|
|
218
|
+
|
|
219
|
+
```ts Input
|
|
220
|
+
10000;
|
|
221
|
+
100000;
|
|
222
|
+
1000000;
|
|
223
|
+
1.0 - 42.0;
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
```ts Output
|
|
227
|
+
1e4;
|
|
228
|
+
1e5;
|
|
229
|
+
1e6;
|
|
230
|
+
1 - 42;
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
### Arithmetic constant folding
|
|
234
|
+
|
|
235
|
+
**Mode:** `--minify-syntax`
|
|
236
|
+
|
|
237
|
+
Evaluates arithmetic operations at compile time.
|
|
238
|
+
|
|
239
|
+
```ts Input
|
|
240
|
+
1 + 2;
|
|
241
|
+
10 - 5;
|
|
242
|
+
3 * 4;
|
|
243
|
+
10 / 2;
|
|
244
|
+
10 % 3;
|
|
245
|
+
2 ** 3;
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
```ts Output
|
|
249
|
+
3;
|
|
250
|
+
5;
|
|
251
|
+
12;
|
|
252
|
+
5;
|
|
253
|
+
1;
|
|
254
|
+
8;
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
### Bitwise constant folding
|
|
258
|
+
|
|
259
|
+
**Mode:** `--minify-syntax`
|
|
260
|
+
|
|
261
|
+
Evaluates bitwise operations at compile time.
|
|
262
|
+
|
|
263
|
+
```ts Input
|
|
264
|
+
5 & 3;
|
|
265
|
+
5 | 3;
|
|
266
|
+
5 ^ 3;
|
|
267
|
+
8 << 2;
|
|
268
|
+
32 >> 2;
|
|
269
|
+
~5;
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
```ts Output
|
|
273
|
+
1;
|
|
274
|
+
7;
|
|
275
|
+
6;
|
|
276
|
+
32;
|
|
277
|
+
8 - 6;
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
### String concatenation
|
|
281
|
+
|
|
282
|
+
**Mode:** `--minify-syntax`
|
|
283
|
+
|
|
284
|
+
Combines string literals at compile time.
|
|
285
|
+
|
|
286
|
+
```ts Input
|
|
287
|
+
"a" + "b";
|
|
288
|
+
"x" + 123;
|
|
289
|
+
"foo" + "bar" + "baz";
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
```ts Output
|
|
293
|
+
"ab";
|
|
294
|
+
"x123";
|
|
295
|
+
"foobarbaz";
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
### String indexing
|
|
299
|
+
|
|
300
|
+
**Mode:** `--minify-syntax`
|
|
301
|
+
|
|
302
|
+
Evaluates string character access at compile time.
|
|
303
|
+
|
|
304
|
+
```ts Input
|
|
305
|
+
"foo"[2];
|
|
306
|
+
"hello"[0];
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
```ts Output
|
|
310
|
+
"o";
|
|
311
|
+
"h";
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
### Template literal folding
|
|
315
|
+
|
|
316
|
+
**Mode:** `--minify-syntax`
|
|
317
|
+
|
|
318
|
+
Evaluates template literals with constant expressions.
|
|
319
|
+
|
|
320
|
+
```ts Input
|
|
321
|
+
`a${123}b``result: ${5 + 10}`;
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
```ts Output
|
|
325
|
+
"a123b";
|
|
326
|
+
"result: 15";
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
### Template literal to string conversion
|
|
330
|
+
|
|
331
|
+
**Mode:** `--minify-syntax`
|
|
332
|
+
|
|
333
|
+
Converts simple template literals to regular strings.
|
|
334
|
+
|
|
335
|
+
```ts Input
|
|
336
|
+
`Hello World``Line 1
|
|
337
|
+
Line 2`;
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
```ts Output
|
|
341
|
+
"Hello World";
|
|
342
|
+
"Line 1\nLine 2";
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
### String quote optimization
|
|
346
|
+
|
|
347
|
+
**Mode:** `--minify-syntax`
|
|
348
|
+
|
|
349
|
+
Chooses the optimal quote character to minimize escapes.
|
|
350
|
+
|
|
351
|
+
```ts Input
|
|
352
|
+
"It's a string";
|
|
353
|
+
'He said "hello"'`Simple string`;
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
```ts Output
|
|
357
|
+
"It's a string";
|
|
358
|
+
'He said "hello"';
|
|
359
|
+
"Simple string";
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
### Array spread inlining
|
|
363
|
+
|
|
364
|
+
**Mode:** `--minify-syntax`
|
|
365
|
+
|
|
366
|
+
Inlines array spread operations with constant arrays.
|
|
367
|
+
|
|
368
|
+
```ts Input
|
|
369
|
+
[1, ...[2, 3], 4]
|
|
370
|
+
[...[a, b]]
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
```ts Output
|
|
374
|
+
[1, 2, 3, 4][(a, b)];
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
### Array indexing
|
|
378
|
+
|
|
379
|
+
**Mode:** `--minify-syntax`
|
|
380
|
+
|
|
381
|
+
Evaluates constant array access at compile time.
|
|
382
|
+
|
|
383
|
+
```ts Input
|
|
384
|
+
[x][0]
|
|
385
|
+
['a', 'b', 'c'][1]
|
|
386
|
+
['a', , 'c'][1]
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
```ts Output
|
|
390
|
+
x;
|
|
391
|
+
("b");
|
|
392
|
+
void 0;
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
### Property access optimization
|
|
396
|
+
|
|
397
|
+
**Mode:** `--minify-syntax`
|
|
398
|
+
|
|
399
|
+
Converts bracket notation to dot notation when possible.
|
|
400
|
+
|
|
401
|
+
```ts Input
|
|
402
|
+
obj["property"];
|
|
403
|
+
obj["validName"];
|
|
404
|
+
obj["123"];
|
|
405
|
+
obj["invalid-name"];
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
```ts Output
|
|
409
|
+
obj.property;
|
|
410
|
+
obj.validName;
|
|
411
|
+
obj["123"];
|
|
412
|
+
obj["invalid-name"];
|
|
413
|
+
```
|
|
414
|
+
|
|
415
|
+
### Comparison folding
|
|
416
|
+
|
|
417
|
+
**Mode:** `--minify-syntax`
|
|
418
|
+
|
|
419
|
+
Evaluates constant comparisons at compile time.
|
|
420
|
+
|
|
421
|
+
```ts Input
|
|
422
|
+
3 < 5;
|
|
423
|
+
5 > 3;
|
|
424
|
+
3 <= 3;
|
|
425
|
+
5 >= 6;
|
|
426
|
+
"a" < "b";
|
|
427
|
+
```
|
|
428
|
+
|
|
429
|
+
```ts Output
|
|
430
|
+
!0;
|
|
431
|
+
!0;
|
|
432
|
+
!0;
|
|
433
|
+
!1;
|
|
434
|
+
!0;
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
### Logical operation folding
|
|
438
|
+
|
|
439
|
+
**Mode:** `--minify-syntax`
|
|
440
|
+
|
|
441
|
+
Simplifies logical operations with constant values.
|
|
442
|
+
|
|
443
|
+
```ts Input
|
|
444
|
+
true && x;
|
|
445
|
+
false && x;
|
|
446
|
+
true || x;
|
|
447
|
+
false || x;
|
|
448
|
+
```
|
|
449
|
+
|
|
450
|
+
```ts Output
|
|
451
|
+
x;
|
|
452
|
+
!1;
|
|
453
|
+
!0;
|
|
454
|
+
x;
|
|
455
|
+
```
|
|
456
|
+
|
|
457
|
+
### Nullish coalescing folding
|
|
458
|
+
|
|
459
|
+
**Mode:** `--minify-syntax`
|
|
460
|
+
|
|
461
|
+
Evaluates nullish coalescing with known values.
|
|
462
|
+
|
|
463
|
+
```ts Input
|
|
464
|
+
null ?? x;
|
|
465
|
+
undefined ?? x;
|
|
466
|
+
42 ?? x;
|
|
467
|
+
```
|
|
468
|
+
|
|
469
|
+
```ts Output
|
|
470
|
+
x;
|
|
471
|
+
x;
|
|
472
|
+
42;
|
|
473
|
+
```
|
|
474
|
+
|
|
475
|
+
### Comma expression simplification
|
|
476
|
+
|
|
477
|
+
**Mode:** `--minify-syntax`
|
|
478
|
+
|
|
479
|
+
Removes side-effect-free expressions from comma sequences.
|
|
480
|
+
|
|
481
|
+
```ts Input
|
|
482
|
+
(0, x)(123, "str", x);
|
|
483
|
+
```
|
|
484
|
+
|
|
485
|
+
```ts Output
|
|
486
|
+
x;
|
|
487
|
+
x;
|
|
488
|
+
```
|
|
489
|
+
|
|
490
|
+
### Ternary conditional folding
|
|
491
|
+
|
|
492
|
+
**Mode:** `--minify-syntax`
|
|
493
|
+
|
|
494
|
+
Evaluates conditional expressions with constant conditions.
|
|
495
|
+
|
|
496
|
+
```ts Input
|
|
497
|
+
true ? a : b;
|
|
498
|
+
false ? a : b;
|
|
499
|
+
x ? true : false;
|
|
500
|
+
x ? false : true;
|
|
501
|
+
```
|
|
502
|
+
|
|
503
|
+
```ts Output
|
|
504
|
+
a;
|
|
505
|
+
b;
|
|
506
|
+
x;
|
|
507
|
+
!x;
|
|
508
|
+
```
|
|
509
|
+
|
|
510
|
+
### Unary expression folding
|
|
511
|
+
|
|
512
|
+
**Mode:** `--minify-syntax`
|
|
513
|
+
|
|
514
|
+
Simplifies unary operations.
|
|
515
|
+
|
|
516
|
+
```ts Input
|
|
517
|
+
+123 + "123" - -x;
|
|
518
|
+
~~x;
|
|
519
|
+
!!x;
|
|
520
|
+
```
|
|
521
|
+
|
|
522
|
+
```ts Output
|
|
523
|
+
123;
|
|
524
|
+
123;
|
|
525
|
+
x;
|
|
526
|
+
x;
|
|
527
|
+
x;
|
|
528
|
+
```
|
|
529
|
+
|
|
530
|
+
### Double negation removal
|
|
531
|
+
|
|
532
|
+
**Mode:** `--minify-syntax`
|
|
533
|
+
|
|
534
|
+
Removes unnecessary double negations.
|
|
535
|
+
|
|
536
|
+
```ts Input
|
|
537
|
+
!!x;
|
|
538
|
+
!!!x;
|
|
539
|
+
```
|
|
540
|
+
|
|
541
|
+
```ts Output
|
|
542
|
+
x;
|
|
543
|
+
!x;
|
|
544
|
+
```
|
|
545
|
+
|
|
546
|
+
### If statement optimization
|
|
547
|
+
|
|
548
|
+
**Mode:** `--minify-syntax`
|
|
549
|
+
|
|
550
|
+
Optimizes if statements with constant conditions.
|
|
551
|
+
|
|
552
|
+
```ts Input
|
|
553
|
+
if (true) x;
|
|
554
|
+
if (false) x;
|
|
555
|
+
if (x) {
|
|
556
|
+
a;
|
|
557
|
+
}
|
|
558
|
+
if (x) {
|
|
559
|
+
} else y;
|
|
560
|
+
```
|
|
561
|
+
|
|
562
|
+
```ts Output
|
|
563
|
+
x;
|
|
564
|
+
// removed
|
|
565
|
+
if (x) a;
|
|
566
|
+
if (!x) y;
|
|
567
|
+
```
|
|
568
|
+
|
|
569
|
+
### Dead code elimination
|
|
570
|
+
|
|
571
|
+
**Mode:** `--minify-syntax`
|
|
572
|
+
|
|
573
|
+
Removes unreachable code and code without side effects.
|
|
574
|
+
|
|
575
|
+
```ts Input
|
|
576
|
+
if (false) {
|
|
577
|
+
unreachable();
|
|
578
|
+
}
|
|
579
|
+
function foo() {
|
|
580
|
+
return x;
|
|
581
|
+
deadCode();
|
|
582
|
+
}
|
|
583
|
+
```
|
|
584
|
+
|
|
585
|
+
```ts Output
|
|
586
|
+
function foo() {
|
|
587
|
+
return x;
|
|
588
|
+
}
|
|
589
|
+
```
|
|
590
|
+
|
|
591
|
+
### Unreachable branch removal
|
|
592
|
+
|
|
593
|
+
**Mode:** `--minify-syntax`
|
|
594
|
+
|
|
595
|
+
Removes branches that can never execute.
|
|
596
|
+
|
|
597
|
+
```ts Input
|
|
598
|
+
while (false) {
|
|
599
|
+
neverRuns();
|
|
600
|
+
}
|
|
601
|
+
```
|
|
602
|
+
|
|
603
|
+
```ts Output
|
|
604
|
+
// removed entirely
|
|
605
|
+
```
|
|
606
|
+
|
|
607
|
+
### Empty block removal
|
|
608
|
+
|
|
609
|
+
**Mode:** `--minify-syntax`
|
|
610
|
+
|
|
611
|
+
Removes empty blocks and unnecessary braces.
|
|
612
|
+
|
|
613
|
+
```ts Input
|
|
614
|
+
{
|
|
615
|
+
}
|
|
616
|
+
if (x) {
|
|
617
|
+
}
|
|
618
|
+
```
|
|
619
|
+
|
|
620
|
+
```ts Output
|
|
621
|
+
// removed
|
|
622
|
+
```
|
|
623
|
+
|
|
624
|
+
### Single statement block unwrapping
|
|
625
|
+
|
|
626
|
+
**Mode:** `--minify-syntax`
|
|
627
|
+
|
|
628
|
+
Removes unnecessary braces around single statements.
|
|
629
|
+
|
|
630
|
+
```ts Input
|
|
631
|
+
if (condition) {
|
|
632
|
+
doSomething();
|
|
633
|
+
}
|
|
634
|
+
```
|
|
635
|
+
|
|
636
|
+
```ts Output
|
|
637
|
+
if (condition) doSomething();
|
|
638
|
+
```
|
|
639
|
+
|
|
640
|
+
### TypeScript enum inlining
|
|
641
|
+
|
|
642
|
+
**Mode:** `--minify-syntax`
|
|
643
|
+
|
|
644
|
+
Inlines TypeScript enum values at compile time.
|
|
645
|
+
|
|
646
|
+
```ts Input
|
|
647
|
+
enum Color {
|
|
648
|
+
Red,
|
|
649
|
+
Green,
|
|
650
|
+
Blue,
|
|
651
|
+
}
|
|
652
|
+
const x = Color.Red;
|
|
653
|
+
```
|
|
654
|
+
|
|
655
|
+
```ts Output
|
|
656
|
+
const x = 0;
|
|
657
|
+
```
|
|
658
|
+
|
|
659
|
+
### Pure annotation support
|
|
660
|
+
|
|
661
|
+
**Mode:** Always active
|
|
662
|
+
|
|
663
|
+
Respects `/*@__PURE__*/` annotations for tree shaking.
|
|
664
|
+
|
|
665
|
+
```ts Input
|
|
666
|
+
const x = /*@__PURE__*/ expensive();
|
|
667
|
+
// If x is unused...
|
|
668
|
+
```
|
|
669
|
+
|
|
670
|
+
```ts Output
|
|
671
|
+
// removed entirely
|
|
672
|
+
```
|
|
673
|
+
|
|
674
|
+
### Identifier renaming
|
|
675
|
+
|
|
676
|
+
**Mode:** `--minify-identifiers`
|
|
677
|
+
|
|
678
|
+
Renames local variables to shorter names based on usage frequency.
|
|
679
|
+
|
|
680
|
+
```ts Input
|
|
681
|
+
function calculateSum(firstNumber, secondNumber) {
|
|
682
|
+
const result = firstNumber + secondNumber;
|
|
683
|
+
return result;
|
|
684
|
+
}
|
|
685
|
+
```
|
|
686
|
+
|
|
687
|
+
```ts Output
|
|
688
|
+
function a(b, c) {
|
|
689
|
+
const d = b + c;
|
|
690
|
+
return d;
|
|
691
|
+
}
|
|
692
|
+
```
|
|
693
|
+
|
|
694
|
+
**Naming strategy:**
|
|
695
|
+
|
|
696
|
+
- Most frequently used identifiers get the shortest names (a, b, c...)
|
|
697
|
+
- Single letters: a-z (26 names)
|
|
698
|
+
- Double letters: aa-zz (676 names)
|
|
699
|
+
- Triple letters and beyond as needed
|
|
700
|
+
|
|
701
|
+
**Preserved identifiers:**
|
|
702
|
+
|
|
703
|
+
- JavaScript keywords and reserved words
|
|
704
|
+
- Global identifiers
|
|
705
|
+
- Named exports (to maintain API)
|
|
706
|
+
- CommonJS names: `exports`, `module`
|
|
707
|
+
|
|
708
|
+
### Whitespace removal
|
|
709
|
+
|
|
710
|
+
**Mode:** `--minify-whitespace`
|
|
711
|
+
|
|
712
|
+
Removes all unnecessary whitespace.
|
|
713
|
+
|
|
714
|
+
```ts Input
|
|
715
|
+
function add(a, b) {
|
|
716
|
+
return a + b;
|
|
717
|
+
}
|
|
718
|
+
let x = 10;
|
|
719
|
+
```
|
|
720
|
+
|
|
721
|
+
```ts Output
|
|
722
|
+
function add(a, b) {
|
|
723
|
+
return a + b;
|
|
724
|
+
}
|
|
725
|
+
let x = 10;
|
|
726
|
+
```
|
|
727
|
+
|
|
728
|
+
### Semicolon optimization
|
|
729
|
+
|
|
730
|
+
**Mode:** `--minify-whitespace`
|
|
731
|
+
|
|
732
|
+
Inserts semicolons only when necessary.
|
|
733
|
+
|
|
734
|
+
```ts Input
|
|
735
|
+
let a = 1;
|
|
736
|
+
let b = 2;
|
|
737
|
+
return a + b;
|
|
738
|
+
```
|
|
739
|
+
|
|
740
|
+
```ts Output
|
|
741
|
+
let a = 1;
|
|
742
|
+
let b = 2;
|
|
743
|
+
return a + b;
|
|
744
|
+
```
|
|
745
|
+
|
|
746
|
+
### Operator spacing removal
|
|
747
|
+
|
|
748
|
+
**Mode:** `--minify-whitespace`
|
|
749
|
+
|
|
750
|
+
Removes spaces around operators.
|
|
751
|
+
|
|
752
|
+
```ts Input
|
|
753
|
+
a + b;
|
|
754
|
+
x = y * z;
|
|
755
|
+
(foo && bar) || baz;
|
|
756
|
+
```
|
|
757
|
+
|
|
758
|
+
```ts Output
|
|
759
|
+
a + b;
|
|
760
|
+
x = y * z;
|
|
761
|
+
(foo && bar) || baz;
|
|
762
|
+
```
|
|
763
|
+
|
|
764
|
+
### Comment removal
|
|
765
|
+
|
|
766
|
+
**Mode:** `--minify-whitespace`
|
|
767
|
+
|
|
768
|
+
Removes comments except important license comments.
|
|
769
|
+
|
|
770
|
+
```ts Input
|
|
771
|
+
// This comment is removed
|
|
772
|
+
/* So is this */
|
|
773
|
+
/*! But this license comment is kept */
|
|
774
|
+
function test() {
|
|
775
|
+
/* inline comment */
|
|
776
|
+
}
|
|
777
|
+
```
|
|
778
|
+
|
|
779
|
+
```ts Output
|
|
780
|
+
/*! But this license comment is kept */
|
|
781
|
+
function test() {}
|
|
782
|
+
```
|
|
783
|
+
|
|
784
|
+
### Object and array formatting
|
|
785
|
+
|
|
786
|
+
**Mode:** `--minify-whitespace`
|
|
787
|
+
|
|
788
|
+
Removes whitespace in object and array literals.
|
|
789
|
+
|
|
790
|
+
```ts Input
|
|
791
|
+
const obj = {
|
|
792
|
+
name: "John",
|
|
793
|
+
age: 30,
|
|
794
|
+
};
|
|
795
|
+
const arr = [1, 2, 3];
|
|
796
|
+
```
|
|
797
|
+
|
|
798
|
+
```ts Output
|
|
799
|
+
const obj = { name: "John", age: 30 };
|
|
800
|
+
const arr = [1, 2, 3];
|
|
801
|
+
```
|
|
802
|
+
|
|
803
|
+
### Control flow formatting
|
|
804
|
+
|
|
805
|
+
**Mode:** `--minify-whitespace`
|
|
806
|
+
|
|
807
|
+
Removes whitespace in control structures.
|
|
808
|
+
|
|
809
|
+
```ts Input
|
|
810
|
+
if (condition) {
|
|
811
|
+
doSomething();
|
|
812
|
+
}
|
|
813
|
+
for (let i = 0; i < 10; i++) {
|
|
814
|
+
console.log(i);
|
|
815
|
+
}
|
|
816
|
+
```
|
|
817
|
+
|
|
818
|
+
```ts Output
|
|
819
|
+
if (condition) doSomething();
|
|
820
|
+
for (let i = 0; i < 10; i++) console.log(i);
|
|
821
|
+
```
|
|
822
|
+
|
|
823
|
+
### Function formatting
|
|
824
|
+
|
|
825
|
+
**Mode:** `--minify-whitespace`
|
|
826
|
+
|
|
827
|
+
Removes whitespace in function declarations.
|
|
828
|
+
|
|
829
|
+
```ts Input
|
|
830
|
+
function myFunction(param1, param2) {
|
|
831
|
+
return param1 + param2;
|
|
832
|
+
}
|
|
833
|
+
const arrow = (a, b) => a + b;
|
|
834
|
+
```
|
|
835
|
+
|
|
836
|
+
```ts Output
|
|
837
|
+
function myFunction(a, b) {
|
|
838
|
+
return a + b;
|
|
839
|
+
}
|
|
840
|
+
const arrow = (a, b) => a + b;
|
|
841
|
+
```
|
|
842
|
+
|
|
843
|
+
### Parentheses minimization
|
|
844
|
+
|
|
845
|
+
**Mode:** Always active
|
|
846
|
+
|
|
847
|
+
Only adds parentheses when necessary for operator precedence.
|
|
848
|
+
|
|
849
|
+
```ts Input
|
|
850
|
+
(a + b) * c;
|
|
851
|
+
a + (b * c)(x);
|
|
852
|
+
```
|
|
853
|
+
|
|
854
|
+
```ts Output
|
|
855
|
+
(a + b) * c;
|
|
856
|
+
a + b * c;
|
|
857
|
+
x;
|
|
858
|
+
```
|
|
859
|
+
|
|
860
|
+
### Property mangling
|
|
861
|
+
|
|
862
|
+
**Mode:** `--minify-identifiers` (with configuration)
|
|
863
|
+
|
|
864
|
+
Renames object properties to shorter names when configured.
|
|
865
|
+
|
|
866
|
+
```ts#input.ts (with property mangling enabled)
|
|
867
|
+
obj.longPropertyName
|
|
868
|
+
```
|
|
869
|
+
|
|
870
|
+
```ts Output
|
|
871
|
+
obj.a;
|
|
872
|
+
```
|
|
873
|
+
|
|
874
|
+
### Template literal value folding
|
|
875
|
+
|
|
876
|
+
**Mode:** `--minify-syntax`
|
|
877
|
+
|
|
878
|
+
Converts non-string interpolated values to strings and folds them into the template.
|
|
879
|
+
|
|
880
|
+
```ts Input
|
|
881
|
+
`hello ${123}``value: ${true}``result: ${null}``status: ${undefined}``big: ${10n}`;
|
|
882
|
+
```
|
|
883
|
+
|
|
884
|
+
```ts Output
|
|
885
|
+
"hello 123";
|
|
886
|
+
"value: true";
|
|
887
|
+
"result: null";
|
|
888
|
+
"status: undefined";
|
|
889
|
+
"big: 10";
|
|
890
|
+
```
|
|
891
|
+
|
|
892
|
+
### String length constant folding
|
|
893
|
+
|
|
894
|
+
**Mode:** `--minify-syntax`
|
|
895
|
+
|
|
896
|
+
Evaluates `.length` property on string literals at compile time.
|
|
897
|
+
|
|
898
|
+
```ts Input
|
|
899
|
+
"hello world".length;
|
|
900
|
+
"test".length;
|
|
901
|
+
```
|
|
902
|
+
|
|
903
|
+
```ts Output
|
|
904
|
+
11;
|
|
905
|
+
4;
|
|
906
|
+
```
|
|
907
|
+
|
|
908
|
+
### Constructor call simplification
|
|
909
|
+
|
|
910
|
+
**Mode:** `--minify-syntax`
|
|
911
|
+
|
|
912
|
+
Simplifies constructor calls for built-in types.
|
|
913
|
+
|
|
914
|
+
```ts Input
|
|
915
|
+
new Object();
|
|
916
|
+
new Object(null);
|
|
917
|
+
new Object({ a: 1 });
|
|
918
|
+
new Array();
|
|
919
|
+
new Array(x, y);
|
|
920
|
+
```
|
|
921
|
+
|
|
922
|
+
```ts Output
|
|
923
|
+
{
|
|
924
|
+
}
|
|
925
|
+
{
|
|
926
|
+
}
|
|
927
|
+
{
|
|
928
|
+
a: 1;
|
|
929
|
+
}
|
|
930
|
+
[][(x, y)];
|
|
931
|
+
```
|
|
932
|
+
|
|
933
|
+
### Single property object inlining
|
|
934
|
+
|
|
935
|
+
**Mode:** `--minify-syntax`
|
|
936
|
+
|
|
937
|
+
Inlines property access for objects with a single property.
|
|
938
|
+
|
|
939
|
+
```ts Input
|
|
940
|
+
({ fn: () => console.log("hi") }).fn();
|
|
941
|
+
```
|
|
942
|
+
|
|
943
|
+
```ts Output
|
|
944
|
+
(() => console.log("hi"))();
|
|
945
|
+
```
|
|
946
|
+
|
|
947
|
+
### String charCodeAt constant folding
|
|
948
|
+
|
|
949
|
+
**Mode:** Always active
|
|
950
|
+
|
|
951
|
+
Evaluates `charCodeAt()` on string literals for ASCII characters.
|
|
952
|
+
|
|
953
|
+
```ts Input
|
|
954
|
+
"hello".charCodeAt(1);
|
|
955
|
+
"A".charCodeAt(0);
|
|
956
|
+
```
|
|
957
|
+
|
|
958
|
+
```ts Output
|
|
959
|
+
101;
|
|
960
|
+
65;
|
|
961
|
+
```
|
|
962
|
+
|
|
963
|
+
### Void 0 equality to null equality
|
|
964
|
+
|
|
965
|
+
**Mode:** `--minify-syntax`
|
|
966
|
+
|
|
967
|
+
Converts loose equality checks with `void 0` to `null` since they're equivalent.
|
|
968
|
+
|
|
969
|
+
```ts Input
|
|
970
|
+
x == void 0;
|
|
971
|
+
x != void 0;
|
|
972
|
+
```
|
|
973
|
+
|
|
974
|
+
```ts Output
|
|
975
|
+
x == null;
|
|
976
|
+
x != null;
|
|
977
|
+
```
|
|
978
|
+
|
|
979
|
+
### Negation operator optimization
|
|
980
|
+
|
|
981
|
+
**Mode:** `--minify-syntax`
|
|
982
|
+
|
|
983
|
+
Moves negation operator through comma expressions.
|
|
984
|
+
|
|
985
|
+
```ts Input
|
|
986
|
+
-(a, b) - (x, y, z);
|
|
987
|
+
```
|
|
988
|
+
|
|
989
|
+
```ts Output
|
|
990
|
+
(a, -b);
|
|
991
|
+
(x, y, -z);
|
|
992
|
+
```
|
|
993
|
+
|
|
994
|
+
### Import.meta property inlining
|
|
995
|
+
|
|
996
|
+
**Mode:** Bundle mode
|
|
997
|
+
|
|
998
|
+
Inlines `import.meta` properties at build time when values are known.
|
|
999
|
+
|
|
1000
|
+
```ts Input
|
|
1001
|
+
import.meta.dir;
|
|
1002
|
+
import.meta.file;
|
|
1003
|
+
import.meta.path;
|
|
1004
|
+
import.meta.url;
|
|
1005
|
+
```
|
|
1006
|
+
|
|
1007
|
+
```ts Output
|
|
1008
|
+
"/path/to/directory";
|
|
1009
|
+
"filename.js";
|
|
1010
|
+
"/full/path/to/file.js";
|
|
1011
|
+
"file:///full/path/to/file.js";
|
|
1012
|
+
```
|
|
1013
|
+
|
|
1014
|
+
### Variable declaration merging
|
|
1015
|
+
|
|
1016
|
+
**Mode:** `--minify-syntax`
|
|
1017
|
+
|
|
1018
|
+
Merges adjacent variable declarations of the same type.
|
|
1019
|
+
|
|
1020
|
+
```ts Input
|
|
1021
|
+
let a = 1;
|
|
1022
|
+
let b = 2;
|
|
1023
|
+
const c = 3;
|
|
1024
|
+
const d = 4;
|
|
1025
|
+
```
|
|
1026
|
+
|
|
1027
|
+
```ts Output
|
|
1028
|
+
let a = 1,
|
|
1029
|
+
b = 2;
|
|
1030
|
+
const c = 3,
|
|
1031
|
+
d = 4;
|
|
1032
|
+
```
|
|
1033
|
+
|
|
1034
|
+
### Expression statement merging
|
|
1035
|
+
|
|
1036
|
+
**Mode:** `--minify-syntax`
|
|
1037
|
+
|
|
1038
|
+
Merges adjacent expression statements using comma operator.
|
|
1039
|
+
|
|
1040
|
+
```ts Input
|
|
1041
|
+
console.log(1);
|
|
1042
|
+
console.log(2);
|
|
1043
|
+
console.log(3);
|
|
1044
|
+
```
|
|
1045
|
+
|
|
1046
|
+
```ts Output
|
|
1047
|
+
(console.log(1), console.log(2), console.log(3));
|
|
1048
|
+
```
|
|
1049
|
+
|
|
1050
|
+
### Return statement merging
|
|
1051
|
+
|
|
1052
|
+
**Mode:** `--minify-syntax`
|
|
1053
|
+
|
|
1054
|
+
Merges expressions before return with comma operator.
|
|
1055
|
+
|
|
1056
|
+
```ts Input
|
|
1057
|
+
console.log(x);
|
|
1058
|
+
return y;
|
|
1059
|
+
```
|
|
1060
|
+
|
|
1061
|
+
```ts Output
|
|
1062
|
+
return (console.log(x), y);
|
|
1063
|
+
```
|
|
1064
|
+
|
|
1065
|
+
### Throw statement merging
|
|
1066
|
+
|
|
1067
|
+
**Mode:** `--minify-syntax`
|
|
1068
|
+
|
|
1069
|
+
Merges expressions before throw with comma operator.
|
|
1070
|
+
|
|
1071
|
+
```ts Input
|
|
1072
|
+
console.log(x);
|
|
1073
|
+
throw new Error();
|
|
1074
|
+
```
|
|
1075
|
+
|
|
1076
|
+
```ts Output
|
|
1077
|
+
throw (console.log(x), new Error());
|
|
1078
|
+
```
|
|
1079
|
+
|
|
1080
|
+
### TypeScript enum cross-module inlining
|
|
1081
|
+
|
|
1082
|
+
**Mode:** `--minify-syntax` (bundle mode)
|
|
1083
|
+
|
|
1084
|
+
Inlines enum values across module boundaries.
|
|
1085
|
+
|
|
1086
|
+
```ts#input.ts (lib.ts)
|
|
1087
|
+
export enum Color { Red, Green, Blue }
|
|
1088
|
+
|
|
1089
|
+
// Input (main.ts)
|
|
1090
|
+
import { Color } from './lib';
|
|
1091
|
+
const x = Color.Red;
|
|
1092
|
+
```
|
|
1093
|
+
|
|
1094
|
+
```ts Output
|
|
1095
|
+
const x = 0;
|
|
1096
|
+
```
|
|
1097
|
+
|
|
1098
|
+
### Computed property enum inlining
|
|
1099
|
+
|
|
1100
|
+
**Mode:** `--minify-syntax`
|
|
1101
|
+
|
|
1102
|
+
Inlines enum values used as computed object properties.
|
|
1103
|
+
|
|
1104
|
+
```ts Input
|
|
1105
|
+
enum Keys {
|
|
1106
|
+
FOO = "foo",
|
|
1107
|
+
}
|
|
1108
|
+
const obj = { [Keys.FOO]: value };
|
|
1109
|
+
```
|
|
1110
|
+
|
|
1111
|
+
```ts Output
|
|
1112
|
+
const obj = { foo: value };
|
|
1113
|
+
```
|
|
1114
|
+
|
|
1115
|
+
### String number to numeric index
|
|
1116
|
+
|
|
1117
|
+
**Mode:** `--minify-syntax`
|
|
1118
|
+
|
|
1119
|
+
Converts string numeric property access to numeric index.
|
|
1120
|
+
|
|
1121
|
+
```ts Input
|
|
1122
|
+
obj["0"];
|
|
1123
|
+
arr["5"];
|
|
1124
|
+
```
|
|
1125
|
+
|
|
1126
|
+
```ts Output
|
|
1127
|
+
obj[0];
|
|
1128
|
+
arr[5];
|
|
1129
|
+
```
|
|
1130
|
+
|
|
1131
|
+
### Arrow function body shortening
|
|
1132
|
+
|
|
1133
|
+
**Mode:** Always active
|
|
1134
|
+
|
|
1135
|
+
Uses expression body syntax when an arrow function only returns a value.
|
|
1136
|
+
|
|
1137
|
+
```ts Input
|
|
1138
|
+
() => {
|
|
1139
|
+
return x;
|
|
1140
|
+
};
|
|
1141
|
+
a => {
|
|
1142
|
+
return a + 1;
|
|
1143
|
+
};
|
|
1144
|
+
```
|
|
1145
|
+
|
|
1146
|
+
```ts Output
|
|
1147
|
+
() => x;
|
|
1148
|
+
a => a + 1;
|
|
1149
|
+
```
|
|
1150
|
+
|
|
1151
|
+
### Object property shorthand
|
|
1152
|
+
|
|
1153
|
+
**Mode:** Always active
|
|
1154
|
+
|
|
1155
|
+
Uses shorthand syntax when property name and value identifier match.
|
|
1156
|
+
|
|
1157
|
+
```ts Input
|
|
1158
|
+
{ x: x, y: y }
|
|
1159
|
+
{ name: name, age: age }
|
|
1160
|
+
```
|
|
1161
|
+
|
|
1162
|
+
```ts Output
|
|
1163
|
+
{
|
|
1164
|
+
(x, y);
|
|
1165
|
+
}
|
|
1166
|
+
{
|
|
1167
|
+
(name, age);
|
|
1168
|
+
}
|
|
1169
|
+
```
|
|
1170
|
+
|
|
1171
|
+
### Method shorthand
|
|
1172
|
+
|
|
1173
|
+
**Mode:** Always active
|
|
1174
|
+
|
|
1175
|
+
Uses method shorthand syntax in object literals.
|
|
1176
|
+
|
|
1177
|
+
```ts Input
|
|
1178
|
+
{
|
|
1179
|
+
foo: function() {},
|
|
1180
|
+
bar: async function() {}
|
|
1181
|
+
}
|
|
1182
|
+
```
|
|
1183
|
+
|
|
1184
|
+
```ts Output
|
|
1185
|
+
{
|
|
1186
|
+
foo() {},
|
|
1187
|
+
async bar() {}
|
|
1188
|
+
}
|
|
1189
|
+
```
|
|
1190
|
+
|
|
1191
|
+
### Drop debugger statements
|
|
1192
|
+
|
|
1193
|
+
**Mode:** `--drop=debugger`
|
|
1194
|
+
|
|
1195
|
+
Removes `debugger` statements from code.
|
|
1196
|
+
|
|
1197
|
+
```ts Input
|
|
1198
|
+
function test() {
|
|
1199
|
+
debugger;
|
|
1200
|
+
return x;
|
|
1201
|
+
}
|
|
1202
|
+
```
|
|
1203
|
+
|
|
1204
|
+
```ts Output
|
|
1205
|
+
function test() {
|
|
1206
|
+
return x;
|
|
1207
|
+
}
|
|
1208
|
+
```
|
|
1209
|
+
|
|
1210
|
+
### Drop console calls
|
|
1211
|
+
|
|
1212
|
+
**Mode:** `--drop=console`
|
|
1213
|
+
|
|
1214
|
+
Removes all `console.*` method calls from code.
|
|
1215
|
+
|
|
1216
|
+
```ts Input
|
|
1217
|
+
console.log("debug");
|
|
1218
|
+
console.warn("warning");
|
|
1219
|
+
x = console.error("error");
|
|
1220
|
+
```
|
|
1221
|
+
|
|
1222
|
+
```ts Output
|
|
1223
|
+
void 0;
|
|
1224
|
+
void 0;
|
|
1225
|
+
x = void 0;
|
|
1226
|
+
```
|
|
1227
|
+
|
|
1228
|
+
### Drop custom function calls
|
|
1229
|
+
|
|
1230
|
+
**Mode:** `--drop=<name>`
|
|
1231
|
+
|
|
1232
|
+
Removes calls to specified global functions or methods.
|
|
1233
|
+
|
|
1234
|
+
```ts#input.ts with --drop=assert
|
|
1235
|
+
assert(condition);
|
|
1236
|
+
obj.assert(test);
|
|
1237
|
+
```
|
|
1238
|
+
|
|
1239
|
+
```ts Output
|
|
1240
|
+
void 0;
|
|
1241
|
+
void 0;
|
|
1242
|
+
```
|
|
1243
|
+
|
|
1244
|
+
## Keep Names
|
|
1245
|
+
|
|
1246
|
+
When minifying identifiers, you may want to preserve original function and class names for debugging purposes. Use the `--keep-names` flag:
|
|
1247
|
+
|
|
1248
|
+
```bash
|
|
1249
|
+
bun build ./index.ts --minify --keep-names --outfile=out.js
|
|
1250
|
+
```
|
|
1251
|
+
|
|
1252
|
+
Or in the JavaScript API:
|
|
1253
|
+
|
|
1254
|
+
```ts
|
|
1255
|
+
await Bun.build({
|
|
1256
|
+
entrypoints: ["./index.ts"],
|
|
1257
|
+
outdir: "./out",
|
|
1258
|
+
minify: {
|
|
1259
|
+
identifiers: true,
|
|
1260
|
+
keepNames: true,
|
|
1261
|
+
},
|
|
1262
|
+
});
|
|
1263
|
+
```
|
|
1264
|
+
|
|
1265
|
+
This preserves the `.name` property on functions and classes while still minifying the actual identifier names in the code.
|
|
1266
|
+
|
|
1267
|
+
## Combined Example
|
|
1268
|
+
|
|
1269
|
+
Using all three minification modes together:
|
|
1270
|
+
|
|
1271
|
+
```ts#input.ts (158 bytes)
|
|
1272
|
+
const myVariable = 42;
|
|
1273
|
+
|
|
1274
|
+
const myFunction = () => {
|
|
1275
|
+
const isValid = true;
|
|
1276
|
+
const result = undefined;
|
|
1277
|
+
return isValid ? myVariable : result;
|
|
1278
|
+
};
|
|
1279
|
+
|
|
1280
|
+
const output = myFunction();
|
|
1281
|
+
```
|
|
1282
|
+
|
|
1283
|
+
```ts#output.ts
|
|
1284
|
+
// Output with --minify (49 bytes, 69% reduction)
|
|
1285
|
+
const a=42,b=()=>{const c=!0,d=void 0;return c?a:d},e=b();
|
|
1286
|
+
```
|
|
1287
|
+
|
|
1288
|
+
## When to Use Minification
|
|
1289
|
+
|
|
1290
|
+
**Use `--minify` for:**
|
|
1291
|
+
|
|
1292
|
+
- Production bundles
|
|
1293
|
+
- Reducing CDN bandwidth costs
|
|
1294
|
+
- Improving page load times
|
|
1295
|
+
|
|
1296
|
+
**Use individual modes for:**
|
|
1297
|
+
|
|
1298
|
+
- **`--minify-whitespace`:** Quick size reduction without semantic changes
|
|
1299
|
+
- **`--minify-syntax`:** Smaller output while keeping readable identifiers for debugging
|
|
1300
|
+
- **`--minify-identifiers`:** Maximum size reduction (combine with `--keep-names` for better stack traces)
|
|
1301
|
+
|
|
1302
|
+
**Avoid minification for:**
|
|
1303
|
+
|
|
1304
|
+
- Development builds (harder to debug)
|
|
1305
|
+
- When you need readable error messages
|
|
1306
|
+
- Libraries where consumers may read the source
|