aboutsummaryrefslogtreecommitdiff
path: root/src/js_lexer.zig
diff options
context:
space:
mode:
authorGravatar Dylan Conway <35280289+dylan-conway@users.noreply.github.com> 2023-04-20 05:23:12 -0700
committerGravatar GitHub <noreply@github.com> 2023-04-20 05:23:12 -0700
commitd78ecc76c854310fd47da32071d22301b0782ec3 (patch)
treedcee5bde1b3598203de25f07a632cfb55aaa01e5 /src/js_lexer.zig
parent9e7bfdec8cd4fc1827a5d793844afe36638ded37 (diff)
downloadbun-d78ecc76c854310fd47da32071d22301b0782ec3.tar.gz
bun-d78ecc76c854310fd47da32071d22301b0782ec3.tar.zst
bun-d78ecc76c854310fd47da32071d22301b0782ec3.zip
Symbol minification (#2695)
* minify * Update renamer.zig * --minify-whitespace * Speed up minification a little * handle private names * 5% faster minification * use helper function * fix nested scope slots * `bun build --minify` gets another +8% faster * print semicolons afterwards * print semicolon after checking error * after all error checking * Delete code for generating legacy bundes * remove extra whitespace around if statements * print space before import identifier * Use `@constCast` * Make `S.Local#decls` use `BabyList(Decl)` * Add `fromSlice` helper to `BabyList` * Remove unnecessary optional chains * minify `undefined, true, false` * Another @constCast * Implement merge adjacent local var * Support --minify in `bun build --transform` * skip comments when counting character frequencies * Don't wrap commonjs with --transform on (unless targeting bun) * Support --minify in the runtime * Fix edgecase with import * as * don't infinite loop * --trnasform shouldn't mess with require * Only track comments when minifying --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Diffstat (limited to 'src/js_lexer.zig')
-rw-r--r--src/js_lexer.zig18
1 files changed, 15 insertions, 3 deletions
diff --git a/src/js_lexer.zig b/src/js_lexer.zig
index 8ad3cdbba..8f285396d 100644
--- a/src/js_lexer.zig
+++ b/src/js_lexer.zig
@@ -144,7 +144,6 @@ fn NewLexer_(
is_legacy_octal_literal: bool = false,
is_log_disabled: bool = false,
comments_to_preserve_before: std.ArrayList(js_ast.G.Comment),
- all_original_comments: ?[]js_ast.G.Comment = null,
code_point: CodePoint = -1,
identifier: []const u8 = "",
jsx_pragma: JSXPragma = .{},
@@ -165,6 +164,8 @@ fn NewLexer_(
/// Only used for JSON stringification when bundling
/// This is a zero-bit type unless we're parsing JSON.
is_ascii_only: JSONBool = JSONBoolDefault,
+ track_comments: bool = false,
+ all_comments: std.ArrayList(logger.Range),
pub fn clone(self: *const LexerType) LexerType {
return LexerType{
@@ -183,7 +184,6 @@ fn NewLexer_(
.is_legacy_octal_literal = self.is_legacy_octal_literal,
.is_log_disabled = self.is_log_disabled,
.comments_to_preserve_before = self.comments_to_preserve_before,
- .all_original_comments = self.all_original_comments,
.code_point = self.code_point,
.identifier = self.identifier,
.regex_flags_start = self.regex_flags_start,
@@ -198,6 +198,7 @@ fn NewLexer_(
.string_literal = self.string_literal,
.string_literal_is_ascii = self.string_literal_is_ascii,
.is_ascii_only = self.is_ascii_only,
+ .all_comments = self.all_comments,
};
}
@@ -276,7 +277,10 @@ fn NewLexer_(
return @enumToInt(lexer.token) >= @enumToInt(T.t_identifier);
}
- pub fn deinit(_: *LexerType) void {}
+ pub fn deinit(this: *LexerType) void {
+ this.all_comments.clearAndFree();
+ this.comments_to_preserve_before.clearAndFree();
+ }
fn decodeEscapeSequences(lexer: *LexerType, start: usize, text: string, comptime BufType: type, buf_: *BufType) !void {
var buf = buf_.*;
@@ -1816,6 +1820,11 @@ fn NewLexer_(
const has_legal_annotation = text.len > 2 and text[2] == '!';
const is_multiline_comment = text.len > 1 and text[1] == '*';
+ if (lexer.track_comments)
+ // Save the original comment text so we can subtract comments from the
+ // character frequency analysis used by symbol minification
+ lexer.all_comments.append(lexer.range()) catch unreachable;
+
// Omit the trailing "*/" from the checks below
const end_comment_text =
if (is_multiline_comment)
@@ -1974,6 +1983,7 @@ fn NewLexer_(
.string_literal_is_ascii = true,
.allocator = allocator,
.comments_to_preserve_before = std.ArrayList(js_ast.G.Comment).init(allocator),
+ .all_comments = std.ArrayList(logger.Range).init(allocator),
};
lex.step();
try lex.next();
@@ -1991,6 +2001,7 @@ fn NewLexer_(
.prev_error_loc = logger.Loc.Empty,
.allocator = allocator,
.comments_to_preserve_before = std.ArrayList(js_ast.G.Comment).init(allocator),
+ .all_comments = std.ArrayList(logger.Range).init(allocator),
};
lex.step();
try lex.next();
@@ -2008,6 +2019,7 @@ fn NewLexer_(
.prev_error_loc = logger.Loc.Empty,
.allocator = allocator,
.comments_to_preserve_before = std.ArrayList(js_ast.G.Comment).init(allocator),
+ .all_comments = std.ArrayList(logger.Range).init(allocator),
};
}