From 93fdbed85dc3fc4a714a746b4fc29ed5845ffccb Mon Sep 17 00:00:00 2001 From: pfg Date: Fri, 6 Dec 2024 20:03:20 -0800 Subject: [PATCH] add back whitespace checks but limited to 33 bytes --- src/bake/DevServer.zig | 2 +- src/bundler.zig | 8 ++++++++ src/bundler/bundle_v2.zig | 12 ++++++++++++ src/string_immutable.zig | 4 ++++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/bake/DevServer.zig b/src/bake/DevServer.zig index 9004984894..35394a1293 100644 --- a/src/bake/DevServer.zig +++ b/src/bake/DevServer.zig @@ -2051,7 +2051,7 @@ pub fn IncrementalGraph(side: bake.Side) type { if (Environment.allow_assert) { switch (kind) { .css => bun.assert(code.len == 0), - .js => if (code.len == 0 or (code.len < 33 and std.mem.trimLeft(u8, code, "\n\r ").len == 0)) { + .js => if (bun.strings.isSmallAndOnlyWhitespace(code)) { // Should at least contain the function wrapper bun.Output.panic("Empty chunk is impossible: {s} {s}", .{ key, diff --git a/src/bundler.zig b/src/bundler.zig index 54274b5ed1..f64cebed1f 100644 --- a/src/bundler.zig +++ b/src/bundler.zig @@ -1137,6 +1137,10 @@ pub const Bundler = struct { .ts, .tsx, => { + if (bun.strings.isSmallAndOnlyWhitespace(source.contents)) { + return ParseResult{ .source = source, .input_fd = input_fd, .loader = loader, .empty = true, .ast = js_ast.Ast.empty }; + } + // wasm magic number if (source.isWebAssembly()) { return ParseResult{ @@ -1251,6 +1255,10 @@ pub const Bundler = struct { }, // TODO: use lazy export AST inline .toml, .json => |kind| { + if (bun.strings.isSmallAndOnlyWhitespace(source.contents)) { + return ParseResult{ .source = source, .input_fd = input_fd, .loader = loader, .empty = true, .ast = js_ast.Ast.empty }; + } + var expr = if (kind == .json) // We allow importing tsconfig.*.json or jsconfig.*.json with comments // These files implicitly become JSONC files, which aligns with the behavior of text editors. diff --git a/src/bundler/bundle_v2.zig b/src/bundler/bundle_v2.zig index bbe7ee1543..55e3e220c1 100644 --- a/src/bundler/bundle_v2.zig +++ b/src/bundler/bundle_v2.zig @@ -3587,6 +3587,10 @@ pub const ParseTask = struct { ) !JSAst { switch (loader) { .jsx, .tsx, .js, .ts => { + if (bun.strings.isSmallAndOnlyWhitespace(source.contents)) return switch (opts.module_type == .esm) { + inline else => |as_undefined| try getEmptyAST(log, bundler, opts, allocator, source, if (as_undefined) E.Undefined else E.Object), + }; + const trace = tracer(@src(), "ParseJS"); defer trace.end(); return if (try resolver.caches.js.parse( @@ -3609,12 +3613,20 @@ pub const ParseTask = struct { }; }, .json => { + if (strings.isSmallAndOnlyWhitespace(source.contents)) return switch (opts.module_type == .esm) { + inline else => |as_undefined| try getEmptyAST(log, bundler, opts, allocator, source, if (as_undefined) E.Undefined else E.Object), + }; + const trace = tracer(@src(), "ParseJSON"); defer trace.end(); const root = (try resolver.caches.json.parsePackageJSON(log, source, allocator, false)) orelse Expr.init(E.Object, E.Object{}, Logger.Loc.Empty); return JSAst.init((try js_parser.newLazyExportAST(allocator, bundler.options.define, opts, log, root, &source, "")).?); }, .toml => { + if (strings.isSmallAndOnlyWhitespace(source.contents)) return switch (opts.module_type == .esm) { + inline else => |as_undefined| try getEmptyAST(log, bundler, opts, allocator, source, if (as_undefined) E.Undefined else E.Object), + }; + const trace = tracer(@src(), "ParseTOML"); defer trace.end(); const root = try TOML.parse(&source, log, allocator, false); diff --git a/src/string_immutable.zig b/src/string_immutable.zig index bfac2b814f..af9cadf0a1 100644 --- a/src/string_immutable.zig +++ b/src/string_immutable.zig @@ -4930,6 +4930,10 @@ pub fn trim(slice: anytype, comptime values_to_strip: []const u8) @TypeOf(slice) return slice[begin..end]; } +pub fn isSmallAndOnlyWhitespace(slice: string) bool { + return slice.len == 0 or (slice.len < 33 and std.mem.trimLeft(u8, slice, "\n\r ").len == 0); +} + pub const whitespace_chars = [_]u8{ ' ', '\t', '\n', '\r', std.ascii.control_code.vt, std.ascii.control_code.ff }; pub fn lengthOfLeadingWhitespaceASCII(slice: string) usize {