aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-03-09 03:58:32 -0800
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-03-09 03:58:32 -0800
commit026657d83b7c9143f6cea823c406438c58d18f7b (patch)
tree21a5564c5a6a4c6c7acf70257c305d9a7d4985c7
parente35895888e584de148dfe42d606e338e2ba5d9a2 (diff)
downloadbun-026657d83b7c9143f6cea823c406438c58d18f7b.tar.gz
bun-026657d83b7c9143f6cea823c406438c58d18f7b.tar.zst
bun-026657d83b7c9143f6cea823c406438c58d18f7b.zip
make the map slightly smaller
-rw-r--r--src/js_printer.zig64
-rw-r--r--src/options.zig138
2 files changed, 108 insertions, 94 deletions
diff --git a/src/js_printer.zig b/src/js_printer.zig
index 73d5ec575..a7fe33609 100644
--- a/src/js_printer.zig
+++ b/src/js_printer.zig
@@ -915,19 +915,12 @@ pub fn NewPrinter(
pub fn bestQuoteCharForString(_: *Printer, str: anytype, allow_backtick_: bool) u8 {
if (comptime is_json) return '"';
- const allow_backtick = allow_backtick_;
+ const costs: QuoteCost = if (allow_backtick_)
var single_cost: usize = 0;
var double_cost: usize = 0;
var backtick_cost: usize = 0;
var char: u8 = 0;
- var i: usize = 0;
- while (i < str.len) {
- switch (str[i]) {
- '\'' => {
- single_cost += 1;
- },
- '"' => {
double_cost += 1;
},
'`' => {
@@ -935,8 +928,6 @@ pub fn NewPrinter(
},
'\r', '\n' => {
if (allow_backtick) {
- return '`';
- }
},
'\\' => {
i += 1;
@@ -944,9 +935,6 @@ pub fn NewPrinter(
'$' => {
if (i + 1 < str.len and str[i + 1] == '{') {
backtick_cost += 1;
- }
- },
- else => {},
}
i += 1;
}
@@ -1121,16 +1109,9 @@ pub fn NewPrinter(
'@',
'#',
'%',
- '^',
- '&',
'*',
'+',
- '=',
- ' ',
- => {
- e.print(@intCast(u8, c));
},
-
// Special-case the bell character since it may cause dumping this file to
// the terminal to make a sound, which is undesirable. Note that we can't
// use an octal literal to print this shorter since octal literals are not
@@ -1140,10 +1121,18 @@ pub fn NewPrinter(
},
0x08 => {
e.print("\\b");
+ else
+ e.print("\\f");
},
0x0C => {
e.print("\\f");
},
+ '\t' => {
+ if (quote == '`')
+ e.print("\t")
+ else
+ e.print("\\t");
+ },
'\n' => {
if (quote == '`') {
e.print('\n');
@@ -1151,12 +1140,17 @@ pub fn NewPrinter(
e.print("\\n");
}
},
+ // we never print \r un-escaped
std.ascii.control_code.CR => {
e.print("\\r");
},
// \v
std.ascii.control_code.VT => {
- e.print("\\v");
+ if (quote == '`') {
+ e.print(std.ascii.control_code.VT);
+ } else {
+ e.print("\\v");
+ }
},
// "\\"
'\\' => {
@@ -1203,6 +1197,23 @@ pub fn NewPrinter(
else => {
switch (c) {
+ first_ascii...last_ascii => {
+ const remain = text[i..];
+ if (remain.len > 1 and remain[0] < last_ascii and remain[0] > first_ascii) {
+ if (strings.@"nextUTF16NonASCIIOr$`\\"([]const u16, remain)) |count| {
+ i += count;
+ var ptr = e.writer.reserve(count) catch unreachable;
+ strings.copyU16IntoU8(to_copy, []const u16, remain[0..count]);
+ e.writer.advance(count);
+
+ continue :outer;
+ }
+
+ {
+ const count = @truncate(u32, remain.len);
+ var ptr = e.writer.reserve(count) catch unreachable;
+ var to_copy = ptr[0..count];
+ strings.copyU16IntoU8(to_copy, []const u16, remain);
first_high_surrogate...last_high_surrogate => {
// Is there a next character?
@@ -1214,7 +1225,7 @@ pub fn NewPrinter(
i += 1;
// Escape this character if UTF-8 isn't allowed
- if (ascii_only) {
+ if (ascii_only_always_on_unless_minifying) {
var ptr = e.writer.reserve(12) catch unreachable;
ptr[0..12].* = [_]u8{
'\\', 'u', hex_chars[c >> 12], hex_chars[(c >> 8) & 15], hex_chars[(c >> 4) & 15], hex_chars[c & 15],
@@ -3899,7 +3910,7 @@ pub fn NewPrinter(
p.printSymbol(default_name.ref.?);
p.print(" = ");
- if (!bun) {
+ if (!is_bun_platform) {
p.print("(");
p.printSymbol(s.namespace_ref);
p.print(" && \"default\" in ");
@@ -3933,7 +3944,7 @@ pub fn NewPrinter(
}
}
pub fn printLoadFromBundle(p: *Printer, import_record_index: u32) void {
- if (bun) {
+ if (is_bun_platform) {
const record = p.import_records[import_record_index];
p.print("module.require(\"");
p.print(record.path.text);
@@ -4347,7 +4358,10 @@ pub fn NewPrinter(
var source_map_builder: SourceMap.Chunk.Builder = undefined;
if (comptime generate_source_map) {
- source_map_builder = SourceMap.Chunk.Builder{ .source_map = MutableString.initEmpty(allocator) };
+ source_map_builder = SourceMap.Chunk.Builder{
+ .source_map = SourceMap.Chunk.Builder.SourceMapper.init(allocator, is_bun_platform),
+ .cover_lines_without_mappings = true,
+ };
source_map_builder.line_offset_tables = SourceMap.LineOffsetTable.generate(allocator, source.contents, @intCast(i32, tree.approximate_newline_count));
}
diff --git a/src/options.zig b/src/options.zig
index 80034db12..b110e50ee 100644
--- a/src/options.zig
+++ b/src/options.zig
@@ -12,18 +12,18 @@ const resolve_path = @import("./resolver/resolve_path.zig");
const NodeModuleBundle = @import("./node_module_bundle.zig").NodeModuleBundle;
const URL = @import("./query_string_map.zig").URL;
const ConditionsMap = @import("./resolver/package_json.zig").ESModule.ConditionsMap;
-const _global = @import("global.zig");
-const string = _global.string;
-const Output = _global.Output;
-const Global = _global.Global;
-const Environment = _global.Environment;
-const strings = _global.strings;
-const MutableString = _global.MutableString;
-const FileDescriptorType = _global.FileDescriptorType;
-const stringZ = _global.stringZ;
-const default_allocator = _global.default_allocator;
-const C = _global.C;
-const StoredFileDescriptorType = _global.StoredFileDescriptorType;
+const bun = @import("global.zig");
+const string = bun.string;
+const Output = bun.Output;
+const Global = bun.Global;
+const Environment = bun.Environment;
+const strings = bun.strings;
+const MutableString = bun.MutableString;
+const FileDescriptorType = bun.FileDescriptorType;
+const stringZ = bun.stringZ;
+const default_allocator = bun.default_allocator;
+const C = bun.C;
+const StoredFileDescriptorType = bun.StoredFileDescriptorType;
const JSC = @import("javascript_core");
const Runtime = @import("./runtime.zig").Runtime;
const Analytics = @import("./analytics/analytics_thread.zig");
@@ -291,63 +291,63 @@ pub const ExternalModules = struct {
"zlib",
};
- pub const NodeBuiltinsMap = ComptimeStringMap(bool, .{
- .{ "_http_agent", true },
- .{ "_http_client", true },
- .{ "_http_common", true },
- .{ "_http_incoming", true },
- .{ "_http_outgoing", true },
- .{ "_http_server", true },
- .{ "_stream_duplex", true },
- .{ "_stream_passthrough", true },
- .{ "_stream_readable", true },
- .{ "_stream_transform", true },
- .{ "_stream_wrap", true },
- .{ "_stream_writable", true },
- .{ "_tls_common", true },
- .{ "_tls_wrap", true },
- .{ "assert", true },
- .{ "async_hooks", true },
- .{ "buffer", true },
- .{ "child_process", true },
- .{ "cluster", true },
- .{ "console", true },
- .{ "constants", true },
- .{ "crypto", true },
- .{ "dgram", true },
- .{ "diagnostics_channel", true },
- .{ "dns", true },
- .{ "domain", true },
- .{ "events", true },
- .{ "fs", true },
- .{ "http", true },
- .{ "http2", true },
- .{ "https", true },
- .{ "inspector", true },
- .{ "module", true },
- .{ "net", true },
- .{ "os", true },
- .{ "path", true },
- .{ "perf_hooks", true },
- .{ "process", true },
- .{ "punycode", true },
- .{ "querystring", true },
- .{ "readline", true },
- .{ "repl", true },
- .{ "stream", true },
- .{ "string_decoder", true },
- .{ "sys", true },
- .{ "timers", true },
- .{ "tls", true },
- .{ "trace_events", true },
- .{ "tty", true },
- .{ "url", true },
- .{ "util", true },
- .{ "v8", true },
- .{ "vm", true },
- .{ "wasi", true },
- .{ "worker_threads", true },
- .{ "zlib", true },
+ pub const NodeBuiltinsMap = ComptimeStringMap(void, .{
+ .{ "_http_agent", void{} },
+ .{ "_http_client", void{} },
+ .{ "_http_common", void{} },
+ .{ "_http_incoming", void{} },
+ .{ "_http_outgoing", void{} },
+ .{ "_http_server", void{} },
+ .{ "_stream_duplex", void{} },
+ .{ "_stream_passthrough", void{} },
+ .{ "_stream_readable", void{} },
+ .{ "_stream_transform", void{} },
+ .{ "_stream_wrap", void{} },
+ .{ "_stream_writable", void{} },
+ .{ "_tls_common", void{} },
+ .{ "_tls_wrap", void{} },
+ .{ "assert", void{} },
+ .{ "async_hooks", void{} },
+ .{ "buffer", void{} },
+ .{ "child_process", void{} },
+ .{ "cluster", void{} },
+ .{ "console", void{} },
+ .{ "constants", void{} },
+ .{ "crypto", void{} },
+ .{ "dgram", void{} },
+ .{ "diagnostics_channel", void{} },
+ .{ "dns", void{} },
+ .{ "domain", void{} },
+ .{ "events", void{} },
+ .{ "fs", void{} },
+ .{ "http", void{} },
+ .{ "http2", void{} },
+ .{ "https", void{} },
+ .{ "inspector", void{} },
+ .{ "module", void{} },
+ .{ "net", void{} },
+ .{ "os", void{} },
+ .{ "path", void{} },
+ .{ "perf_hooks", void{} },
+ .{ "process", void{} },
+ .{ "punycode", void{} },
+ .{ "querystring", void{} },
+ .{ "readline", void{} },
+ .{ "repl", void{} },
+ .{ "stream", void{} },
+ .{ "string_decoder", void{} },
+ .{ "sys", void{} },
+ .{ "timers", void{} },
+ .{ "tls", void{} },
+ .{ "trace_events", void{} },
+ .{ "tty", void{} },
+ .{ "url", void{} },
+ .{ "util", void{} },
+ .{ "v8", void{} },
+ .{ "vm", void{} },
+ .{ "wasi", void{} },
+ .{ "worker_threads", void{} },
+ .{ "zlib", void{} },
});
};