Minify JS
JavaScript Minification
Section titled “JavaScript Minification”The minify_js tool minifies a JavaScript file in place using a
pure-stdlib state-machine minifier. No Node.js, no external dependencies,
no toolchain rebuild — drop-in minification with full recovery via the
standard backup system.
Added in v4.5.7 (2026-06-07). See the CHANGELOG.
For context on the full tool surface, see Core Tools.
minify_js
Section titled “minify_js”Annotations:
| Annotation | Value |
|---|---|
| readOnly | false (writes by default; dry_run:true is read-only) |
| destructive | true |
| idempotent | true (re-running on the already-minified file is a no-op) |
Parameters:
| Parameter | Required | Type | Default | Description |
|---|---|---|---|---|
path | Yes | string | — | Path to the .js file to minify (overwritten in place) |
output_path | No | string | — | If set, write minified output to this path instead of overwriting path |
remove_comments | No | bool | true | Strip // and /* */ comments |
collapse_whitespace | No | bool | true | Collapse runs of spaces/tabs to a single space where needed |
single_line | No | bool | true | Emit all output on a single line |
dry_run | No | bool | false | Preview without writing |
create_backup | No | bool | true | Create a backup before overwriting (only when writing to path) |
Errors:
- If
output_pathresolves outside--allowed-paths, the call fails withaccess denied. - If
output_pathwould overwrite a symlink pointing outside--allowed-paths, the call fails with the TOCTOU defense message. - If all three knobs (
remove_comments,collapse_whitespace,single_line) are set tofalse, the call fails withnothing to do(refuse silently defaulting to a no-op).
What the minifier handles correctly
Section titled “What the minifier handles correctly”The minifier is a best-effort state machine (core/minifier.go). It
never modifies the contents of strings, regexes, or template
substitutions. It correctly handles:
//line comments and/* */block comments- Single- and double-quoted strings (with escapes)
- Template literals (backticks) including
${expr}interpolation - Regex literals (
/.../[flags]) with character classes - The regex-vs-division disambiguation (real JS tokenizer behaviour)
- Shebangs (
#!/usr/bin/env nodeat the top of a file)
Conservative heuristics are used for exotic edge cases (regexes with
/ inside character classes, tagged-template edge cases). When the
minifier cannot make a safe decision it leaves the source unchanged
— never produces broken output.
Examples
Section titled “Examples”Dry run first (recommended)
Section titled “Dry run first (recommended)”minify_js({ path: "app.js", dry_run: true})// → "MINIFY (dry-run) app.js | 87342→31045B (-56297, 64.4%) | comments:42"Verbose response includes a 500-char preview of the minified output.
Live run with the defaults
Section titled “Live run with the defaults”minify_js({ path: "app.js"})// → "MINIFIED app.js | 87342→31045B (-56297, 64.4%) | comments:42 | UNDO:20260607-xxx"The original file is moved to the backup directory before the
minified copy is written. The backup ID is shown in the response;
use it with backup(action:"undo_last", file_path:"app.js") to roll
back.
Minify to a different path
Section titled “Minify to a different path”minify_js({ path: "src/app.js", output_path: "dist/app.min.js", create_backup: false // source untouched, no backup needed})Reject all-false options
Section titled “Reject all-false options”minify_js({ path: "app.js", remove_comments: false, collapse_whitespace: false, single_line: false})// → Error: "all of remove_comments, collapse_whitespace, and single_line are false — nothing to do"Response formats
Section titled “Response formats”Compact:
MINIFY <target> | <in>→<out>B (-<saved>, <pct>%) | comments:<n>[ | UNDO:<id>][ | from:<source>]Verbose: Source, Output, Input bytes, Output bytes, Saved,
Comments stripped, optional preview (first 500 chars), optional
truncated:input-malformed warning if the source has an unterminated
string or comment.
Recovery
Section titled “Recovery”The minifier creates a backup via the standard BackupManager when
overwriting in place. Recovery:
// Undo the most recent minify on this filebackup({action: "undo_last", file_path: "app.js"})
// Preview what would be undonebackup({action: "undo_last", file_path: "app.js", preview: true})
// Restore a specific backup by full IDbackup({ action: "restore", backup_id: "20260607-xxxxx-full-id", file_path: "app.js"})See Safe editing protocol for the full step-through undo workflow.
When to use this tool
Section titled “When to use this tool”Use minify_js when:
- You need a quick minified copy of a JS file and the user does not want to rebuild via their toolchain (no Node, no webpack, no esbuild).
- You need to ship a one-off script and want a smaller payload.
- You want a deterministic, auditable minification that produces a single backup ID you can roll back from.
Do NOT use this tool when:
- You need advanced transformations (tree-shaking, dead-code elimination, ES-module-to-CJS). The minifier is a pure byte-level minifier — it does not understand module semantics.
- The source uses exotic syntax the minifier might mishandle. Always
dry_run:truefirst and inspect the preview before live-minifying.
Implementation details
Section titled “Implementation details”- Pure stdlib. No external dependencies, no
exec.Commandcalls. - State machine.
core/minifier.go(~700 LOC) — single pass over the source bytes. - Backups. When writing to
pathandcreate_backup:true, the backup is created viaengine.GetBackupManager()and linked into the file’sPreviousBackupIDchain (soundo_lastwalks it). - Cache.
engine.InvalidateCache(path)is called after writing, so subsequentread_filecalls see the new bytes.
Files: core/minifier.go, tools_minify.go,
core/minifier_test.go (25+ cases).
Related
Section titled “Related”- Core Tools — full tool matrix
- Safe editing protocol
- Changelog — v4.5.7 release notes
Version: 4.5.29 Last updated: 2026-07-11