Skip to main content

Module javascript

Module javascript 

Source
Expand description

JavaScript / TypeScript minifier.

The JS lexer is the most complex of the v0.3 set:

  • Template literals `…${expr}…${expr}…`. The body is literal except inside ${…} interpolations, which contain arbitrary JS code (and may themselves contain template literals, recursively). We track interpolation by scanning for ${, then counting {/} until the brace balance returns to zero.
  • Regex literals /pattern/flags. Lexically ambiguous with division. We disambiguate via the previous-significant-token heuristic: regex iff the previous non-whitespace, non-comment token was a punctuator (other than )/]/}/++/--) or one of the expression-position keywords (return, typeof, in, of, delete, void, new, throw, await, yield, instanceof, case, do, else).
  • ASI. JavaScript can implicitly insert ; at end of certain lines. Stripping such newlines without inserting an explicit ; changes semantics (return\n{x:1} returns undefined; return{x:1} returns the object). Without a real parser we preserve newlines verbatim and trust the engine’s ASI rules.
  • TypeScript adds type syntax (x: T, <T>, as T) but the tokenization is unchanged from JS, so the same lexer handles both.

Strategy: conservative (Strategy B). Newlines preserved.

Functions§

minify