mirror of
https://github.com/oven-sh/bun
synced 2026-02-10 10:58:56 +00:00
Merge branch 'main' into zack/dev-server-sourcemaps-server-side
This commit is contained in:
@@ -1779,15 +1779,9 @@ pub fn appendSourceMapChunk(
|
||||
start_state.original_line += original_line.value;
|
||||
start_state.original_column += original_column.value;
|
||||
|
||||
j.push(
|
||||
appendMappingToBuffer(
|
||||
MutableString.initEmpty(allocator),
|
||||
j.lastByte(),
|
||||
prev_end_state,
|
||||
start_state,
|
||||
).list.items,
|
||||
allocator,
|
||||
);
|
||||
var str = MutableString.initEmpty(allocator);
|
||||
appendMappingToBuffer(&str, j.lastByte(), prev_end_state, start_state);
|
||||
j.push(str.slice(), allocator);
|
||||
|
||||
// Then append everything after that without modification.
|
||||
j.pushStatic(source_map);
|
||||
@@ -1812,8 +1806,7 @@ pub fn appendSourceMappingURLRemote(
|
||||
}
|
||||
|
||||
/// This function is extremely hot.
|
||||
pub fn appendMappingToBuffer(buffer_: MutableString, last_byte: u8, prev_state: SourceMapState, current_state: SourceMapState) MutableString {
|
||||
var buffer = buffer_;
|
||||
pub fn appendMappingToBuffer(buffer: *MutableString, last_byte: u8, prev_state: SourceMapState, current_state: SourceMapState) void {
|
||||
const needs_comma = last_byte != 0 and last_byte != ';' and last_byte != '"';
|
||||
|
||||
const vlqs = [_]VLQ{
|
||||
@@ -1846,8 +1839,6 @@ pub fn appendMappingToBuffer(buffer_: MutableString, last_byte: u8, prev_state:
|
||||
@memcpy(writable[0..item.len], item.slice());
|
||||
writable = writable[item.len..];
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
pub const Chunk = struct {
|
||||
@@ -1867,22 +1858,28 @@ pub const Chunk = struct {
|
||||
/// ignore empty chunks
|
||||
should_ignore: bool = true,
|
||||
|
||||
pub const empty: Chunk = .{
|
||||
.buffer = MutableString.initEmpty(bun.default_allocator),
|
||||
.mappings_count = 0,
|
||||
.end_state = .{},
|
||||
.final_generated_column = 0,
|
||||
.should_ignore = true,
|
||||
};
|
||||
pub fn initEmpty() Chunk {
|
||||
return .{
|
||||
.buffer = MutableString.initEmpty(bun.default_allocator),
|
||||
.mappings_count = 0,
|
||||
.end_state = .{},
|
||||
.final_generated_column = 0,
|
||||
.should_ignore = true,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(this: *Chunk) void {
|
||||
this.buffer.deinit();
|
||||
}
|
||||
|
||||
pub fn printSourceMapContents(
|
||||
chunk: Chunk,
|
||||
source: *const Logger.Source,
|
||||
mutable: MutableString,
|
||||
mutable: *MutableString,
|
||||
include_sources_contents: bool,
|
||||
comptime ascii_only: bool,
|
||||
) !MutableString {
|
||||
return printSourceMapContentsAtOffset(
|
||||
) !void {
|
||||
try printSourceMapContentsAtOffset(
|
||||
chunk,
|
||||
source,
|
||||
mutable,
|
||||
@@ -1895,13 +1892,11 @@ pub const Chunk = struct {
|
||||
pub fn printSourceMapContentsAtOffset(
|
||||
chunk: Chunk,
|
||||
source: *const Logger.Source,
|
||||
mutable: MutableString,
|
||||
mutable: *MutableString,
|
||||
include_sources_contents: bool,
|
||||
offset: usize,
|
||||
comptime ascii_only: bool,
|
||||
) !MutableString {
|
||||
var output = mutable;
|
||||
|
||||
) !void {
|
||||
// attempt to pre-allocate
|
||||
|
||||
var filename_buf: bun.PathBuffer = undefined;
|
||||
@@ -1914,23 +1909,21 @@ pub const Chunk = struct {
|
||||
filename = filename_buf[0 .. filename.len + 1];
|
||||
}
|
||||
|
||||
output.growIfNeeded(
|
||||
mutable.growIfNeeded(
|
||||
filename.len + 2 + (source.contents.len * @as(usize, @intFromBool(include_sources_contents))) + (chunk.buffer.list.items.len - offset) + 32 + 39 + 29 + 22 + 20,
|
||||
) catch unreachable;
|
||||
try output.append("{\n \"version\":3,\n \"sources\": [");
|
||||
try mutable.append("{\n \"version\":3,\n \"sources\": [");
|
||||
|
||||
output = try JSPrinter.quoteForJSON(filename, output, ascii_only);
|
||||
try JSPrinter.quoteForJSON(filename, mutable, ascii_only);
|
||||
|
||||
if (include_sources_contents) {
|
||||
try output.append("],\n \"sourcesContent\": [");
|
||||
output = try JSPrinter.quoteForJSON(source.contents, output, ascii_only);
|
||||
try mutable.append("],\n \"sourcesContent\": [");
|
||||
try JSPrinter.quoteForJSON(source.contents, mutable, ascii_only);
|
||||
}
|
||||
|
||||
try output.append("],\n \"mappings\": ");
|
||||
output = try JSPrinter.quoteForJSON(chunk.buffer.list.items[offset..], output, ascii_only);
|
||||
try output.append(", \"names\": []\n}");
|
||||
|
||||
return output;
|
||||
try mutable.append("],\n \"mappings\": ");
|
||||
try JSPrinter.quoteForJSON(chunk.buffer.list.items[offset..], mutable, ascii_only);
|
||||
try mutable.append(", \"names\": []\n}");
|
||||
}
|
||||
|
||||
// TODO: remove the indirection by having generic functions for SourceMapFormat and NewBuilder. Source maps are always VLQ
|
||||
@@ -1959,6 +1952,10 @@ pub const Chunk = struct {
|
||||
return this.ctx.getBuffer();
|
||||
}
|
||||
|
||||
pub inline fn takeBuffer(this: *Format) MutableString {
|
||||
return this.ctx.takeBuffer();
|
||||
}
|
||||
|
||||
pub inline fn getCount(this: Format) usize {
|
||||
return this.ctx.getCount();
|
||||
}
|
||||
@@ -1971,8 +1968,6 @@ pub const Chunk = struct {
|
||||
offset: usize = 0,
|
||||
approximate_input_line_count: usize = 0,
|
||||
|
||||
pub const Format = SourceMapFormat(VLQSourceMap);
|
||||
|
||||
pub fn init(allocator: std.mem.Allocator, prepend_count: bool) VLQSourceMap {
|
||||
var map = VLQSourceMap{
|
||||
.data = MutableString.initEmpty(allocator),
|
||||
@@ -1997,7 +1992,7 @@ pub const Chunk = struct {
|
||||
else
|
||||
0;
|
||||
|
||||
this.data = appendMappingToBuffer(this.data, last_byte, prev_state, current_state);
|
||||
appendMappingToBuffer(&this.data, last_byte, prev_state, current_state);
|
||||
this.count += 1;
|
||||
}
|
||||
|
||||
@@ -2009,6 +2004,11 @@ pub const Chunk = struct {
|
||||
return this.data;
|
||||
}
|
||||
|
||||
pub fn takeBuffer(this: *VLQSourceMap) MutableString {
|
||||
defer this.data = .initEmpty(this.data.allocator);
|
||||
return this.data;
|
||||
}
|
||||
|
||||
pub fn getCount(this: VLQSourceMap) usize {
|
||||
return this.count;
|
||||
}
|
||||
@@ -2017,7 +2017,6 @@ pub const Chunk = struct {
|
||||
pub fn NewBuilder(comptime SourceMapFormatType: type) type {
|
||||
return struct {
|
||||
const ThisBuilder = @This();
|
||||
input_source_map: ?*SourceMap = null,
|
||||
source_map: SourceMapper,
|
||||
line_offset_tables: LineOffsetTable.List = .{},
|
||||
prev_state: SourceMapState = SourceMapState{},
|
||||
@@ -2048,13 +2047,14 @@ pub const Chunk = struct {
|
||||
|
||||
pub noinline fn generateChunk(b: *ThisBuilder, output: []const u8) Chunk {
|
||||
b.updateGeneratedLineAndColumn(output);
|
||||
var buffer = b.source_map.getBuffer();
|
||||
if (b.prepend_count) {
|
||||
b.source_map.getBuffer().list.items[0..8].* = @as([8]u8, @bitCast(b.source_map.getBuffer().list.items.len));
|
||||
b.source_map.getBuffer().list.items[8..16].* = @as([8]u8, @bitCast(b.source_map.getCount()));
|
||||
b.source_map.getBuffer().list.items[16..24].* = @as([8]u8, @bitCast(b.approximate_input_line_count));
|
||||
buffer.list.items[0..8].* = @as([8]u8, @bitCast(buffer.list.items.len));
|
||||
buffer.list.items[8..16].* = @as([8]u8, @bitCast(b.source_map.getCount()));
|
||||
buffer.list.items[16..24].* = @as([8]u8, @bitCast(b.approximate_input_line_count));
|
||||
}
|
||||
return Chunk{
|
||||
.buffer = b.source_map.getBuffer(),
|
||||
.buffer = b.source_map.takeBuffer(),
|
||||
.mappings_count = b.source_map.getCount(),
|
||||
.end_state = b.prev_state,
|
||||
.final_generated_column = b.generated_column,
|
||||
@@ -2130,17 +2130,7 @@ pub const Chunk = struct {
|
||||
b.last_generated_update = @as(u32, @truncate(output.len));
|
||||
}
|
||||
|
||||
pub fn appendMapping(b: *ThisBuilder, current_state_: SourceMapState) void {
|
||||
var current_state = current_state_;
|
||||
// If the input file had a source map, map all the way back to the original
|
||||
if (b.input_source_map) |input| {
|
||||
if (input.find(current_state.original_line, current_state.original_column)) |mapping| {
|
||||
current_state.source_index = mapping.sourceIndex();
|
||||
current_state.original_line = mapping.originalLine();
|
||||
current_state.original_column = mapping.originalColumn();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn appendMapping(b: *ThisBuilder, current_state: SourceMapState) void {
|
||||
b.appendMappingWithoutRemapping(current_state);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user