diff --git a/CHANGELOG.md b/CHANGELOG.md index 37f5e72c1b0..51abf23aaec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,20 @@ It's now possible to use `--target=es2021` to target the newly-released JavaScript version ES2021. The only difference between that and `--target=es2020` is that logical assignment operators such as `a ||= b` are not converted to regular assignment operators such as `a || (a = b)`. +* Minify the syntax `Infinity` to `1 / 0` ([#1385](https://github.com/evanw/esbuild/pull/1385)) + + The `--minify-syntax` flag (automatically enabled by `--minify`) will now minify the expression `Infinity` to `1 / 0`, which uses fewer bytes: + + ```js + // Original code + const a = Infinity; + + // Output with "--minify-syntax" + const a = 1 / 0; + ``` + + This change was contributed by [@Gusted](https://github.com/Gusted). + ## 0.12.9 * Allow `this` with `--define` ([#1361](https://github.com/evanw/esbuild/issues/1361)) diff --git a/internal/js_printer/js_printer.go b/internal/js_printer/js_printer.go index d9cf7ca79e2..6f2d060676b 100644 --- a/internal/js_printer/js_printer.go +++ b/internal/js_printer/js_printer.go @@ -2132,15 +2132,27 @@ func (p *printer) printExpr(expr js_ast.Expr, level js_ast.L, flags printExprFla if value != value { p.printSpaceBeforeIdentifier() p.print("NaN") - } else if value == positiveInfinity { - p.printSpaceBeforeIdentifier() - p.print("Infinity") - } else if value == negativeInfinity { - if level >= js_ast.LPrefix { - p.print("(-Infinity)") - } else { + } else if value == positiveInfinity || value == negativeInfinity { + wrap := (p.options.MangleSyntax && level >= js_ast.LMultiply) || + (value == negativeInfinity && level >= js_ast.LPrefix) + if wrap { + p.print("(") + } + if value == negativeInfinity { p.printSpaceBeforeOperator(js_ast.UnOpNeg) - p.print("-Infinity") + p.print("-") + } else { + p.printSpaceBeforeIdentifier() + } + if !p.options.MangleSyntax { + p.print("Infinity") + } else if p.options.RemoveWhitespace { + p.print("1/0") + } else { + p.print("1 / 0") + } + if wrap { + p.print(")") } } else { if !math.Signbit(value) { diff --git a/internal/js_printer/js_printer_test.go b/internal/js_printer/js_printer_test.go index 48c3570ddd6..b711983d2a2 100644 --- a/internal/js_printer/js_printer_test.go +++ b/internal/js_printer/js_printer_test.go @@ -931,3 +931,61 @@ func TestAvoidSlashScript(t *testing.T) { expectPrintedMinify(t, "x = 1 < / script/.exec(y).length", "x=1