minify

package
v0.38.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 21, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package minify implements a token-aware JavaScript minifier used to shrink the embedded runtime sources before they're served.

Scope (tier-2):

  • Strip line + block comments.
  • Collapse whitespace, preserving newlines only where ASI cares.
  • Distinguish regex literals from division.
  • Preserve string + template-literal payloads byte-for-byte.
  • Insert a single space when removing whitespace would fuse two adjacent tokens (`let a` → `leta`, `+ +` → `++`, `/ /` → `//`).
  • Rewrite a `const` declaration keyword to `let`. For any program that runs without throwing, the two are semantically identical (same block scoping, same TDZ, same per-iteration for-of/for-in binding); const's extra assignment check can only fire in code that is already broken. Non-declaration uses (`a.const`, `{const: 1}`, `class A{const=1}`) are left alone: the rewrite applies only when the keyword follows a non-`.` token and the next token starts a binding (identifier, `{`, or `[`).
  • Drop a `;` or `,` immediately before a `}` (`a=1;}` → `a=1}`, `{a:1,}` → `{a:1}`). In valid JS a `}` can only follow `;` as a statement/class-element terminator (droppable) and `,` as a trailing comma in an object literal / destructuring / import-specifier list (droppable; array-hole commas sit before `]`, which is never touched). Exception: a `;` whose previous token is `)` is never dropped — it may be an empty statement serving as an if/while/for body (`if(x);}` → `if(x)}` is a SyntaxError), and a scanner can't tell that apart from `g();}`. The same rule keeps the do-while terminator (`do{}while(x);}`).

Out of scope (would be tier-3): identifier renaming, dead-code elimination, constant folding, anything that requires a parser.

The output is intentionally still valid, readable-ish JavaScript — it stays parseable by the browser without source maps and a developer can still set breakpoints inside it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Minify

func Minify(src string) string

Minify returns a shrunk-but-equivalent JavaScript source. The output preserves the semantics of the input across every construct the embedded runtime currently uses. Inputs that aren't valid JS produce undefined-but-stable output (the minifier never panics).

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL