97 releases (58 breaking)
Uses new Rust 2024
new 0.58.1 | Mar 13, 2025 |
---|---|
0.57.0 | Mar 11, 2025 |
0.44.0 | Dec 25, 2024 |
0.38.0 | Nov 26, 2024 |
0.1.0 | Jul 27, 2023 |
#976 in Programming languages
12,365 downloads per month
Used in 20 crates
(2 directly)
5.5MB
111K
SLoC
Minifier
A JavaScript minifier has three components:
- compressor
- mangler
- printer
Compressor
The compressor is responsible for rewriting statements and expressions for minimal text output. Terser is a good place to start for learning the fundamentals.
Mangler
The mangler implementation is part of the SymbolTable
residing in oxc_semantic
.
It is responsible for shortening variables. Its algorithm should be gzip friendly.
The printer is also responsible for printing out the shortened variable names.
Printer
The printer is responsible for removing whitespace from the source text.
Assumptions
- Properties of the global object defined in the ECMAScript spec behaves the same as in the spec
- Examples of properties:
Infinity
,parseInt
,Object
,Promise.resolve
- Examples that breaks this assumption:
globalThis.Object = class MyObject {}
- Examples of properties:
- The code does not rely on the
name
property ofFunction
orClass
- Examples that breaks this assumption:
function fn() {}; console.log(f.name === 'fn')
- Examples that breaks this assumption:
document.all
is not used or behaves as a normal object- Examples that breaks this assumption:
console.log(typeof document.all === 'undefined')
- Examples that breaks this assumption:
- TDZ violation does not happen
- Examples that breaks this assumption:
(() => { console.log(v); let v; })()
- Examples that breaks this assumption:
with
statement is not used- Examples that breaks this assumption:
with (Math) { console.log(PI); }
- Examples that breaks this assumption:
.toString()
,.valueOf()
,[Symbol.toPrimitive]()
are side-effect free- Examples that breaks this assumption:
{ toString() { console.log('sideeffect') } }
- Examples that breaks this assumption:
- Errors thrown when creating a String or an Array that exceeds the maximum length can disappear or moved
- Examples that breaks this assumption:
try { new Array(Number(2n**53n)) } catch { console.log('log') }
- Examples that breaks this assumption:
- Invalid super class error does not happen
- Examples that breaks this assumption:
const v = []; class A extends v {}
- Examples that breaks this assumption:
Terser Tests
The fixtures are copied from https://github.com/terser/terser/tree/v5.9.0/test/compress
Dependencies
~9.5MB
~170K SLoC