From bd9f137b1bfb5bc3b215515ff9305e70a638daf9 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 29 Aug 2021 21:48:14 -0700 Subject: latest Former-commit-id: 096ec1222ad723d006b0151f10cb0c1b95e2bfd3 --- src/api/schema.peechy | 1 - src/api/schema.zig | 2925 ++++++++++++++---------------- src/bundler.zig | 573 +++--- src/fs.zig | 16 +- src/hash_map_v2.zig | 6 +- src/import_record.zig | 2 + src/javascript/jsc/bindings/bindings.zig | 2 +- src/javascript/jsc/bindings/exports.zig | 12 +- src/javascript/jsc/javascript.zig | 30 +- src/js_parser/js_parser.zig | 104 +- src/js_printer.zig | 202 ++- src/linker.zig | 161 +- src/node_module_bundle.zig | 2 +- src/options.zig | 102 +- src/panic_handler.zig | 2 - src/query_string_map.zig | 23 +- src/resolver/package_json.zig | 55 +- src/resolver/resolver.zig | 89 +- src/runtime.js | 10 +- src/runtime.version | 2 +- src/string_immutable.zig | 16 +- src/test/fixtures/browsermap-false.ts | 3 + src/test/fixtures/main-field.ts | 3 + src/test/fixtures/package.json | 7 - src/test/fixtures/type-only-import.ts | 33 + src/test/project.zig | 139 ++ 26 files changed, 2552 insertions(+), 1968 deletions(-) create mode 100644 src/test/fixtures/browsermap-false.ts create mode 100644 src/test/fixtures/main-field.ts delete mode 100644 src/test/fixtures/package.json create mode 100644 src/test/fixtures/type-only-import.ts (limited to 'src') diff --git a/src/api/schema.peechy b/src/api/schema.peechy index f296b6452..fd0b81fd2 100644 --- a/src/api/schema.peechy +++ b/src/api/schema.peechy @@ -90,7 +90,6 @@ struct JavascriptBundle { // generated by hashing all ${name}@${version} in sorted order byte[] app_package_json_dependencies_hash; - byte[] import_from_name; // This is what StringPointer refers to diff --git a/src/api/schema.zig b/src/api/schema.zig index 3e542b035..639eeae98 100644 --- a/src/api/schema.zig +++ b/src/api/schema.zig @@ -1,4 +1,3 @@ - const std = @import("std"); pub const Reader = struct { @@ -282,1872 +281,1762 @@ pub fn Writer(comptime WritableStream: type) type { pub const ByteWriter = Writer(*std.io.FixedBufferStream([]u8)); pub const FileWriter = Writer(std.fs.File); +pub const Api = struct { + pub const Loader = enum(u8) { + _none, + /// jsx + jsx, + /// js + js, + /// ts + ts, -pub const Api = struct { - -pub const Loader = enum(u8) { - -_none, - /// jsx - jsx, - - /// js - js, - - /// ts - ts, - - /// tsx - tsx, - - /// css - css, - - /// file - file, - - /// json - json, - -_, - - pub fn jsonStringify(self: *const @This(), opts: anytype, o: anytype) !void { - return try std.json.stringify(@tagName(self), opts, o); - } - - -}; - -pub const ResolveMode = enum(u8) { - -_none, - /// disable - disable, + /// tsx + tsx, - /// lazy - lazy, + /// css + css, - /// dev - dev, - - /// bundle - bundle, - -_, - - pub fn jsonStringify(self: *const @This(), opts: anytype, o: anytype) !void { - return try std.json.stringify(@tagName(self), opts, o); - } - - -}; - -pub const Platform = enum(u8) { - -_none, - /// browser - browser, - - /// node - node, - - /// bun - bun, - -_, - - pub fn jsonStringify(self: *const @This(), opts: anytype, o: anytype) !void { - return try std.json.stringify(@tagName(self), opts, o); - } - - -}; + /// file + file, -pub const CssInJsBehavior = enum(u8) { + /// json + json, -_none, - /// facade - facade, + _, - /// facade_onimportcss - facade_onimportcss, + pub fn jsonStringify(self: *const @This(), opts: anytype, o: anytype) !void { + return try std.json.stringify(@tagName(self), opts, o); + } + }; - /// auto_onimportcss - auto_onimportcss, + pub const ResolveMode = enum(u8) { + _none, + /// disable + disable, -_, + /// lazy + lazy, - pub fn jsonStringify(self: *const @This(), opts: anytype, o: anytype) !void { - return try std.json.stringify(@tagName(self), opts, o); - } + /// dev + dev, - -}; + /// bundle + bundle, -pub const JsxRuntime = enum(u8) { + _, -_none, - /// automatic - automatic, + pub fn jsonStringify(self: *const @This(), opts: anytype, o: anytype) !void { + return try std.json.stringify(@tagName(self), opts, o); + } + }; - /// classic - classic, + pub const Platform = enum(u8) { + _none, + /// browser + browser, -_, + /// node + node, - pub fn jsonStringify(self: *const @This(), opts: anytype, o: anytype) !void { - return try std.json.stringify(@tagName(self), opts, o); - } + /// bun + bun, - -}; + _, -pub const Jsx = struct { -/// factory -factory: []const u8, + pub fn jsonStringify(self: *const @This(), opts: anytype, o: anytype) !void { + return try std.json.stringify(@tagName(self), opts, o); + } + }; -/// runtime -runtime: JsxRuntime, + pub const CssInJsBehavior = enum(u8) { + _none, + /// facade + facade, -/// fragment -fragment: []const u8, + /// facade_onimportcss + facade_onimportcss, -/// development -development: bool = false, + /// auto_onimportcss + auto_onimportcss, -/// import_source -import_source: []const u8, + _, -/// react_fast_refresh -react_fast_refresh: bool = false, + pub fn jsonStringify(self: *const @This(), opts: anytype, o: anytype) !void { + return try std.json.stringify(@tagName(self), opts, o); + } + }; + pub const JsxRuntime = enum(u8) { + _none, + /// automatic + automatic, -pub fn decode(reader: anytype) anyerror!Jsx { - var this = std.mem.zeroes(Jsx); + /// classic + classic, - this.factory = try reader.readValue([]const u8); - this.runtime = try reader.readValue(JsxRuntime); - this.fragment = try reader.readValue([]const u8); - this.development = try reader.readValue(bool); - this.import_source = try reader.readValue([]const u8); - this.react_fast_refresh = try reader.readValue(bool); - return this; -} + _, -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { - try writer.writeValue(this.factory); - try writer.writeEnum(this.runtime); - try writer.writeValue(this.fragment); - try writer.writeInt(@intCast(u8, @boolToInt(this.development))); - try writer.writeValue(this.import_source); - try writer.writeInt(@intCast(u8, @boolToInt(this.react_fast_refresh))); -} + pub fn jsonStringify(self: *const @This(), opts: anytype, o: anytype) !void { + return try std.json.stringify(@tagName(self), opts, o); + } + }; -}; + pub const Jsx = struct { + /// factory + factory: []const u8, -pub const StringPointer = packed struct { -/// offset -offset: u32 = 0, + /// runtime + runtime: JsxRuntime, -/// length -length: u32 = 0, + /// fragment + fragment: []const u8, + /// development + development: bool = false, -pub fn decode(reader: anytype) anyerror!StringPointer { - var this = std.mem.zeroes(StringPointer); + /// import_source + import_source: []const u8, - this.offset = try reader.readValue(u32); - this.length = try reader.readValue(u32); - return this; -} + /// react_fast_refresh + react_fast_refresh: bool = false, -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { - try writer.writeInt(this.offset); - try writer.writeInt(this.length); -} + pub fn decode(reader: anytype) anyerror!Jsx { + var this = std.mem.zeroes(Jsx); -}; + this.factory = try reader.readValue([]const u8); + this.runtime = try reader.readValue(JsxRuntime); + this.fragment = try reader.readValue([]const u8); + this.development = try reader.readValue(bool); + this.import_source = try reader.readValue([]const u8); + this.react_fast_refresh = try reader.readValue(bool); + return this; + } -pub const JavascriptBundledModule = struct { -/// path -path: StringPointer, + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + try writer.writeValue(this.factory); + try writer.writeEnum(this.runtime); + try writer.writeValue(this.fragment); + try writer.writeInt(@intCast(u8, @boolToInt(this.development))); + try writer.writeValue(this.import_source); + try writer.writeInt(@intCast(u8, @boolToInt(this.react_fast_refresh))); + } + }; -/// code -code: StringPointer, + pub const StringPointer = packed struct { + /// offset + offset: u32 = 0, -/// package_id -package_id: u32 = 0, + /// length + length: u32 = 0, -/// id -id: u32 = 0, + pub fn decode(reader: anytype) anyerror!StringPointer { + var this = std.mem.zeroes(StringPointer); -/// path_extname_length -path_extname_length: u8 = 0, + this.offset = try reader.readValue(u32); + this.length = try reader.readValue(u32); + return this; + } + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + try writer.writeInt(this.offset); + try writer.writeInt(this.length); + } + }; -pub fn decode(reader: anytype) anyerror!JavascriptBundledModule { - var this = std.mem.zeroes(JavascriptBundledModule); + pub const JavascriptBundledModule = struct { + /// path + path: StringPointer, - this.path = try reader.readValue(StringPointer); - this.code = try reader.readValue(StringPointer); - this.package_id = try reader.readValue(u32); - this.id = try reader.readValue(u32); - this.path_extname_length = try reader.readValue(u8); - return this; -} + /// code + code: StringPointer, -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { - try writer.writeValue(this.path); - try writer.writeValue(this.code); - try writer.writeInt(this.package_id); - try writer.writeInt(this.id); - try writer.writeInt(this.path_extname_length); -} + /// package_id + package_id: u32 = 0, -}; + /// id + id: u32 = 0, -pub const JavascriptBundledPackage = struct { -/// name -name: StringPointer, + /// path_extname_length + path_extname_length: u8 = 0, -/// version -version: StringPointer, + pub fn decode(reader: anytype) anyerror!JavascriptBundledModule { + var this = std.mem.zeroes(JavascriptBundledModule); -/// hash -hash: u32 = 0, + this.path = try reader.readValue(StringPointer); + this.code = try reader.readValue(StringPointer); + this.package_id = try reader.readValue(u32); + this.id = try reader.readValue(u32); + this.path_extname_length = try reader.readValue(u8); + return this; + } -/// modules_offset -modules_offset: u32 = 0, + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + try writer.writeValue(this.path); + try writer.writeValue(this.code); + try writer.writeInt(this.package_id); + try writer.writeInt(this.id); + try writer.writeInt(this.path_extname_length); + } + }; -/// modules_length -modules_length: u32 = 0, + pub const JavascriptBundledPackage = struct { + /// name + name: StringPointer, + /// version + version: StringPointer, -pub fn decode(reader: anytype) anyerror!JavascriptBundledPackage { - var this = std.mem.zeroes(JavascriptBundledPackage); + /// hash + hash: u32 = 0, - this.name = try reader.readValue(StringPointer); - this.version = try reader.readValue(StringPointer); - this.hash = try reader.readValue(u32); - this.modules_offset = try reader.readValue(u32); - this.modules_length = try reader.readValue(u32); - return this; -} + /// modules_offset + modules_offset: u32 = 0, -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { - try writer.writeValue(this.name); - try writer.writeValue(this.version); - try writer.writeInt(this.hash); - try writer.writeInt(this.modules_offset); - try writer.writeInt(this.modules_length); -} + /// modules_length + modules_length: u32 = 0, -}; + pub fn decode(reader: anytype) anyerror!JavascriptBundledPackage { + var this = std.mem.zeroes(JavascriptBundledPackage); -pub const JavascriptBundle = struct { -/// modules -modules: []const JavascriptBundledModule, + this.name = try reader.readValue(StringPointer); + this.version = try reader.readValue(StringPointer); + this.hash = try reader.readValue(u32); + this.modules_offset = try reader.readValue(u32); + this.modules_length = try reader.readValue(u32); + return this; + } -/// packages -packages: []const JavascriptBundledPackage, + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + try writer.writeValue(this.name); + try writer.writeValue(this.version); + try writer.writeInt(this.hash); + try writer.writeInt(this.modules_offset); + try writer.writeInt(this.modules_length); + } + }; -/// etag -etag: []const u8, + pub const JavascriptBundle = struct { + /// modules + modules: []const JavascriptBundledModule, -/// generated_at -generated_at: u32 = 0, + /// packages + packages: []const JavascriptBundledPackage, -/// app_package_json_dependencies_hash -app_package_json_dependencies_hash: []const u8, + /// etag + etag: []const u8, -/// import_from_name -import_from_name: []const u8, + /// generated_at + generated_at: u32 = 0, -/// manifest_string -manifest_string: []const u8, + /// app_package_json_dependencies_hash + app_package_json_dependencies_hash: []const u8, + /// import_from_name + import_from_name: []const u8, -pub fn decode(reader: anytype) anyerror!JavascriptBundle { - var this = std.mem.zeroes(JavascriptBundle); + /// manifest_string + manifest_string: []const u8, - this.modules = try reader.readArray(JavascriptBundledModule); - this.packages = try reader.readArray(JavascriptBundledPackage); - this.etag = try reader.readArray(u8); - this.generated_at = try reader.readValue(u32); - this.app_package_json_dependencies_hash = try reader.readArray(u8); - this.import_from_name = try reader.readArray(u8); - this.manifest_string = try reader.readArray(u8); - return this; -} + pub fn decode(reader: anytype) anyerror!JavascriptBundle { + var this = std.mem.zeroes(JavascriptBundle); -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { - try writer.writeArray(JavascriptBundledModule, this.modules); - try writer.writeArray(JavascriptBundledPackage, this.packages); - try writer.writeArray(u8, this.etag); - try writer.writeInt(this.generated_at); - try writer.writeArray(u8, this.app_package_json_dependencies_hash); - try writer.writeArray(u8, this.import_from_name); - try writer.writeArray(u8, this.manifest_string); -} + this.modules = try reader.readArray(JavascriptBundledModule); + this.packages = try reader.readArray(JavascriptBundledPackage); + this.etag = try reader.readArray(u8); + this.generated_at = try reader.readValue(u32); + this.app_package_json_dependencies_hash = try reader.readArray(u8); + this.import_from_name = try reader.readArray(u8); + this.manifest_string = try reader.readArray(u8); + return this; + } -}; + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + try writer.writeArray(JavascriptBundledModule, this.modules); + try writer.writeArray(JavascriptBundledPackage, this.packages); + try writer.writeArray(u8, this.etag); + try writer.writeInt(this.generated_at); + try writer.writeArray(u8, this.app_package_json_dependencies_hash); + try writer.writeArray(u8, this.import_from_name); + try writer.writeArray(u8, this.manifest_string); + } + }; -pub const JavascriptBundleContainer = struct { -/// bundle_format_version -bundle_format_version: ?u32 = null, - -/// routes -routes: ?LoadedRouteConfig = null, - -/// framework -framework: ?LoadedFramework = null, - -/// bundle -bundle: ?JavascriptBundle = null, - -/// code_length -code_length: ?u32 = null, - - -pub fn decode(reader: anytype) anyerror!JavascriptBundleContainer { - var this = std.mem.zeroes(JavascriptBundleContainer); - - while(true) { - switch (try reader.readByte()) { - 0 => { return this; }, - - 1 => { - this.bundle_format_version = try reader.readValue(u32); -}, - 2 => { - this.routes = try reader.readValue(LoadedRouteConfig); -}, - 3 => { - this.framework = try reader.readValue(LoadedFramework); -}, - 4 => { - this.bundle = try reader.readValue(JavascriptBundle); -}, - 5 => { - this.code_length = try reader.readValue(u32); -}, - else => { - return error.InvalidMessage; - }, - } - } -unreachable; -} + pub const JavascriptBundleContainer = struct { + /// bundle_format_version + bundle_format_version: ?u32 = null, -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { -if (this.bundle_format_version) |bundle_format_version| { - try writer.writeFieldID(1); - try writer.writeInt(bundle_format_version); -} -if (this.routes) |routes| { - try writer.writeFieldID(2); - try writer.writeValue(routes); -} -if (this.framework) |framework| { - try writer.writeFieldID(3); - try writer.writeValue(framework); -} -if (this.bundle) |bundle| { - try writer.writeFieldID(4); - try writer.writeValue(bundle); -} -if (this.code_length) |code_length| { - try writer.writeFieldID(5); - try writer.writeInt(code_length); -} -try writer.endMessage(); -} + /// routes + routes: ?LoadedRouteConfig = null, -}; + /// framework + framework: ?LoadedFramework = null, -pub const ScanDependencyMode = enum(u8) { + /// bundle + bundle: ?JavascriptBundle = null, -_none, - /// app - app, + /// code_length + code_length: ?u32 = null, - /// all - all, + pub fn decode(reader: anytype) anyerror!JavascriptBundleContainer { + var this = std.mem.zeroes(JavascriptBundleContainer); -_, + while (true) { + switch (try reader.readByte()) { + 0 => { + return this; + }, - pub fn jsonStringify(self: *const @This(), opts: anytype, o: anytype) !void { - return try std.json.stringify(@tagName(self), opts, o); + 1 => { + this.bundle_format_version = try reader.readValue(u32); + }, + 2 => { + this.routes = try reader.readValue(LoadedRouteConfig); + }, + 3 => { + this.framework = try reader.readValue(LoadedFramework); + }, + 4 => { + this.bundle = try reader.readValue(JavascriptBundle); + }, + 5 => { + this.code_length = try reader.readValue(u32); + }, + else => { + return error.InvalidMessage; + }, } + } + unreachable; + } - -}; - -pub const ModuleImportType = enum(u8) { - -_none, - /// import - import, + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + if (this.bundle_format_version) |bundle_format_version| { + try writer.writeFieldID(1); + try writer.writeInt(bundle_format_version); + } + if (this.routes) |routes| { + try writer.writeFieldID(2); + try writer.writeValue(routes); + } + if (this.framework) |framework| { + try writer.writeFieldID(3); + try writer.writeValue(framework); + } + if (this.bundle) |bundle| { + try writer.writeFieldID(4); + try writer.writeValue(bundle); + } + if (this.code_length) |code_length| { + try writer.writeFieldID(5); + try writer.writeInt(code_length); + } + try writer.endMessage(); + } + }; - /// require - require, + pub const ScanDependencyMode = enum(u8) { + _none, + /// app + app, -_, + /// all + all, - pub fn jsonStringify(self: *const @This(), opts: anytype, o: anytype) !void { - return try std.json.stringify(@tagName(self), opts, o); - } + _, - -}; + pub fn jsonStringify(self: *const @This(), opts: anytype, o: anytype) !void { + return try std.json.stringify(@tagName(self), opts, o); + } + }; -pub const ModuleImportRecord = struct { -/// kind -kind: ModuleImportType, + pub const ModuleImportType = enum(u8) { + _none, + /// import + import, -/// path -path: []const u8, + /// require + require, -/// dynamic -dynamic: bool = false, + _, + pub fn jsonStringify(self: *const @This(), opts: anytype, o: anytype) !void { + return try std.json.stringify(@tagName(self), opts, o); + } + }; -pub fn decode(reader: anytype) anyerror!ModuleImportRecord { - var this = std.mem.zeroes(ModuleImportRecord); + pub const ModuleImportRecord = struct { + /// kind + kind: ModuleImportType, - this.kind = try reader.readValue(ModuleImportType); - this.path = try reader.readValue([]const u8); - this.dynamic = try reader.readValue(bool); - return this; -} + /// path + path: []const u8, -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { - try writer.writeEnum(this.kind); - try writer.writeValue(this.path); - try writer.writeInt(@intCast(u8, @boolToInt(this.dynamic))); -} + /// dynamic + dynamic: bool = false, -}; + pub fn decode(reader: anytype) anyerror!ModuleImportRecord { + var this = std.mem.zeroes(ModuleImportRecord); -pub const Module = struct { -/// path -path: []const u8, + this.kind = try reader.readValue(ModuleImportType); + this.path = try reader.readValue([]const u8); + this.dynamic = try reader.readValue(bool); + return this; + } -/// imports -imports: []const ModuleImportRecord, + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + try writer.writeEnum(this.kind); + try writer.writeValue(this.path); + try writer.writeInt(@intCast(u8, @boolToInt(this.dynamic))); + } + }; + pub const Module = struct { + /// path + path: []const u8, -pub fn decode(reader: anytype) anyerror!Module { - var this = std.mem.zeroes(Module); + /// imports + imports: []const ModuleImportRecord, - this.path = try reader.readValue([]const u8); - this.imports = try reader.readArray(ModuleImportRecord); - return this; -} + pub fn decode(reader: anytype) anyerror!Module { + var this = std.mem.zeroes(Module); -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { - try writer.writeValue(this.path); - try writer.writeArray(ModuleImportRecord, this.imports); -} + this.path = try reader.readValue([]const u8); + this.imports = try reader.readArray(ModuleImportRecord); + return this; + } -}; + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + try writer.writeValue(this.path); + try writer.writeArray(ModuleImportRecord, this.imports); + } + }; -pub const StringMap = struct { -/// keys -keys: []const []const u8, + pub const StringMap = struct { + /// keys + keys: []const []const u8, -/// values -values: []const []const u8, + /// values + values: []const []const u8, + pub fn decode(reader: anytype) anyerror!StringMap { + var this = std.mem.zeroes(StringMap); -pub fn decode(reader: anytype) anyerror!StringMap { - var this = std.mem.zeroes(StringMap); + this.keys = try reader.readArray([]const u8); + this.values = try reader.readArray([]const u8); + return this; + } - this.keys = try reader.readArray([]const u8); - this.values = try reader.readArray([]const u8); - return this; -} + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + try writer.writeArray([]const u8, this.keys); + try writer.writeArray([]const u8, this.values); + } + }; -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { - try writer.writeArray([]const u8, this.keys); - try writer.writeArray([]const u8, this.values); -} + pub const LoaderMap = struct { + /// extensions + extensions: []const []const u8, -}; + /// loaders + loaders: []const Loader, -pub const LoaderMap = struct { -/// extensions -extensions: []const []const u8, + pub fn decode(reader: anytype) anyerror!LoaderMap { + var this = std.mem.zeroes(LoaderMap); -/// loaders -loaders: []const Loader, + this.extensions = try reader.readArray([]const u8); + this.loaders = try reader.readArray(Loader); + return this; + } + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + try writer.writeArray([]const u8, this.extensions); + try writer.writeArray(Loader, this.loaders); + } + }; -pub fn decode(reader: anytype) anyerror!LoaderMap { - var this = std.mem.zeroes(LoaderMap); + pub const DotEnvBehavior = enum(u32) { + _none, + /// disable + disable, - this.extensions = try reader.readArray([]const u8); - this.loaders = try reader.readArray(Loader); - return this; -} + /// prefix + prefix, -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { - try writer.writeArray([]const u8, this.extensions); - try writer.writeArray(Loader, this.loaders); -} + /// load_all + load_all, -}; + _, -pub const DotEnvBehavior = enum(u32) { + pub fn jsonStringify(self: *const @This(), opts: anytype, o: anytype) !void { + return try std.json.stringify(@tagName(self), opts, o); + } + }; -_none, - /// disable - disable, + pub const EnvConfig = struct { + /// prefix + prefix: ?[]const u8 = null, - /// prefix - prefix, + /// defaults + defaults: ?StringMap = null, - /// load_all - load_all, + pub fn decode(reader: anytype) anyerror!EnvConfig { + var this = std.mem.zeroes(EnvConfig); -_, + while (true) { + switch (try reader.readByte()) { + 0 => { + return this; + }, - pub fn jsonStringify(self: *const @This(), opts: anytype, o: anytype) !void { - return try std.json.stringify(@tagName(self), opts, o); + 1 => { + this.prefix = try reader.readValue([]const u8); + }, + 2 => { + this.defaults = try reader.readValue(StringMap); + }, + else => { + return error.InvalidMessage; + }, } + } + unreachable; + } - -}; - -pub const EnvConfig = struct { -/// prefix -prefix: ?[]const u8 = null, - -/// defaults -defaults: ?StringMap = null, - - -pub fn decode(reader: anytype) anyerror!EnvConfig { - var this = std.mem.zeroes(EnvConfig); - - while(true) { - switch (try reader.readByte()) { - 0 => { return this; }, - - 1 => { - this.prefix = try reader.readValue([]const u8); -}, - 2 => { - this.defaults = try reader.readValue(StringMap); -}, - else => { - return error.InvalidMessage; - }, - } - } -unreachable; -} - -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { -if (this.prefix) |prefix| { - try writer.writeFieldID(1); - try writer.writeValue(prefix); -} -if (this.defaults) |defaults| { - try writer.writeFieldID(2); - try writer.writeValue(defaults); -} -try writer.endMessage(); -} - -}; - -pub const LoadedEnvConfig = struct { -/// dotenv -dotenv: DotEnvBehavior, - -/// defaults -defaults: StringMap, - -/// prefix -prefix: []const u8, - - -pub fn decode(reader: anytype) anyerror!LoadedEnvConfig { - var this = std.mem.zeroes(LoadedEnvConfig); - - this.dotenv = try reader.readValue(DotEnvBehavior); - this.defaults = try reader.readValue(StringMap); - this.prefix = try reader.readValue([]const u8); - return this; -} - -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { - try writer.writeEnum(this.dotenv); - try writer.writeValue(this.defaults); - try writer.writeValue(this.prefix); -} - -}; - -pub const FrameworkConfig = struct { -/// package -package: ?[]const u8 = null, - -/// client -client: ?[]const u8 = null, - -/// server -server: ?[]const u8 = null, - -/// development -development: ?bool = null, - -/// client_env -client_env: ?EnvConfig = null, - -/// server_env -server_env: ?EnvConfig = null, - -/// client_css_in_js -client_css_in_js: ?CssInJsBehavior = null, - - -pub fn decode(reader: anytype) anyerror!FrameworkConfig { - var this = std.mem.zeroes(FrameworkConfig); - - while(true) { - switch (try reader.readByte()) { - 0 => { return this; }, - - 1 => { - this.package = try reader.readValue([]const u8); -}, - 2 => { - this.client = try reader.readValue([]const u8); -}, - 3 => { - this.server = try reader.readValue([]const u8); -}, - 4 => { - this.development = try reader.readValue(bool); -}, - 5 => { - this.client_env = try reader.readValue(EnvConfig); -}, - 6 => { - this.server_env = try reader.readValue(EnvConfig); -}, - 7 => { - this.client_css_in_js = try reader.readValue(CssInJsBehavior); -}, - else => { - return error.InvalidMessage; - }, - } - } -unreachable; -} - -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { -if (this.package) |package| { - try writer.writeFieldID(1); - try writer.writeValue(package); -} -if (this.client) |client| { - try writer.writeFieldID(2); - try writer.writeValue(client); -} -if (this.server) |server| { - try writer.writeFieldID(3); - try writer.writeValue(server); -} -if (this.development) |development| { - try writer.writeFieldID(4); - try writer.writeInt(@intCast(u8, @boolToInt(development))); -} -if (this.client_env) |client_env| { - try writer.writeFieldID(5); - try writer.writeValue(client_env); -} -if (this.server_env) |server_env| { - try writer.writeFieldID(6); - try writer.writeValue(server_env); -} -if (this.client_css_in_js) |client_css_in_js| { - try writer.writeFieldID(7); - try writer.writeEnum(client_css_in_js); -} -try writer.endMessage(); -} - -}; - -pub const LoadedFramework = struct { -/// entry_point -entry_point: []const u8, + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + if (this.prefix) |prefix| { + try writer.writeFieldID(1); + try writer.writeValue(prefix); + } + if (this.defaults) |defaults| { + try writer.writeFieldID(2); + try writer.writeValue(defaults); + } + try writer.endMessage(); + } + }; -/// package -package: []const u8, + pub const LoadedEnvConfig = struct { + /// dotenv + dotenv: DotEnvBehavior, -/// development -development: bool = false, + /// defaults + defaults: StringMap, -/// client -client: bool = false, + /// prefix + prefix: []const u8, -/// env -env: LoadedEnvConfig, + pub fn decode(reader: anytype) anyerror!LoadedEnvConfig { + var this = std.mem.zeroes(LoadedEnvConfig); -/// client_css_in_js -client_css_in_js: CssInJsBehavior, + this.dotenv = try reader.readValue(DotEnvBehavior); + this.defaults = try reader.readValue(StringMap); + this.prefix = try reader.readValue([]const u8); + return this; + } + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + try writer.writeEnum(this.dotenv); + try writer.writeValue(this.defaults); + try writer.writeValue(this.prefix); + } + }; -pub fn decode(reader: anytype) anyerror!LoadedFramework { - var this = std.mem.zeroes(LoadedFramework); + pub const FrameworkConfig = struct { + /// package + package: ?[]const u8 = null, - this.entry_point = try reader.readValue([]const u8); - this.package = try reader.readValue([]const u8); - this.development = try reader.readValue(bool); - this.client = try reader.readValue(bool); - this.env = try reader.readValue(LoadedEnvConfig); - this.client_css_in_js = try reader.readValue(CssInJsBehavior); - return this; -} + /// client + client: ?[]const u8 = null, -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { - try writer.writeValue(this.entry_point); - try writer.writeValue(this.package); - try writer.writeInt(@intCast(u8, @boolToInt(this.development))); - try writer.writeInt(@intCast(u8, @boolToInt(this.client))); - try writer.writeValue(this.env); - try writer.writeEnum(this.client_css_in_js); -} + /// server + server: ?[]const u8 = null, -}; + /// development + development: ?bool = null, -pub const LoadedRouteConfig = struct { -/// dir -dir: []const u8, + /// client_env + client_env: ?EnvConfig = null, -/// extensions -extensions: []const []const u8, + /// server_env + server_env: ?EnvConfig = null, -/// static_dir -static_dir: []const u8, + /// client_css_in_js + client_css_in_js: ?CssInJsBehavior = null, -/// asset_prefix -asset_prefix: []const u8, + pub fn decode(reader: anytype) anyerror!FrameworkConfig { + var this = std.mem.zeroes(FrameworkConfig); + while (true) { + switch (try reader.readByte()) { + 0 => { + return this; + }, -pub fn decode(reader: anytype) anyerror!LoadedRouteConfig { - var this = std.mem.zeroes(LoadedRouteConfig); + 1 => { + this.package = try reader.readValue([]const u8); + }, + 2 => { + this.client = try reader.readValue([]const u8); + }, + 3 => { + this.server = try reader.readValue([]const u8); + }, + 4 => { + this.development = try reader.readValue(bool); + }, + 5 => { + this.client_env = try reader.readValue(EnvConfig); + }, + 6 => { + this.server_env = try reader.readValue(EnvConfig); + }, + 7 => { + this.client_css_in_js = try reader.readValue(CssInJsBehavior); + }, + else => { + return error.InvalidMessage; + }, + } + } + unreachable; + } - this.dir = try reader.readValue([]const u8); - this.extensions = try reader.readArray([]const u8); - this.static_dir = try reader.readValue([]const u8); - this.asset_prefix = try reader.readValue([]const u8); - return this; -} + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + if (this.package) |package| { + try writer.writeFieldID(1); + try writer.writeValue(package); + } + if (this.client) |client| { + try writer.writeFieldID(2); + try writer.writeValue(client); + } + if (this.server) |server| { + try writer.writeFieldID(3); + try writer.writeValue(server); + } + if (this.development) |development| { + try writer.writeFieldID(4); + try writer.writeInt(@intCast(u8, @boolToInt(development))); + } + if (this.client_env) |client_env| { + try writer.writeFieldID(5); + try writer.writeValue(client_env); + } + if (this.server_env) |server_env| { + try writer.writeFieldID(6); + try writer.writeValue(server_env); + } + if (this.client_css_in_js) |client_css_in_js| { + try writer.writeFieldID(7); + try writer.writeEnum(client_css_in_js); + } + try writer.endMessage(); + } + }; -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { - try writer.writeValue(this.dir); - try writer.writeArray([]const u8, this.extensions); - try writer.writeValue(this.static_dir); - try writer.writeValue(this.asset_prefix); -} + pub const LoadedFramework = struct { + /// entry_point + entry_point: []const u8, -}; + /// package + package: []const u8, -pub const RouteConfig = struct { -/// dir -dir: []const []const u8, - -/// extensions -extensions: []const []const u8, - -/// static_dir -static_dir: ?[]const u8 = null, - -/// asset_prefix -asset_prefix: ?[]const u8 = null, - - -pub fn decode(reader: anytype) anyerror!RouteConfig { - var this = std.mem.zeroes(RouteConfig); - - while(true) { - switch (try reader.readByte()) { - 0 => { return this; }, - - 1 => { - this.dir = try reader.readArray([]const u8); -}, - 2 => { - this.extensions = try reader.readArray([]const u8); -}, - 3 => { - this.static_dir = try reader.readValue([]const u8); -}, - 4 => { - this.asset_prefix = try reader.readValue([]const u8); -}, - else => { - return error.InvalidMessage; - }, - } - } -unreachable; -} + /// development + development: bool = false, -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { -if (this.dir) |dir| { - try writer.writeFieldID(1); - try writer.writeArray([]const u8, dir); -} -if (this.extensions) |extensions| { - try writer.writeFieldID(2); - try writer.writeArray([]const u8, extensions); -} -if (this.static_dir) |static_dir| { - try writer.writeFieldID(3); - try writer.writeValue(static_dir); -} -if (this.asset_prefix) |asset_prefix| { - try writer.writeFieldID(4); - try writer.writeValue(asset_prefix); -} -try writer.endMessage(); -} + /// client + client: bool = false, -}; + /// env + env: LoadedEnvConfig, -pub const TransformOptions = struct { -/// jsx -jsx: ?Jsx = null, + /// client_css_in_js + client_css_in_js: CssInJsBehavior, -/// tsconfig_override -tsconfig_override: ?[]const u8 = null, + pub fn decode(reader: anytype) anyerror!LoadedFramework { + var this = std.mem.zeroes(LoadedFramework); -/// resolve -resolve: ?ResolveMode = null, + this.entry_point = try reader.readValue([]const u8); + this.package = try reader.readValue([]const u8); + this.development = try reader.readValue(bool); + this.client = try reader.readValue(bool); + this.env = try reader.readValue(LoadedEnvConfig); + this.client_css_in_js = try reader.readValue(CssInJsBehavior); + return this; + } -/// origin -origin: ?[]const u8 = null, - -/// absolute_working_dir -absolute_working_dir: ?[]const u8 = null, - -/// define -define: ?StringMap = null, - -/// preserve_symlinks -preserve_symlinks: ?bool = null, - -/// entry_points -entry_points: []const []const u8, - -/// write -write: ?bool = null, - -/// inject -inject: []const []const u8, - -/// output_dir -output_dir: ?[]const u8 = null, - -/// external -external: []const []const u8, - -/// loaders -loaders: ?LoaderMap = null, - -/// main_fields -main_fields: []const []const u8, - -/// platform -platform: ?Platform = null, - -/// serve -serve: ?bool = null, - -/// extension_order -extension_order: []const []const u8, - -/// generate_node_module_bundle -generate_node_module_bundle: ?bool = null, - -/// node_modules_bundle_path -node_modules_bundle_path: ?[]const u8 = null, - -/// node_modules_bundle_path_server -node_modules_bundle_path_server: ?[]const u8 = null, - -/// framework -framework: ?FrameworkConfig = null, - -/// router -router: ?RouteConfig = null, - -/// no_summary -no_summary: ?bool = null, - - -pub fn decode(reader: anytype) anyerror!TransformOptions { - var this = std.mem.zeroes(TransformOptions); - - while(true) { - switch (try reader.readByte()) { - 0 => { return this; }, - - 1 => { - this.jsx = try reader.readValue(Jsx); -}, - 2 => { - this.tsconfig_override = try reader.readValue([]const u8); -}, - 3 => { - this.resolve = try reader.readValue(ResolveMode); -}, - 4 => { - this.origin = try reader.readValue([]const u8); -}, - 5 => { - this.absolute_working_dir = try reader.readValue([]const u8); -}, - 6 => { - this.define = try reader.readValue(StringMap); -}, - 7 => { - this.preserve_symlinks = try reader.readValue(bool); -}, - 8 => { - this.entry_points = try reader.readArray([]const u8); -}, - 9 => { - this.write = try reader.readValue(bool); -}, - 10 => { - this.inject = try reader.readArray([]const u8); -}, - 11 => { - this.output_dir = try reader.readValue([]const u8); -}, - 12 => { - this.external = try reader.readArray([]const u8); -}, - 13 => { - this.loaders = try reader.readValue(LoaderMap); -}, - 14 => { - this.main_fields = try reader.readArray([]const u8); -}, - 15 => { - this.platform = try reader.readValue(Platform); -}, - 16 => { - this.serve = try reader.readValue(bool); -}, - 17 => { - this.extension_order = try reader.readArray([]const u8); -}, - 18 => { - this.generate_node_module_bundle = try reader.readValue(bool); -}, - 19 => { - this.node_modules_bundle_path = try reader.readValue([]const u8); -}, - 20 => { - this.node_modules_bundle_path_server = try reader.readValue([]const u8); -}, - 21 => { - this.framework = try reader.readValue(FrameworkConfig); -}, - 22 => { - this.router = try reader.readValue(RouteConfig); -}, - 23 => { - this.no_summary = try reader.readValue(bool); -}, - else => { - return error.InvalidMessage; - }, - } - } -unreachable; -} + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + try writer.writeValue(this.entry_point); + try writer.writeValue(this.package); + try writer.writeInt(@intCast(u8, @boolToInt(this.development))); + try writer.writeInt(@intCast(u8, @boolToInt(this.client))); + try writer.writeValue(this.env); + try writer.writeEnum(this.client_css_in_js); + } + }; -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { -if (this.jsx) |jsx| { - try writer.writeFieldID(1); - try writer.writeValue(jsx); -} -if (this.tsconfig_override) |tsconfig_override| { - try writer.writeFieldID(2); - try writer.writeValue(tsconfig_override); -} -if (this.resolve) |resolve| { - try writer.writeFieldID(3); - try writer.writeEnum(resolve); -} -if (this.origin) |origin| { - try writer.writeFieldID(4); - try writer.writeValue(origin); -} -if (this.absolute_working_dir) |absolute_working_dir| { - try writer.writeFieldID(5); - try writer.writeValue(absolute_working_dir); -} -if (this.define) |define| { - try writer.writeFieldID(6); - try writer.writeValue(define); -} -if (this.preserve_symlinks) |preserve_symlinks| { - try writer.writeFieldID(7); - try writer.writeInt(@intCast(u8, @boolToInt(preserve_symlinks))); -} -if (this.entry_points) |entry_points| { - try writer.writeFieldID(8); - try writer.writeArray([]const u8, entry_points); -} -if (this.write) |write| { - try writer.writeFieldID(9); - try writer.writeInt(@intCast(u8, @boolToInt(write))); -} -if (this.inject) |inject| { - try writer.writeFieldID(10); - try writer.writeArray([]const u8, inject); -} -if (this.output_dir) |output_dir| { - try writer.writeFieldID(11); - try writer.writeValue(output_dir); -} -if (this.external) |external| { - try writer.writeFieldID(12); - try writer.writeArray([]const u8, external); -} -if (this.loaders) |loaders| { - try writer.writeFieldID(13); - try writer.writeValue(loaders); -} -if (this.main_fields) |main_fields| { - try writer.writeFieldID(14); - try writer.writeArray([]const u8, main_fields); -} -if (this.platform) |platform| { - try writer.writeFieldID(15); - try writer.writeEnum(platform); -} -if (this.serve) |serve| { - try writer.writeFieldID(16); - try writer.writeInt(@intCast(u8, @boolToInt(serve))); -} -if (this.extension_order) |extension_order| { - try writer.writeFieldID(17); - try writer.writeArray([]const u8, extension_order); -} -if (this.generate_node_module_bundle) |generate_node_module_bundle| { - try writer.writeFieldID(18); - try writer.writeInt(@intCast(u8, @boolToInt(generate_node_module_bundle))); -} -if (this.node_modules_bundle_path) |node_modules_bundle_path| { - try writer.writeFieldID(19); - try writer.writeValue(node_modules_bundle_path); -} -if (this.node_modules_bundle_path_server) |node_modules_bundle_path_server| { - try writer.writeFieldID(20); - try writer.writeValue(node_modules_bundle_path_server); -} -if (this.framework) |framework| { - try writer.writeFieldID(21); - try writer.writeValue(framework); -} -if (this.router) |router| { - try writer.writeFieldID(22); - try writer.writeValue(router); -} -if (this.no_summary) |no_summary| { - try writer.writeFieldID(23); - try writer.writeInt(@intCast(u8, @boolToInt(no_summary))); -} -try writer.endMessage(); -} + pub const LoadedRouteConfig = struct { + /// dir + dir: []const u8, -}; + /// extensions + extensions: []const []const u8, -pub const FileHandle = struct { -/// path -path: []const u8, + /// static_dir + static_dir: []const u8, -/// size -size: u32 = 0, + /// asset_prefix + asset_prefix: []const u8, -/// fd -fd: u32 = 0, + pub fn decode(reader: anytype) anyerror!LoadedRouteConfig { + var this = std.mem.zeroes(LoadedRouteConfig); + this.dir = try reader.readValue([]const u8); + this.extensions = try reader.readArray([]const u8); + this.static_dir = try reader.readValue([]const u8); + this.asset_prefix = try reader.readValue([]const u8); + return this; + } -pub fn decode(reader: anytype) anyerror!FileHandle { - var this = std.mem.zeroes(FileHandle); + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + try writer.writeValue(this.dir); + try writer.writeArray([]const u8, this.extensions); + try writer.writeValue(this.static_dir); + try writer.writeValue(this.asset_prefix); + } + }; - this.path = try reader.readValue([]const u8); - this.size = try reader.readValue(u32); - this.fd = try reader.readValue(u32); - return this; -} + pub const RouteConfig = struct { + /// dir + dir: []const []const u8, -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { - try writer.writeValue(this.path); - try writer.writeInt(this.size); - try writer.writeInt(this.fd); -} + /// extensions + extensions: []const []const u8, -}; + /// static_dir + static_dir: ?[]const u8 = null, -pub const Transform = struct { -/// handle -handle: ?FileHandle = null, - -/// path -path: ?[]const u8 = null, - -/// contents -contents: []const u8, - -/// loader -loader: ?Loader = null, - -/// options -options: ?TransformOptions = null, - - -pub fn decode(reader: anytype) anyerror!Transform { - var this = std.mem.zeroes(Transform); - - while(true) { - switch (try reader.readByte()) { - 0 => { return this; }, - - 1 => { - this.handle = try reader.readValue(FileHandle); -}, - 2 => { - this.path = try reader.readValue([]const u8); -}, - 3 => { - this.contents = try reader.readArray(u8); -}, - 4 => { - this.loader = try reader.readValue(Loader); -}, - 5 => { - this.options = try reader.readValue(TransformOptions); -}, - else => { - return error.InvalidMessage; - }, - } - } -unreachable; -} + /// asset_prefix + asset_prefix: ?[]const u8 = null, -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { -if (this.handle) |handle| { - try writer.writeFieldID(1); - try writer.writeValue(handle); -} -if (this.path) |path| { - try writer.writeFieldID(2); - try writer.writeValue(path); -} -if (this.contents) |contents| { - try writer.writeFieldID(3); - try writer.writeArray(u8, contents); -} -if (this.loader) |loader| { - try writer.writeFieldID(4); - try writer.writeEnum(loader); -} -if (this.options) |options| { - try writer.writeFieldID(5); - try writer.writeValue(options); -} -try writer.endMessage(); -} + pub fn decode(reader: anytype) anyerror!RouteConfig { + var this = std.mem.zeroes(RouteConfig); -}; + while (true) { + switch (try reader.readByte()) { + 0 => { + return this; + }, -pub const TransformResponseStatus = enum(u32) { + 1 => { + this.dir = try reader.readArray([]const u8); + }, + 2 => { + this.extensions = try reader.readArray([]const u8); + }, + 3 => { + this.static_dir = try reader.readValue([]const u8); + }, + 4 => { + this.asset_prefix = try reader.readValue([]const u8); + }, + else => { + return error.InvalidMessage; + }, + } + } + unreachable; + } -_none, - /// success - success, + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + if (this.dir) |dir| { + try writer.writeFieldID(1); + try writer.writeArray([]const u8, dir); + } + if (this.extensions) |extensions| { + try writer.writeFieldID(2); + try writer.writeArray([]const u8, extensions); + } + if (this.static_dir) |static_dir| { + try writer.writeFieldID(3); + try writer.writeValue(static_dir); + } + if (this.asset_prefix) |asset_prefix| { + try writer.writeFieldID(4); + try writer.writeValue(asset_prefix); + } + try writer.endMessage(); + } + }; - /// fail - fail, + pub const TransformOptions = struct { + /// jsx + jsx: ?Jsx = null, -_, + /// tsconfig_override + tsconfig_override: ?[]const u8 = null, - pub fn jsonStringify(self: *const @This(), opts: anytype, o: anytype) !void { - return try std.json.stringify(@tagName(self), opts, o); - } + /// resolve + resolve: ?ResolveMode = null, - -}; + /// origin + origin: ?[]const u8 = null, -pub const OutputFile = struct { -/// data -data: []const u8, + /// absolute_working_dir + absolute_working_dir: ?[]const u8 = null, -/// path -path: []const u8, + /// define + define: ?StringMap = null, + /// preserve_symlinks + preserve_symlinks: ?bool = null, -pub fn decode(reader: anytype) anyerror!OutputFile { - var this = std.mem.zeroes(OutputFile); + /// entry_points + entry_points: []const []const u8, - this.data = try reader.readArray(u8); - this.path = try reader.readValue([]const u8); - return this; -} + /// write + write: ?bool = null, -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { - try writer.writeArray(u8, this.data); - try writer.writeValue(this.path); -} + /// inject + inject: []const []const u8, -}; + /// output_dir + output_dir: ?[]const u8 = null, -pub const TransformResponse = struct { -/// status -status: TransformResponseStatus, + /// external + external: []const []const u8, -/// files -files: []const OutputFile, + /// loaders + loaders: ?LoaderMap = null, -/// errors -errors: []const Message, + /// main_fields + main_fields: []const []const u8, + /// platform + platform: ?Platform = null, -pub fn decode(reader: anytype) anyerror!TransformResponse { - var this = std.mem.zeroes(TransformResponse); + /// serve + serve: ?bool = null, - this.status = try reader.readValue(TransformResponseStatus); - this.files = try reader.readArray(OutputFile); - this.errors = try reader.readArray(Message); - return this; -} + /// extension_order + extension_order: []const []const u8, -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { - try writer.writeEnum(this.status); - try writer.writeArray(OutputFile, this.files); - try writer.writeArray(Message, this.errors); -} + /// generate_node_module_bundle + generate_node_module_bundle: ?bool = null, -}; + /// node_modules_bundle_path + node_modules_bundle_path: ?[]const u8 = null, -pub const MessageKind = enum(u32) { + /// node_modules_bundle_path_server + node_modules_bundle_path_server: ?[]const u8 = null, -_none, - /// err - err, + /// framework + framework: ?FrameworkConfig = null, - /// warn - warn, + /// router + router: ?RouteConfig = null, - /// note - note, + /// no_summary + no_summary: ?bool = null, - /// debug - debug, + pub fn decode(reader: anytype) anyerror!TransformOptions { + var this = std.mem.zeroes(TransformOptions); -_, + while (true) { + switch (try reader.readByte()) { + 0 => { + return this; + }, - pub fn jsonStringify(self: *const @This(), opts: anytype, o: anytype) !void { - return try std.json.stringify(@tagName(self), opts, o); + 1 => { + this.jsx = try reader.readValue(Jsx); + }, + 2 => { + this.tsconfig_override = try reader.readValue([]const u8); + }, + 3 => { + this.resolve = try reader.readValue(ResolveMode); + }, + 4 => { + this.origin = try reader.readValue([]const u8); + }, + 5 => { + this.absolute_working_dir = try reader.readValue([]const u8); + }, + 6 => { + this.define = try reader.readValue(StringMap); + }, + 7 => { + this.preserve_symlinks = try reader.readValue(bool); + }, + 8 => { + this.entry_points = try reader.readArray([]const u8); + }, + 9 => { + this.write = try reader.readValue(bool); + }, + 10 => { + this.inject = try reader.readArray([]const u8); + }, + 11 => { + this.output_dir = try reader.readValue([]const u8); + }, + 12 => { + this.external = try reader.readArray([]const u8); + }, + 13 => { + this.loaders = try reader.readValue(LoaderMap); + }, + 14 => { + this.main_fields = try reader.readArray([]const u8); + }, + 15 => { + this.platform = try reader.readValue(Platform); + }, + 16 => { + this.serve = try reader.readValue(bool); + }, + 17 => { + this.extension_order = try reader.readArray([]const u8); + }, + 18 => { + this.generate_node_module_bundle = try reader.readValue(bool); + }, + 19 => { + this.node_modules_bundle_path = try reader.readValue([]const u8); + }, + 20 => { + this.node_modules_bundle_path_server = try reader.readValue([]const u8); + }, + 21 => { + this.framework = try reader.readValue(FrameworkConfig); + }, + 22 => { + this.router = try reader.readValue(RouteConfig); + }, + 23 => { + this.no_summary = try reader.readValue(bool); + }, + else => { + return error.InvalidMessage; + }, } + } + unreachable; + } - -}; - -pub const Location = struct { -/// file -file: []const u8, - -/// namespace -namespace: []const u8, + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + if (this.jsx) |jsx| { + try writer.writeFieldID(1); + try writer.writeValue(jsx); + } + if (this.tsconfig_override) |tsconfig_override| { + try writer.writeFieldID(2); + try writer.writeValue(tsconfig_override); + } + if (this.resolve) |resolve| { + try writer.writeFieldID(3); + try writer.writeEnum(resolve); + } + if (this.origin) |origin| { + try writer.writeFieldID(4); + try writer.writeValue(origin); + } + if (this.absolute_working_dir) |absolute_working_dir| { + try writer.writeFieldID(5); + try writer.writeValue(absolute_working_dir); + } + if (this.define) |define| { + try writer.writeFieldID(6); + try writer.writeValue(define); + } + if (this.preserve_symlinks) |preserve_symlinks| { + try writer.writeFieldID(7); + try writer.writeInt(@intCast(u8, @boolToInt(preserve_symlinks))); + } + if (this.entry_points) |entry_points| { + try writer.writeFieldID(8); + try writer.writeArray([]const u8, entry_points); + } + if (this.write) |write| { + try writer.writeFieldID(9); + try writer.writeInt(@intCast(u8, @boolToInt(write))); + } + if (this.inject) |inject| { + try writer.writeFieldID(10); + try writer.writeArray([]const u8, inject); + } + if (this.output_dir) |output_dir| { + try writer.writeFieldID(11); + try writer.writeValue(output_dir); + } + if (this.external) |external| { + try writer.writeFieldID(12); + try writer.writeArray([]const u8, external); + } + if (this.loaders) |loaders| { + try writer.writeFieldID(13); + try writer.writeValue(loaders); + } + if (this.main_fields) |main_fields| { + try writer.writeFieldID(14); + try writer.writeArray([]const u8, main_fields); + } + if (this.platform) |platform| { + try writer.writeFieldID(15); + try writer.writeEnum(platform); + } + if (this.serve) |serve| { + try writer.writeFieldID(16); + try writer.writeInt(@intCast(u8, @boolToInt(serve))); + } + if (this.extension_order) |extension_order| { + try writer.writeFieldID(17); + try writer.writeArray([]const u8, extension_order); + } + if (this.generate_node_module_bundle) |generate_node_module_bundle| { + try writer.writeFieldID(18); + try writer.writeInt(@intCast(u8, @boolToInt(generate_node_module_bundle))); + } + if (this.node_modules_bundle_path) |node_modules_bundle_path| { + try writer.writeFieldID(19); + try writer.writeValue(node_modules_bundle_path); + } + if (this.node_modules_bundle_path_server) |node_modules_bundle_path_server| { + try writer.writeFieldID(20); + try writer.writeValue(node_modules_bundle_path_server); + } + if (this.framework) |framework| { + try writer.writeFieldID(21); + try writer.writeValue(framework); + } + if (this.router) |router| { + try writer.writeFieldID(22); + try writer.writeValue(router); + } + if (this.no_summary) |no_summary| { + try writer.writeFieldID(23); + try writer.writeInt(@intCast(u8, @boolToInt(no_summary))); + } + try writer.endMessage(); + } + }; -/// line -line: i32 = 0, + pub const FileHandle = struct { + /// path + path: []const u8, -/// column -column: i32 = 0, + /// size + size: u32 = 0, -/// line_text -line_text: []const u8, + /// fd + fd: u32 = 0, -/// suggestion -suggestion: []const u8, + pub fn decode(reader: anytype) anyerror!FileHandle { + var this = std.mem.zeroes(FileHandle); -/// offset -offset: u32 = 0, + this.path = try reader.readValue([]const u8); + this.size = try reader.readValue(u32); + this.fd = try reader.readValue(u32); + return this; + } + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + try writer.writeValue(this.path); + try writer.writeInt(this.size); + try writer.writeInt(this.fd); + } + }; -pub fn decode(reader: anytype) anyerror!Location { - var this = std.mem.zeroes(Location); + pub const Transform = struct { + /// handle + handle: ?FileHandle = null, - this.file = try reader.readValue([]const u8); - this.namespace = try reader.readValue([]const u8); - this.line = try reader.readValue(i32); - this.column = try reader.readValue(i32); - this.line_text = try reader.readValue([]const u8); - this.suggestion = try reader.readValue([]const u8); - this.offset = try reader.readValue(u32); - return this; -} + /// path + path: ?[]const u8 = null, -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { - try writer.writeValue(this.file); - try writer.writeValue(this.namespace); - try writer.writeInt(this.line); - try writer.writeInt(this.column); - try writer.writeValue(this.line_text); - try writer.writeValue(this.suggestion); - try writer.writeInt(this.offset); -} + /// contents + contents: []const u8, -}; + /// loader + loader: ?Loader = null, -pub const MessageData = struct { -/// text -text: ?[]const u8 = null, - -/// location -location: ?Location = null, - - -pub fn decode(reader: anytype) anyerror!MessageData { - var this = std.mem.zeroes(MessageData); - - while(true) { - switch (try reader.readByte()) { - 0 => { return this; }, - - 1 => { - this.text = try reader.readValue([]const u8); -}, - 2 => { - this.location = try reader.readValue(Location); -}, - else => { - return error.InvalidMessage; - }, - } - } -unreachable; -} + /// options + options: ?TransformOptions = null, -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { -if (this.text) |text| { - try writer.writeFieldID(1); - try writer.writeValue(text); -} -if (this.location) |location| { - try writer.writeFieldID(2); - try writer.writeValue(location); -} -try writer.endMessage(); -} + pub fn decode(reader: anytype) anyerror!Transform { + var this = std.mem.zeroes(Transform); -}; + while (true) { + switch (try reader.readByte()) { + 0 => { + return this; + }, -pub const Message = struct { -/// kind -kind: MessageKind, + 1 => { + this.handle = try reader.readValue(FileHandle); + }, + 2 => { + this.path = try reader.readValue([]const u8); + }, + 3 => { + this.contents = try reader.readArray(u8); + }, + 4 => { + this.loader = try reader.readValue(Loader); + }, + 5 => { + this.options = try reader.readValue(TransformOptions); + }, + else => { + return error.InvalidMessage; + }, + } + } + unreachable; + } -/// data -data: MessageData, + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + if (this.handle) |handle| { + try writer.writeFieldID(1); + try writer.writeValue(handle); + } + if (this.path) |path| { + try writer.writeFieldID(2); + try writer.writeValue(path); + } + if (this.contents) |contents| { + try writer.writeFieldID(3); + try writer.writeArray(u8, contents); + } + if (this.loader) |loader| { + try writer.writeFieldID(4); + try writer.writeEnum(loader); + } + if (this.options) |options| { + try writer.writeFieldID(5); + try writer.writeValue(options); + } + try writer.endMessage(); + } + }; -/// notes -notes: []const MessageData, + pub const TransformResponseStatus = enum(u32) { + _none, + /// success + success, + /// fail + fail, -pub fn decode(reader: anytype) anyerror!Message { - var this = std.mem.zeroes(Message); + _, - this.kind = try reader.readValue(MessageKind); - this.data = try reader.readValue(MessageData); - this.notes = try reader.readArray(MessageData); - return this; -} + pub fn jsonStringify(self: *const @This(), opts: anytype, o: anytype) !void { + return try std.json.stringify(@tagName(self), opts, o); + } + }; -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { - try writer.writeEnum(this.kind); - try writer.writeValue(this.data); - try writer.writeArray(MessageData, this.notes); -} + pub const OutputFile = struct { + /// data + data: []const u8, -}; + /// path + path: []const u8, -pub const Log = struct { -/// warnings -warnings: u32 = 0, + pub fn decode(reader: anytype) anyerror!OutputFile { + var this = std.mem.zeroes(OutputFile); -/// errors -errors: u32 = 0, + this.data = try reader.readArray(u8); + this.path = try reader.readValue([]const u8); + return this; + } -/// msgs -msgs: []const Message, + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + try writer.writeArray(u8, this.data); + try writer.writeValue(this.path); + } + }; + pub const TransformResponse = struct { + /// status + status: TransformResponseStatus, -pub fn decode(reader: anytype) anyerror!Log { - var this = std.mem.zeroes(Log); + /// files + files: []const OutputFile, - this.warnings = try reader.readValue(u32); - this.errors = try reader.readValue(u32); - this.msgs = try reader.readArray(Message); - return this; -} + /// errors + errors: []const Message, -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { - try writer.writeInt(this.warnings); - try writer.writeInt(this.errors); - try writer.writeArray(Message, this.msgs); -} + pub fn decode(reader: anytype) anyerror!TransformResponse { + var this = std.mem.zeroes(TransformResponse); -}; + this.status = try reader.readValue(TransformResponseStatus); + this.files = try reader.readArray(OutputFile); + this.errors = try reader.readArray(Message); + return this; + } -pub const Reloader = enum(u8) { + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + try writer.writeEnum(this.status); + try writer.writeArray(OutputFile, this.files); + try writer.writeArray(Message, this.errors); + } + }; -_none, - /// disable - disable, + pub const MessageKind = enum(u32) { + _none, + /// err + err, - /// live - live, + /// warn + warn, - /// fast_refresh - fast_refresh, + /// note + note, -_, + /// debug + debug, - pub fn jsonStringify(self: *const @This(), opts: anytype, o: anytype) !void { - return try std.json.stringify(@tagName(self), opts, o); - } + _, - -}; + pub fn jsonStringify(self: *const @This(), opts: anytype, o: anytype) !void { + return try std.json.stringify(@tagName(self), opts, o); + } + }; -pub const WebsocketMessageKind = enum(u8) { + pub const Location = struct { + /// file + file: []const u8, -_none, - /// welcome - welcome, + /// namespace + namespace: []const u8, - /// file_change_notification - file_change_notification, + /// line + line: i32 = 0, - /// build_success - build_success, + /// column + column: i32 = 0, - /// build_fail - build_fail, + /// line_text + line_text: []const u8, - /// manifest_success - manifest_success, + /// suggestion + suggestion: []const u8, - /// manifest_fail - manifest_fail, + /// offset + offset: u32 = 0, -_, + pub fn decode(reader: anytype) anyerror!Location { + var this = std.mem.zeroes(Location); - pub fn jsonStringify(self: *const @This(), opts: anytype, o: anytype) !void { - return try std.json.stringify(@tagName(self), opts, o); - } + this.file = try reader.readValue([]const u8); + this.namespace = try reader.readValue([]const u8); + this.line = try reader.readValue(i32); + this.column = try reader.readValue(i32); + this.line_text = try reader.readValue([]const u8); + this.suggestion = try reader.readValue([]const u8); + this.offset = try reader.readValue(u32); + return this; + } - -}; + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + try writer.writeValue(this.file); + try writer.writeValue(this.namespace); + try writer.writeInt(this.line); + try writer.writeInt(this.column); + try writer.writeValue(this.line_text); + try writer.writeValue(this.suggestion); + try writer.writeInt(this.offset); + } + }; -pub const WebsocketCommandKind = enum(u8) { + pub const MessageData = struct { + /// text + text: ?[]const u8 = null, -_none, - /// build - build, + /// location + location: ?Location = null, - /// manifest - manifest, + pub fn decode(reader: anytype) anyerror!MessageData { + var this = std.mem.zeroes(MessageData); -_, + while (true) { + switch (try reader.readByte()) { + 0 => { + return this; + }, - pub fn jsonStringify(self: *const @This(), opts: anytype, o: anytype) !void { - return try std.json.stringify(@tagName(self), opts, o); + 1 => { + this.text = try reader.readValue([]const u8); + }, + 2 => { + this.location = try reader.readValue(Location); + }, + else => { + return error.InvalidMessage; + }, } + } + unreachable; + } - -}; - -pub const WebsocketMessage = struct { -/// timestamp -timestamp: u32 = 0, + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + if (this.text) |text| { + try writer.writeFieldID(1); + try writer.writeValue(text); + } + if (this.location) |location| { + try writer.writeFieldID(2); + try writer.writeValue(location); + } + try writer.endMessage(); + } + }; -/// kind -kind: WebsocketMessageKind, + pub const Message = struct { + /// kind + kind: MessageKind, + /// data + data: MessageData, -pub fn decode(reader: anytype) anyerror!WebsocketMessage { - var this = std.mem.zeroes(WebsocketMessage); + /// notes + notes: []const MessageData, - this.timestamp = try reader.readValue(u32); - this.kind = try reader.readValue(WebsocketMessageKind); - return this; -} + pub fn decode(reader: anytype) anyerror!Message { + var this = std.mem.zeroes(Message); -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { - try writer.writeInt(this.timestamp); - try writer.writeEnum(this.kind); -} + this.kind = try reader.readValue(MessageKind); + this.data = try reader.readValue(MessageData); + this.notes = try reader.readArray(MessageData); + return this; + } -}; + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + try writer.writeEnum(this.kind); + try writer.writeValue(this.data); + try writer.writeArray(MessageData, this.notes); + } + }; -pub const WebsocketMessageWelcome = struct { -/// epoch -epoch: u32 = 0, + pub const Log = struct { + /// warnings + warnings: u32 = 0, -/// javascriptReloader -javascript_reloader: Reloader, + /// errors + errors: u32 = 0, + /// msgs + msgs: []const Message, -pub fn decode(reader: anytype) anyerror!WebsocketMessageWelcome { - var this = std.mem.zeroes(WebsocketMessageWelcome); + pub fn decode(reader: anytype) anyerror!Log { + var this = std.mem.zeroes(Log); - this.epoch = try reader.readValue(u32); - this.javascript_reloader = try reader.readValue(Reloader); - return this; -} + this.warnings = try reader.readValue(u32); + this.errors = try reader.readValue(u32); + this.msgs = try reader.readArray(Message); + return this; + } -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { - try writer.writeInt(this.epoch); - try writer.writeEnum(this.javascript_reloader); -} + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + try writer.writeInt(this.warnings); + try writer.writeInt(this.errors); + try writer.writeArray(Message, this.msgs); + } + }; -}; + pub const Reloader = enum(u8) { + _none, + /// disable + disable, -pub const WebsocketMessageFileChangeNotification = struct { -/// id -id: u32 = 0, + /// live + live, -/// loader -loader: Loader, + /// fast_refresh + fast_refresh, + _, -pub fn decode(reader: anytype) anyerror!WebsocketMessageFileChangeNotification { - var this = std.mem.zeroes(WebsocketMessageFileChangeNotification); + pub fn jsonStringify(self: *const @This(), opts: anytype, o: anytype) !void { + return try std.json.stringify(@tagName(self), opts, o); + } + }; - this.id = try reader.readValue(u32); - this.loader = try reader.readValue(Loader); - return this; -} + pub const WebsocketMessageKind = enum(u8) { + _none, + /// welcome + welcome, -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { - try writer.writeInt(this.id); - try writer.writeEnum(this.loader); -} + /// file_change_notification + file_change_notification, -}; + /// build_success + build_success, -pub const WebsocketCommand = struct { -/// kind -kind: WebsocketCommandKind, + /// build_fail + build_fail, -/// timestamp -timestamp: u32 = 0, + /// manifest_success + manifest_success, + /// manifest_fail + manifest_fail, -pub fn decode(reader: anytype) anyerror!WebsocketCommand { - var this = std.mem.zeroes(WebsocketCommand); + _, - this.kind = try reader.readValue(WebsocketCommandKind); - this.timestamp = try reader.readValue(u32); - return this; -} + pub fn jsonStringify(self: *const @This(), opts: anytype, o: anytype) !void { + return try std.json.stringify(@tagName(self), opts, o); + } + }; -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { - try writer.writeEnum(this.kind); - try writer.writeInt(this.timestamp); -} + pub const WebsocketCommandKind = enum(u8) { + _none, + /// build + build, -}; + /// manifest + manifest, -pub const WebsocketCommandBuild = packed struct { -/// id -id: u32 = 0, + _, + pub fn jsonStringify(self: *const @This(), opts: anytype, o: anytype) !void { + return try std.json.stringify(@tagName(self), opts, o); + } + }; -pub fn decode(reader: anytype) anyerror!WebsocketCommandBuild { - var this = std.mem.zeroes(WebsocketCommandBuild); + pub const WebsocketMessage = struct { + /// timestamp + timestamp: u32 = 0, - this.id = try reader.readValue(u32); - return this; -} + /// kind + kind: WebsocketMessageKind, -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { - try writer.writeInt(this.id); -} + pub fn decode(reader: anytype) anyerror!WebsocketMessage { + var this = std.mem.zeroes(WebsocketMessage); -}; + this.timestamp = try reader.readValue(u32); + this.kind = try reader.readValue(WebsocketMessageKind); + return this; + } -pub const WebsocketCommandManifest = packed struct { -/// id -id: u32 = 0, + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + try writer.writeInt(this.timestamp); + try writer.writeEnum(this.kind); + } + }; + pub const WebsocketMessageWelcome = struct { + /// epoch + epoch: u32 = 0, -pub fn decode(reader: anytype) anyerror!WebsocketCommandManifest { - var this = std.mem.zeroes(WebsocketCommandManifest); + /// javascriptReloader + javascript_reloader: Reloader, - this.id = try reader.readValue(u32); - return this; -} + pub fn decode(reader: anytype) anyerror!WebsocketMessageWelcome { + var this = std.mem.zeroes(WebsocketMessageWelcome); -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { - try writer.writeInt(this.id); -} + this.epoch = try reader.readValue(u32); + this.javascript_reloader = try reader.readValue(Reloader); + return this; + } -}; + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + try writer.writeInt(this.epoch); + try writer.writeEnum(this.javascript_reloader); + } + }; -pub const WebsocketMessageBuildSuccess = struct { -/// id -id: u32 = 0, + pub const WebsocketMessageFileChangeNotification = struct { + /// id + id: u32 = 0, -/// from_timestamp -from_timestamp: u32 = 0, + /// loader + loader: Loader, -/// loader -loader: Loader, + pub fn decode(reader: anytype) anyerror!WebsocketMessageFileChangeNotification { + var this = std.mem.zeroes(WebsocketMessageFileChangeNotification); -/// module_path -module_path: []const u8, + this.id = try reader.readValue(u32); + this.loader = try reader.readValue(Loader); + return this; + } -/// blob_length -blob_length: u32 = 0, + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + try writer.writeInt(this.id); + try writer.writeEnum(this.loader); + } + }; + pub const WebsocketCommand = struct { + /// kind + kind: WebsocketCommandKind, -pub fn decode(reader: anytype) anyerror!WebsocketMessageBuildSuccess { - var this = std.mem.zeroes(WebsocketMessageBuildSuccess); + /// timestamp + timestamp: u32 = 0, - this.id = try reader.readValue(u32); - this.from_timestamp = try reader.readValue(u32); - this.loader = try reader.readValue(Loader); - this.module_path = try reader.readValue([]const u8); - this.blob_length = try reader.readValue(u32); - return this; -} + pub fn decode(reader: anytype) anyerror!WebsocketCommand { + var this = std.mem.zeroes(WebsocketCommand); -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { - try writer.writeInt(this.id); - try writer.writeInt(this.from_timestamp); - try writer.writeEnum(this.loader); - try writer.writeValue(this.module_path); - try writer.writeInt(this.blob_length); -} + this.kind = try reader.readValue(WebsocketCommandKind); + this.timestamp = try reader.readValue(u32); + return this; + } -}; + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + try writer.writeEnum(this.kind); + try writer.writeInt(this.timestamp); + } + }; -pub const WebsocketMessageBuildFailure = struct { -/// id -id: u32 = 0, + pub const WebsocketCommandBuild = packed struct { + /// id + id: u32 = 0, -/// from_timestamp -from_timestamp: u32 = 0, + pub fn decode(reader: anytype) anyerror!WebsocketCommandBuild { + var this = std.mem.zeroes(WebsocketCommandBuild); -/// loader -loader: Loader, + this.id = try reader.readValue(u32); + return this; + } -/// module_path -module_path: []const u8, + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + try writer.writeInt(this.id); + } + }; -/// log -log: Log, + pub const WebsocketCommandManifest = packed struct { + /// id + id: u32 = 0, + pub fn decode(reader: anytype) anyerror!WebsocketCommandManifest { + var this = std.mem.zeroes(WebsocketCommandManifest); -pub fn decode(reader: anytype) anyerror!WebsocketMessageBuildFailure { - var this = std.mem.zeroes(WebsocketMessageBuildFailure); + this.id = try reader.readValue(u32); + return this; + } - this.id = try reader.readValue(u32); - this.from_timestamp = try reader.readValue(u32); - this.loader = try reader.readValue(Loader); - this.module_path = try reader.readValue([]const u8); - this.log = try reader.readValue(Log); - return this; -} + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + try writer.writeInt(this.id); + } + }; -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { - try writer.writeInt(this.id); - try writer.writeInt(this.from_timestamp); - try writer.writeEnum(this.loader); - try writer.writeValue(this.module_path); - try writer.writeValue(this.log); -} + pub const WebsocketMessageBuildSuccess = struct { + /// id + id: u32 = 0, -}; + /// from_timestamp + from_timestamp: u32 = 0, -pub const DependencyManifest = struct { -/// ids -ids: []const u32, + /// loader + loader: Loader, + /// module_path + module_path: []const u8, -pub fn decode(reader: anytype) anyerror!DependencyManifest { - var this = std.mem.zeroes(DependencyManifest); + /// blob_length + blob_length: u32 = 0, - this.ids = try reader.readArray(u32); - return this; -} + pub fn decode(reader: anytype) anyerror!WebsocketMessageBuildSuccess { + var this = std.mem.zeroes(WebsocketMessageBuildSuccess); -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { - try writer.writeArray(u32, this.ids); -} + this.id = try reader.readValue(u32); + this.from_timestamp = try reader.readValue(u32); + this.loader = try reader.readValue(Loader); + this.module_path = try reader.readValue([]const u8); + this.blob_length = try reader.readValue(u32); + return this; + } -}; + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + try writer.writeInt(this.id); + try writer.writeInt(this.from_timestamp); + try writer.writeEnum(this.loader); + try writer.writeValue(this.module_path); + try writer.writeInt(this.blob_length); + } + }; -pub const FileList = struct { -/// ptrs -ptrs: []const StringPointer, + pub const WebsocketMessageBuildFailure = struct { + /// id + id: u32 = 0, -/// files -files: []const u8, + /// from_timestamp + from_timestamp: u32 = 0, + /// loader + loader: Loader, -pub fn decode(reader: anytype) anyerror!FileList { - var this = std.mem.zeroes(FileList); + /// module_path + module_path: []const u8, - this.ptrs = try reader.readArray(StringPointer); - this.files = try reader.readValue([]const u8); - return this; -} + /// log + log: Log, -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { - try writer.writeArray(StringPointer, this.ptrs); - try writer.writeValue(this.files); -} + pub fn decode(reader: anytype) anyerror!WebsocketMessageBuildFailure { + var this = std.mem.zeroes(WebsocketMessageBuildFailure); -}; + this.id = try reader.readValue(u32); + this.from_timestamp = try reader.readValue(u32); + this.loader = try reader.readValue(Loader); + this.module_path = try reader.readValue([]const u8); + this.log = try reader.readValue(Log); + return this; + } -pub const WebsocketMessageResolveIDs = struct { -/// id -id: []const u32, + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + try writer.writeInt(this.id); + try writer.writeInt(this.from_timestamp); + try writer.writeEnum(this.loader); + try writer.writeValue(this.module_path); + try writer.writeValue(this.log); + } + }; -/// list -list: FileList, + pub const DependencyManifest = struct { + /// ids + ids: []const u32, + pub fn decode(reader: anytype) anyerror!DependencyManifest { + var this = std.mem.zeroes(DependencyManifest); -pub fn decode(reader: anytype) anyerror!WebsocketMessageResolveIDs { - var this = std.mem.zeroes(WebsocketMessageResolveIDs); + this.ids = try reader.readArray(u32); + return this; + } - this.id = try reader.readArray(u32); - this.list = try reader.readValue(FileList); - return this; -} + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + try writer.writeArray(u32, this.ids); + } + }; -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { - try writer.writeArray(u32, this.id); - try writer.writeValue(this.list); -} + pub const FileList = struct { + /// ptrs + ptrs: []const StringPointer, -}; + /// files + files: []const u8, -pub const WebsocketCommandResolveIDs = struct { -/// ptrs -ptrs: []const StringPointer, + pub fn decode(reader: anytype) anyerror!FileList { + var this = std.mem.zeroes(FileList); -/// files -files: []const u8, + this.ptrs = try reader.readArray(StringPointer); + this.files = try reader.readValue([]const u8); + return this; + } + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + try writer.writeArray(StringPointer, this.ptrs); + try writer.writeValue(this.files); + } + }; -pub fn decode(reader: anytype) anyerror!WebsocketCommandResolveIDs { - var this = std.mem.zeroes(WebsocketCommandResolveIDs); + pub const WebsocketMessageResolveIDs = struct { + /// id + id: []const u32, - this.ptrs = try reader.readArray(StringPointer); - this.files = try reader.readValue([]const u8); - return this; -} + /// list + list: FileList, -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { - try writer.writeArray(StringPointer, this.ptrs); - try writer.writeValue(this.files); -} + pub fn decode(reader: anytype) anyerror!WebsocketMessageResolveIDs { + var this = std.mem.zeroes(WebsocketMessageResolveIDs); -}; + this.id = try reader.readArray(u32); + this.list = try reader.readValue(FileList); + return this; + } -pub const WebsocketMessageManifestSuccess = struct { -/// id -id: u32 = 0, + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + try writer.writeArray(u32, this.id); + try writer.writeValue(this.list); + } + }; -/// module_path -module_path: []const u8, + pub const WebsocketCommandResolveIDs = struct { + /// ptrs + ptrs: []const StringPointer, -/// loader -loader: Loader, + /// files + files: []const u8, -/// manifest -manifest: DependencyManifest, + pub fn decode(reader: anytype) anyerror!WebsocketCommandResolveIDs { + var this = std.mem.zeroes(WebsocketCommandResolveIDs); + this.ptrs = try reader.readArray(StringPointer); + this.files = try reader.readValue([]const u8); + return this; + } -pub fn decode(reader: anytype) anyerror!WebsocketMessageManifestSuccess { - var this = std.mem.zeroes(WebsocketMessageManifestSuccess); + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + try writer.writeArray(StringPointer, this.ptrs); + try writer.writeValue(this.files); + } + }; - this.id = try reader.readValue(u32); - this.module_path = try reader.readValue([]const u8); - this.loader = try reader.readValue(Loader); - this.manifest = try reader.readValue(DependencyManifest); - return this; -} + pub const WebsocketMessageManifestSuccess = struct { + /// id + id: u32 = 0, -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { - try writer.writeInt(this.id); - try writer.writeValue(this.module_path); - try writer.writeEnum(this.loader); - try writer.writeValue(this.manifest); -} + /// module_path + module_path: []const u8, -}; + /// loader + loader: Loader, -pub const WebsocketMessageManifestFailure = struct { -/// id -id: u32 = 0, + /// manifest + manifest: DependencyManifest, -/// from_timestamp -from_timestamp: u32 = 0, + pub fn decode(reader: anytype) anyerror!WebsocketMessageManifestSuccess { + var this = std.mem.zeroes(WebsocketMessageManifestSuccess); -/// loader -loader: Loader, + this.id = try reader.readValue(u32); + this.module_path = try reader.readValue([]const u8); + this.loader = try reader.readValue(Loader); + this.manifest = try reader.readValue(DependencyManifest); + return this; + } -/// log -log: Log, + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + try writer.writeInt(this.id); + try writer.writeValue(this.module_path); + try writer.writeEnum(this.loader); + try writer.writeValue(this.manifest); + } + }; + pub const WebsocketMessageManifestFailure = struct { + /// id + id: u32 = 0, -pub fn decode(reader: anytype) anyerror!WebsocketMessageManifestFailure { - var this = std.mem.zeroes(WebsocketMessageManifestFailure); + /// from_timestamp + from_timestamp: u32 = 0, - this.id = try reader.readValue(u32); - this.from_timestamp = try reader.readValue(u32); - this.loader = try reader.readValue(Loader); - this.log = try reader.readValue(Log); - return this; -} + /// loader + loader: Loader, -pub fn encode(this: *const @This(), writer: anytype) anyerror!void { - try writer.writeInt(this.id); - try writer.writeInt(this.from_timestamp); - try writer.writeEnum(this.loader); - try writer.writeValue(this.log); -} + /// log + log: Log, -}; + pub fn decode(reader: anytype) anyerror!WebsocketMessageManifestFailure { + var this = std.mem.zeroes(WebsocketMessageManifestFailure); + this.id = try reader.readValue(u32); + this.from_timestamp = try reader.readValue(u32); + this.loader = try reader.readValue(Loader); + this.log = try reader.readValue(Log); + return this; + } + pub fn encode(this: *const @This(), writer: anytype) anyerror!void { + try writer.writeInt(this.id); + try writer.writeInt(this.from_timestamp); + try writer.writeEnum(this.loader); + try writer.writeValue(this.log); + } + }; }; - const ExamplePackedStruct = packed struct { len: u32 = 0, offset: u32 = 0, diff --git a/src/bundler.zig b/src/bundler.zig index ca4db463f..483b5d09b 100644 --- a/src/bundler.zig +++ b/src/bundler.zig @@ -465,8 +465,6 @@ pub fn NewBundler(cache_files: bool) type { pub const WorkerData = struct { shared_buffer: MutableString = undefined, scan_pass_result: js_parser.ScanPassResult = undefined, - templist: [100]_resolver.Result = undefined, - templist_used: u8 = 0, pub fn deinit(this: *WorkerData, allocator: *std.mem.Allocator) void { this.shared_buffer.deinit(); @@ -544,6 +542,7 @@ pub fn NewBundler(cache_files: bool) type { module_list: std.ArrayList(Api.JavascriptBundledModule), package_list: std.ArrayList(Api.JavascriptBundledPackage), header_string_buffer: MutableString, + // Just need to know if we've already enqueued this one package_list_map: std.AutoHashMap(u64, u32), queue: *BunQueue, @@ -560,15 +559,23 @@ pub fn NewBundler(cache_files: bool) type { list_lock: Lock = Lock.init(), pub const current_version: u32 = 1; + const dist_index_js_string_pointer = Api.StringPointer{ .length = "dist/index.js".len }; + const index_js_string_pointer = Api.StringPointer{ .length = "index.js".len, .offset = "dist/".len }; pub fn enqueueItem(this: *GenerateNodeModuleBundle, resolve: _resolver.Result) !void { - const loader = this.bundler.options.loaders.get(resolve.path_pair.primary.name.ext) orelse .file; - if (!loader.isJavaScriptLike()) return; - var result = resolve; - result.path_pair.primary = Fs.Path.init(std.mem.span(try this.allocator.dupeZ(u8, resolve.path_pair.primary.text))); + var path = result.path() orelse return; + const loader = this.bundler.options.loaders.get(path.name.ext) orelse .file; + if (!loader.isJavaScriptLikeOrJSON()) return; + if (BundledModuleData.get(this, &result)) |mod| { + path.* = Fs.Path.init(std.mem.span(try this.allocator.dupeZ(u8, path.text))); + + try this.queue.upsert(mod.module_id, result); + } else { + path.* = Fs.Path.init(std.mem.span(try this.allocator.dupeZ(u8, path.text))); - try this.queue.upsert(result.hash(loader), result); + try this.queue.upsert(result.hash(this.bundler.fs.top_level_dir, loader), result); + } } // The Bun Bundle Format @@ -612,6 +619,14 @@ pub fn NewBundler(cache_files: bool) type { // The specifics of the metadata is not documented here. You can find it in src/api/schema.peechy. pub fn appendHeaderString(generator: *GenerateNodeModuleBundle, str: string) !Api.StringPointer { + // This is so common we might as well just reuse it + // Plus this is one machine word so it's a quick comparison + if (strings.eqlComptime(str, "index.js")) { + return index_js_string_pointer; + } else if (strings.eqlComptime(str, "dist/index.js")) { + return dist_index_js_string_pointer; + } + var offset = generator.header_string_buffer.list.items.len; try generator.header_string_buffer.append(str); return Api.StringPointer{ @@ -639,13 +654,19 @@ pub fn NewBundler(cache_files: bool) type { ); var tmpfile = try tmpdir.createFileZ(tmpname, .{ .read = isDebug, .exclusive = true }); + + errdefer { + tmpfile.close(); + tmpdir.deleteFile(std.mem.span(tmpname)) catch {}; + } + var generator = try allocator.create(GenerateNodeModuleBundle); var queue = try BunQueue.init(allocator); defer allocator.destroy(generator); generator.* = GenerateNodeModuleBundle{ .module_list = std.ArrayList(Api.JavascriptBundledModule).init(allocator), .package_list = std.ArrayList(Api.JavascriptBundledPackage).init(allocator), - .header_string_buffer = try MutableString.init(allocator, 0), + .header_string_buffer = try MutableString.init(allocator, "dist/index.js".len), .allocator = allocator, .queue = queue, // .resolve_queue = queue, @@ -656,6 +677,9 @@ pub fn NewBundler(cache_files: bool) type { .pool = undefined, .write_lock = Lock.init(), }; + // dist/index.js appears more common than /index.js + // but this means we can store both "dist/index.js" and "index.js" in one. + try generator.header_string_buffer.append("dist/index.js"); try generator.package_list_map.ensureTotalCapacity(128); var pool = try allocator.create(ThreadPool); pool.* = ThreadPool{}; @@ -696,6 +720,7 @@ pub fn NewBundler(cache_files: bool) type { } else {} for (bundler.options.entry_points) |entry_point| { + if (bundler.options.platform == .bun) continue; defer this.bundler.resetStore(); const entry_point_path = bundler.normalizeEntryPointPath(entry_point); @@ -750,9 +775,11 @@ pub fn NewBundler(cache_files: bool) type { this.bundler.resetStore(); try this.pool.start(this); - this.pool.wait(this); + try this.pool.wait(this); if (this.log.errors > 0) { + tmpfile.close(); + tmpdir.deleteFile(std.mem.span(tmpname)) catch {}; // We stop here because if there are errors we don't know if the bundle is valid // This manifests as a crash when sorting through the module list because we may have added files to the bundle which were never actually finished being added. return null; @@ -896,8 +923,38 @@ pub fn NewBundler(cache_files: bool) type { return this.header_string_buffer.list.items[ptr.offset .. ptr.offset + ptr.length]; } - // Since we trim the prefixes, we must also compare the package name + // Since we trim the prefixes, we must also compare the package name and version pub fn sortJavascriptModuleByPath(ctx: *GenerateNodeModuleBundle, a: Api.JavascriptBundledModule, b: Api.JavascriptBundledModule) bool { + if (comptime isDebug) { + const a_pkg: Api.JavascriptBundledPackage = ctx.package_list.items[a.package_id]; + const b_pkg: Api.JavascriptBundledPackage = ctx.package_list.items[b.package_id]; + const a_name = ctx.metadataStringPointer(a_pkg.name); + const b_name = ctx.metadataStringPointer(b_pkg.name); + const a_version = ctx.metadataStringPointer(a_pkg.version); + const b_version = ctx.metadataStringPointer(b_pkg.version); + const a_path = ctx.metadataStringPointer(a.path); + const b_path = ctx.metadataStringPointer(b.path); + + std.debug.assert(a_name.len > 0); + std.debug.assert(b_name.len > 0); + std.debug.assert(a_version.len > 0); + std.debug.assert(b_version.len > 0); + std.debug.assert(a_path.len > 0); + std.debug.assert(b_path.len > 0); + + if (strings.eql(a_name, b_name)) { + if (strings.eql(a_version, b_version)) { + std.debug.assert(a_pkg.hash == b_pkg.hash); // hash collision + std.debug.assert(a.package_id == b.package_id); // duplicate package + std.debug.assert(!strings.eql(a_path, b_path)); // duplicate module + } else { + std.debug.assert(a_pkg.hash != b_pkg.hash); // incorrectly generated hash + } + } else { + std.debug.assert(a_pkg.hash != b_pkg.hash); // incorrectly generated hash + } + } + return switch (std.mem.order( u8, ctx.metadataStringPointer( @@ -907,11 +964,23 @@ pub fn NewBundler(cache_files: bool) type { ctx.package_list.items[b.package_id].name, ), )) { - .eq => std.mem.order( + .eq => switch (std.mem.order( u8, - ctx.metadataStringPointer(a.path), - ctx.metadataStringPointer(b.path), - ) == .lt, + ctx.metadataStringPointer( + ctx.package_list.items[a.package_id].version, + ), + ctx.metadataStringPointer( + ctx.package_list.items[b.package_id].version, + ), + )) { + .eq => std.mem.order( + u8, + ctx.metadataStringPointer(a.path), + ctx.metadataStringPointer(b.path), + ) == .lt, + .lt => true, + else => false, + }, .lt => true, else => false, }; @@ -933,19 +1002,17 @@ pub fn NewBundler(cache_files: bool) type { module_id: u32, pub fn get(this: *GenerateNodeModuleBundle, resolve_result: *const _resolver.Result) ?BundledModuleData { + var path = resolve_result.pathConst() orelse return null; const package_json: *const PackageJSON = this.bundler.resolver.rootNodeModulePackageJSON(resolve_result) orelse return null; const package_base_path = package_json.source.path.name.dirWithTrailingSlash(); - const import_path = resolve_result.path_pair.primary.text[package_base_path.len..]; - const package_path = resolve_result.path_pair.primary.text[package_base_path.len - package_json.name.len - 1 ..]; - var hasher = std.hash.Wyhash.init(0); - hasher.update(import_path); - hasher.update(std.mem.asBytes(&package_json.hash)); + const import_path = path.text[package_base_path.len..]; + const package_path = path.text[package_base_path.len - package_json.name.len - 1 ..]; return BundledModuleData{ .import_path = import_path, .package_path = package_path, .package = package_json, - .module_id = @truncate(u32, hasher.final()), + .module_id = package_json.hashModule(package_path), }; } }; @@ -953,22 +1020,20 @@ pub fn NewBundler(cache_files: bool) type { fn processImportRecord(this: *GenerateNodeModuleBundle, import_record: ImportRecord) !void {} pub fn processFile(this: *GenerateNodeModuleBundle, worker: *ThreadPool.Worker, _resolve: _resolver.Result) !void { - var resolve = _resolve; + const resolve = _resolve; if (resolve.is_external) return; var shared_buffer = &worker.data.shared_buffer; var scan_pass_result = &worker.data.scan_pass_result; const is_from_node_modules = resolve.isLikelyNodeModule(); - const loader = this.bundler.options.loaders.get(resolve.path_pair.primary.name.ext) orelse .file; + var file_path = (resolve.pathConst() orelse unreachable).*; + + const loader = this.bundler.options.loader(file_path.name.ext); var bundler = this.bundler; defer scan_pass_result.reset(); defer shared_buffer.reset(); defer this.bundler.resetStore(); - var file_path = resolve.path_pair.primary; - var hasher = std.hash.Wyhash.init(0); - - var module_data: BundledModuleData = undefined; // If we're in a node_module, build that almost normally if (is_from_node_modules) { @@ -978,6 +1043,9 @@ pub fn NewBundler(cache_files: bool) type { .js, .ts, => { + var written: usize = undefined; + var code_offset: u32 = 0; + const entry = try bundler.resolver.caches.fs.readFileShared( bundler.fs, file_path.text, @@ -985,26 +1053,58 @@ pub fn NewBundler(cache_files: bool) type { null, shared_buffer, ); - const source = logger.Source.initRecycledFile(Fs.File{ .path = file_path, .contents = entry.contents }, bundler.allocator) catch return null; - const source_dir = file_path.name.dirWithTrailingSlash(); - var jsx = bundler.options.jsx; - jsx.parse = loader.isJSX(); - - var opts = js_parser.Parser.Options.init(jsx, loader); - opts.transform_require_to_import = false; - opts.enable_bundling = true; - opts.warn_about_unbundled_modules = false; - - var ast: js_ast.Ast = (try bundler.resolver.caches.js.parse( - bundler.allocator, - opts, - bundler.options.define, - this.log, - &source, - )) orelse return; - if (ast.import_records.len > 0) { - { + const module_data = BundledModuleData.get(this, &resolve) orelse return error.ResolveError; + const module_id = module_data.module_id; + const package = module_data.package; + const package_relative_path = module_data.import_path; + file_path.pretty = module_data.package_path; + + // Handle empty files + // We can't just ignore them. Sometimes code will try to import it. Often because of TypeScript types. + // So we just say it's an empty object. Empty object mimicks what "browser": false does as well. + // TODO: optimize this so that all the exports for these are done in one line instead of writing repeatedly + if (entry.contents.len == 0 or (entry.contents.len < 33 and strings.trim(entry.contents, " \n\r").len == 0)) { + this.write_lock.lock(); + defer this.write_lock.unlock(); + code_offset = @truncate(u32, try this.tmpfile.getPos()); + var writer = this.tmpfile.writer(); + var buffered = std.io.bufferedWriter(writer); + + var bufwriter = buffered.writer(); + try bufwriter.writeAll("// "); + try bufwriter.writeAll(package_relative_path); + try bufwriter.writeAll("\nexport var $"); + std.fmt.formatInt(module_id, 16, .lower, .{}, bufwriter) catch unreachable; + try bufwriter.writeAll(" = () => ({});\n"); + try buffered.flush(); + this.tmpfile_byte_offset = @truncate(u32, try this.tmpfile.getPos()); + } else { + const source = logger.Source.initRecycledFile( + Fs.File{ + .path = file_path, + .contents = entry.contents, + }, + bundler.allocator, + ) catch return null; + const source_dir = file_path.name.dirWithTrailingSlash(); + + var jsx = bundler.options.jsx; + jsx.parse = loader.isJSX(); + + var opts = js_parser.Parser.Options.init(jsx, loader); + opts.transform_require_to_import = false; + opts.enable_bundling = true; + opts.warn_about_unbundled_modules = false; + + var ast: js_ast.Ast = (try bundler.resolver.caches.js.parse( + bundler.allocator, + opts, + bundler.options.define, + this.log, + &source, + )) orelse return; + if (ast.import_records.len > 0) { for (ast.import_records) |*import_record, record_id| { // Don't resolve the runtime @@ -1016,18 +1116,31 @@ pub fn NewBundler(cache_files: bool) type { if (_resolved_import.is_external) { continue; } - _resolved_import.path_pair.primary = Fs.Path.init(std.mem.span(try this.allocator.dupeZ(u8, _resolved_import.path_pair.primary.text))); + var path = _resolved_import.path() orelse { + import_record.path.is_disabled = true; + import_record.is_bundled = true; + continue; + }; - const resolved_import: *const _resolver.Result = _resolved_import; + const loader_ = bundler.options.loader(path.name.ext); - const absolute_path = resolved_import.path_pair.primary.text; + if (!loader_.isJavaScriptLikeOrJSON()) { + import_record.path.is_disabled = true; + import_record.is_bundled = true; + continue; + } - module_data = BundledModuleData.get(this, resolved_import) orelse continue; - import_record.module_id = module_data.module_id; + const resolved_import: *const _resolver.Result = _resolved_import; + + const _module_data = BundledModuleData.get(this, resolved_import) orelse continue; + import_record.module_id = _module_data.module_id; import_record.is_bundled = true; - import_record.path = Fs.Path.init(module_data.import_path); + path.* = Fs.Path.init(this.allocator.dupeZ(u8, path.text) catch unreachable); + + import_record.path = path.*; + try this.queue.upsert( - _resolved_import.hash(this.bundler.options.loaders.get(resolved_import.path_pair.primary.name.ext) orelse .file), + _module_data.module_id, _resolved_import.*, ); } else |err| { @@ -1080,157 +1193,161 @@ pub fn NewBundler(cache_files: bool) type { } } } - } - - module_data = BundledModuleData.get(this, &_resolve) orelse return error.ResolveError; - const module_id = module_data.module_id; - const package = module_data.package; - const package_relative_path = module_data.import_path; - // const load_from_symbol_ref = ast.runtime_imports.$$r.?; - // const reexport_ref = ast.runtime_imports.__reExport.?; - const register_ref = ast.runtime_imports.register.?; - const E = js_ast.E; - const Expr = js_ast.Expr; - const Stmt = js_ast.Stmt; - - var prepend_part: js_ast.Part = undefined; - var needs_prepend_part = false; - if (ast.parts.len > 1) { - for (ast.parts) |part| { - if (part.tag != .none and part.stmts.len > 0) { - prepend_part = part; - needs_prepend_part = true; - break; + // const load_from_symbol_ref = ast.runtime_imports.$$r.?; + // const reexport_ref = ast.runtime_imports.__reExport.?; + const register_ref = ast.runtime_imports.register.?; + const E = js_ast.E; + const Expr = js_ast.Expr; + const Stmt = js_ast.Stmt; + + var prepend_part: js_ast.Part = undefined; + var needs_prepend_part = false; + if (ast.parts.len > 1) { + for (ast.parts) |part| { + if (part.tag != .none and part.stmts.len > 0) { + prepend_part = part; + needs_prepend_part = true; + break; + } } } - } - if (ast.parts.len == 0) { - if (comptime isDebug) { - Output.prettyErrorln("Missing AST for file: {s}", .{file_path.text}); - Output.flush(); + if (ast.parts.len == 0) { + if (comptime isDebug) { + Output.prettyErrorln("Missing AST for file: {s}", .{file_path.text}); + Output.flush(); + } } - } - var part = &ast.parts[ast.parts.len - 1]; - var new_stmts: [1]Stmt = undefined; - var register_args: [3]Expr = undefined; - - var package_json_string = E.String{ .utf8 = package.name }; - var module_path_string = E.String{ .utf8 = module_data.import_path }; - var target_identifier = E.Identifier{ .ref = register_ref }; - var cjs_args: [2]js_ast.G.Arg = undefined; - var module_binding = js_ast.B.Identifier{ .ref = ast.module_ref.? }; - var exports_binding = js_ast.B.Identifier{ .ref = ast.exports_ref.? }; - - // if (!ast.uses_module_ref) { - // var symbol = &ast.symbols[ast.module_ref.?.inner_index]; - // symbol.original_name = "_$$"; - // } - - cjs_args[0] = js_ast.G.Arg{ - .binding = js_ast.Binding{ - .loc = logger.Loc.Empty, - .data = .{ .b_identifier = &module_binding }, - }, - }; - cjs_args[1] = js_ast.G.Arg{ - .binding = js_ast.Binding{ - .loc = logger.Loc.Empty, - .data = .{ .b_identifier = &exports_binding }, - }, - }; + var part = &ast.parts[ast.parts.len - 1]; + var new_stmts: [1]Stmt = undefined; + var register_args: [3]Expr = undefined; + + var package_json_string = E.String{ .utf8 = package.name }; + var module_path_string = E.String{ .utf8 = module_data.import_path }; + var target_identifier = E.Identifier{ .ref = register_ref }; + var cjs_args: [2]js_ast.G.Arg = undefined; + var module_binding = js_ast.B.Identifier{ .ref = ast.module_ref.? }; + var exports_binding = js_ast.B.Identifier{ .ref = ast.exports_ref.? }; + + // if (!ast.uses_module_ref) { + // var symbol = &ast.symbols[ast.module_ref.?.inner_index]; + // symbol.original_name = "_$$"; + // } + + cjs_args[0] = js_ast.G.Arg{ + .binding = js_ast.Binding{ + .loc = logger.Loc.Empty, + .data = .{ .b_identifier = &module_binding }, + }, + }; + cjs_args[1] = js_ast.G.Arg{ + .binding = js_ast.Binding{ + .loc = logger.Loc.Empty, + .data = .{ .b_identifier = &exports_binding }, + }, + }; - var closure = E.Arrow{ - .args = &cjs_args, - .body = .{ - .loc = logger.Loc.Empty, - .stmts = part.stmts, - }, - }; + var closure = E.Arrow{ + .args = &cjs_args, + .body = .{ + .loc = logger.Loc.Empty, + .stmts = part.stmts, + }, + }; - // $$m(12345, "react", "index.js", function(module, exports) { + // $$m(12345, "react", "index.js", function(module, exports) { - // }) - register_args[0] = Expr{ .loc = .{ .start = 0 }, .data = .{ .e_string = &package_json_string } }; - register_args[1] = Expr{ .loc = .{ .start = 0 }, .data = .{ .e_string = &module_path_string } }; - register_args[2] = Expr{ .loc = .{ .start = 0 }, .data = .{ .e_arrow = &closure } }; + // }) + register_args[0] = Expr{ .loc = .{ .start = 0 }, .data = .{ .e_string = &package_json_string } }; + register_args[1] = Expr{ .loc = .{ .start = 0 }, .data = .{ .e_string = &module_path_string } }; + register_args[2] = Expr{ .loc = .{ .start = 0 }, .data = .{ .e_arrow = &closure } }; - var call_register = E.Call{ - .target = Expr{ - .data = .{ .e_identifier = &target_identifier }, - .loc = logger.Loc{ .start = 0 }, - }, - .args = ®ister_args, - }; - var register_expr = Expr{ .loc = call_register.target.loc, .data = .{ .e_call = &call_register } }; - var decls: [1]js_ast.G.Decl = undefined; - var bundle_export_binding = js_ast.B.Identifier{ .ref = ast.bundle_export_ref.? }; - var binding = js_ast.Binding{ - .loc = register_expr.loc, - .data = .{ .b_identifier = &bundle_export_binding }, - }; - decls[0] = js_ast.G.Decl{ - .value = register_expr, - .binding = binding, - }; - var export_var = js_ast.S.Local{ - .decls = &decls, - .is_export = true, - }; - new_stmts[0] = Stmt{ .loc = register_expr.loc, .data = .{ .s_local = &export_var } }; - part.stmts = &new_stmts; + var call_register = E.Call{ + .target = Expr{ + .data = .{ .e_identifier = &target_identifier }, + .loc = logger.Loc{ .start = 0 }, + }, + .args = ®ister_args, + }; + var register_expr = Expr{ .loc = call_register.target.loc, .data = .{ .e_call = &call_register } }; + var decls: [1]js_ast.G.Decl = undefined; + var bundle_export_binding = js_ast.B.Identifier{ .ref = ast.bundle_export_ref.? }; + var binding = js_ast.Binding{ + .loc = register_expr.loc, + .data = .{ .b_identifier = &bundle_export_binding }, + }; + decls[0] = js_ast.G.Decl{ + .value = register_expr, + .binding = binding, + }; + var export_var = js_ast.S.Local{ + .decls = &decls, + .is_export = true, + }; + new_stmts[0] = Stmt{ .loc = register_expr.loc, .data = .{ .s_local = &export_var } }; + part.stmts = &new_stmts; + + var writer = js_printer.NewFileWriter(this.tmpfile); + var symbols: [][]js_ast.Symbol = &([_][]js_ast.Symbol{ast.symbols}); + + // It should only have one part. + ast.parts = ast.parts[ast.parts.len - 1 ..]; + + const write_result = + try js_printer.printCommonJSThreaded( + @TypeOf(writer), + writer, + ast, + js_ast.Symbol.Map.initList(symbols), + &source, + false, + js_printer.Options{ + .to_module_ref = Ref.RuntimeRef, + .bundle_export_ref = ast.bundle_export_ref.?, + .source_path = file_path, + .externals = ast.externals, + .indent = 0, + .module_hash = module_id, + .runtime_imports = ast.runtime_imports, + .prepend_part_value = &prepend_part, + .prepend_part_key = if (needs_prepend_part) closure.body.stmts.ptr else null, + }, + Linker, + &bundler.linker, + &this.write_lock, + std.fs.File, + this.tmpfile, + std.fs.File.getPos, + ); - var writer = js_printer.NewFileWriter(this.tmpfile); - var symbols: [][]js_ast.Symbol = &([_][]js_ast.Symbol{ast.symbols}); + code_offset = write_result.off; + written = write_result.len; - // It should only have one part. - ast.parts = ast.parts[ast.parts.len - 1 ..]; - var written: usize = undefined; - var code_offset: u32 = 0; - const write_result = - try js_printer.printCommonJSThreaded( - @TypeOf(writer), - writer, - ast, - js_ast.Symbol.Map.initList(symbols), - &source, - false, - js_printer.Options{ - .to_module_ref = Ref.RuntimeRef, - .bundle_export_ref = ast.bundle_export_ref.?, - .source_path = file_path, - .externals = ast.externals, - .indent = 0, - .module_hash = module_id, - .runtime_imports = ast.runtime_imports, - .prepend_part_value = &prepend_part, - .prepend_part_key = if (needs_prepend_part) closure.body.stmts.ptr else null, - }, - Linker, - &bundler.linker, - &this.write_lock, - std.fs.File, - this.tmpfile, - std.fs.File.getPos, - ); + // Faster to _not_ do the syscall + // But there's some off-by-one error somewhere and more reliable to just do the lseek + this.tmpfile_byte_offset = write_result.end_off; + } if (comptime isDebug) { - Output.prettyln("{s}/{s} \n", .{ package.name, package_relative_path }); + Output.prettyln("{s}@{s}/{s} - {d}:{d} \n", .{ package.name, package.version, package_relative_path, package.hash, module_id }); Output.flush(); + std.debug.assert(package_relative_path.len > 0); } this.list_lock.lock(); defer this.list_lock.unlock(); - code_offset = write_result.off; - written = write_result.len; - // Faster to _not_ do the syscall - // But there's some off-by-one error somewhere and more reliable to just do the lseek - this.tmpfile_byte_offset = write_result.end_off; const code_length = this.tmpfile_byte_offset - code_offset; - // std.debug.assert(code_length == written); + + if (comptime isDebug) { + std.debug.assert(code_length > 0); + std.debug.assert(package.hash != 0); + std.debug.assert(package.version.len > 0); + std.debug.assert(package.name.len > 0); + std.debug.assert(module_id > 0); + } var package_get_or_put_entry = try this.package_list_map.getOrPut(package.hash); @@ -1279,6 +1396,7 @@ pub fn NewBundler(cache_files: bool) type { null, shared_buffer, ) catch return; + if (entry.contents.len == 0 or (entry.contents.len < 33 and strings.trim(entry.contents, " \n\r").len == 0)) return; const source = logger.Source.initRecycledFile(Fs.File{ .path = file_path, .contents = entry.contents }, bundler.allocator) catch return null; const source_dir = file_path.name.dirWithTrailingSlash(); @@ -1307,13 +1425,26 @@ pub fn NewBundler(cache_files: bool) type { continue; } - _resolved_import.path_pair.primary = Fs.Path.init(std.mem.span(try this.allocator.dupeZ(u8, _resolved_import.path_pair.primary.text))); - try this.queue.upsert( - _resolved_import.hash( - this.bundler.options.loaders.get(_resolved_import.path_pair.primary.name.ext) orelse .file, - ), - _resolved_import.*, - ); + var path = _resolved_import.path() orelse continue; + + const loader_ = this.bundler.options.loader(path.name.ext); + if (!loader_.isJavaScriptLikeOrJSON()) continue; + path.* = Fs.Path.init(std.mem.span(try this.allocator.dupeZ(u8, path.text))); + + if (BundledModuleData.get(this, _resolved_import)) |mod| { + try this.queue.upsert( + mod.module_id, + _resolved_import.*, + ); + } else { + try this.queue.upsert( + _resolved_import.hash( + this.bundler.fs.top_level_dir, + loader_, + ), + _resolved_import.*, + ); + } } else |err| { switch (err) { error.ModuleNotFound => { @@ -1393,7 +1524,12 @@ pub fn NewBundler(cache_files: bool) type { errdefer bundler.resetStore(); - var file_path = resolve_result.path_pair.primary; + var file_path = (resolve_result.pathConst() orelse { + return BuildResolveResultPair{ + .written = 0, + .input_fd = null, + }; + }).*; if (strings.indexOf(file_path.text, bundler.fs.top_level_dir)) |i| { file_path.pretty = file_path.text[i + bundler.fs.top_level_dir.len ..]; @@ -1401,12 +1537,7 @@ pub fn NewBundler(cache_files: bool) type { file_path.pretty = allocator.dupe(u8, bundler.fs.relativeTo(file_path.text)) catch unreachable; } - var old_bundler_allocator = bundler.allocator; - bundler.allocator = allocator; - defer bundler.allocator = old_bundler_allocator; - var old_linker_allocator = bundler.linker.allocator; - defer bundler.linker.allocator = old_linker_allocator; - bundler.linker.allocator = allocator; + bundler.setAllocator(allocator); switch (loader) { .css => { @@ -1432,7 +1563,7 @@ pub fn NewBundler(cache_files: bool) type { .written = brk: { if (bundler.options.hot_module_reloading) { break :brk (try CSSBundlerHMR.bundle( - resolve_result.path_pair.primary.text, + file_path.text, bundler.fs, writer, watcher, @@ -1445,7 +1576,7 @@ pub fn NewBundler(cache_files: bool) type { )).written; } else { break :brk (try CSSBundler.bundle( - resolve_result.path_pair.primary.text, + file_path.text, bundler.fs, writer, watcher, @@ -1462,6 +1593,8 @@ pub fn NewBundler(cache_files: bool) type { }; }, else => { + var old_allocator = bundler.allocator; + bundler.setAllocator(allocator); var result = bundler.parse( allocator, file_path, @@ -1471,12 +1604,14 @@ pub fn NewBundler(cache_files: bool) type { filepath_hash, client_entry_point, ) orelse { + bundler.setAllocator(old_allocator); bundler.resetStore(); return BuildResolveResultPair{ .written = 0, .input_fd = null, }; }; + bundler.setAllocator(old_allocator); try bundler.linker.link(file_path, &result, import_path_format, false); @@ -1505,9 +1640,10 @@ pub fn NewBundler(cache_files: bool) type { return null; } + var file_path = (resolve_result.pathConst() orelse return null).*; + // Step 1. Parse & scan - const loader = bundler.options.loaders.get(resolve_result.path_pair.primary.name.ext) orelse .file; - var file_path = resolve_result.path_pair.primary; + const loader = bundler.options.loader(file_path.name.ext); if (client_entry_point_) |client_entry_point| { file_path = client_entry_point.source.path; @@ -1796,15 +1932,11 @@ pub fn NewBundler(cache_files: bool) type { comptime client_entry_point_enabled: bool, ) !ServeResult { var extension = _extension; - var original_resolver_logger = bundler.resolver.log; - var original_bundler_logger = bundler.log; - - defer bundler.log = original_bundler_logger; - defer bundler.resolver.log = original_resolver_logger; - bundler.log = log; - bundler.linker.allocator = allocator; + var old_log = bundler.log; + var old_allocator = bundler.allocator; - bundler.resolver.log = log; + bundler.setLog(log); + defer bundler.setLog(old_log); if (strings.eqlComptime(relative_path, "__runtime.js")) { return ServeResult{ @@ -1862,8 +1994,10 @@ pub fn NewBundler(cache_files: bool) type { break :brk (try bundler.resolver.resolve(bundler.fs.top_level_dir, absolute_path, .stmt)); }; - const loader = bundler.options.loader(resolved.path_pair.primary.name.ext); - const mime_type_ext = bundler.options.out_extensions.get(resolved.path_pair.primary.name.ext) orelse resolved.path_pair.primary.name.ext; + const path = (resolved.pathConst() orelse return error.ModuleNotFound); + + const loader = bundler.options.loader(path.name.ext); + const mime_type_ext = bundler.options.out_extensions.get(path.name.ext) orelse path.name.ext; switch (loader) { .js, .jsx, .ts, .tsx, .css => { @@ -1882,7 +2016,7 @@ pub fn NewBundler(cache_files: bool) type { }; }, else => { - var abs_path = resolved.path_pair.primary.text; + var abs_path = path.text; const file = try std.fs.openFileAbsolute(abs_path, .{ .read = true }); var stat = try file.stat(); return ServeResult{ @@ -1941,10 +2075,17 @@ pub fn NewBundler(cache_files: bool) type { } const result = bundler.resolver.resolve(bundler.fs.top_level_dir, entry, .entry_point) catch |err| { - Output.printError("Error resolving \"{s}\": {s}\n", .{ entry, @errorName(err) }); + Output.prettyError("Error resolving \"{s}\": {s}\n", .{ entry, @errorName(err) }); continue; }; + if (result.pathConst() == null) { + Output.prettyError("\"{s}\" is disabled due to \"browser\" field in package.json.\n", .{ + entry, + }); + continue; + } + if (bundler.linker.enqueueResolveResult(&result) catch unreachable) { entry_points[entry_point_i] = result; entry_point_i += 1; @@ -2098,12 +2239,13 @@ pub fn NewBundler(cache_files: bool) type { // defer count += 1; if (comptime wrap_entry_point) { - const loader = bundler.options.loaders.get(item.path_pair.primary.name.ext) orelse .file; + var path = item.pathConst() orelse unreachable; + const loader = bundler.options.loader(path.name.ext); if (item.import_kind == .entry_point and loader.supportsClientEntryPoint()) { var client_entry_point = try bundler.allocator.create(ClientEntryPoint); client_entry_point.* = ClientEntryPoint{}; - try client_entry_point.generate(ThisBundler, bundler, item.path_pair.primary.name, bundler.options.framework.?.client); + try client_entry_point.generate(ThisBundler, bundler, path.name, bundler.options.framework.?.client); try bundler.virtual_modules.append(client_entry_point); const entry_point_output_file = bundler.buildWithResolveResultEager( @@ -2602,6 +2744,9 @@ pub const ServerEntryPoint = struct { \\//Auto-generated file \\import * as start from '{s}{s}'; \\export * from '{s}{s}'; + \\if ('default' in start && typeof start.default == 'function') {{ + \\ start.default(); + \\}} , .{ dir_to_use, diff --git a/src/fs.zig b/src/fs.zig index 3936ea933..e59d24b6f 100644 --- a/src/fs.zig +++ b/src/fs.zig @@ -16,7 +16,7 @@ const hash_map = @import("hash_map.zig"); pub const Preallocate = struct { pub const Counts = struct { pub const dir_entry: usize = 512; - pub const files: usize = 1024; + pub const files: usize = 4096; }; }; @@ -810,7 +810,7 @@ pub const FileSystem = struct { ) !File { FileSystem.setMaxFd(file.handle); - if (FeatureFlags.disable_filesystem_cache) { + if (comptime FeatureFlags.disable_filesystem_cache) { _ = std.os.fcntl(file.handle, std.os.F_NOCACHE, 1) catch 0; } @@ -820,6 +820,18 @@ pub const FileSystem = struct { return err; }); + // Skip the pread call for empty files + // Otherwise will get out of bounds errors + // plus it's an unnecessary syscall + if (size == 0) { + if (comptime use_shared_buffer) { + shared_buffer.reset(); + return File{ .path = Path.init(path), .contents = shared_buffer.list.items }; + } else { + return File{ .path = Path.init(path), .contents = "" }; + } + } + var file_contents: []u8 = undefined; // When we're serving a JavaScript-like file over HTTP, we do not want to cache the contents in memory diff --git a/src/hash_map_v2.zig b/src/hash_map_v2.zig index cad4bd36c..e0b84fcd5 100644 --- a/src/hash_map_v2.zig +++ b/src/hash_map_v2.zig @@ -48,6 +48,10 @@ pub fn getAutoHashFn(comptime K: type, comptime Context: type) (fn (Context, K) pub fn getAutoEqlFn(comptime K: type, comptime Context: type) (fn (Context, K, K) bool) { return struct { fn eql(ctx: Context, a: K, b: K) bool { + if (comptime @typeInfo(K) == .Struct and @hasDecl(K, "eql")) { + return a.eql(b); + } + return meta.eql(a, b); } }.eql; @@ -965,7 +969,7 @@ pub fn HashMapUnmanaged( /// fuse the basic blocks after the branch to the basic blocks /// from this function. To encourage that, this function is /// marked as inline. - fn getIndex(self: Self, key: anytype, ctx: anytype) callconv(.Inline) ?usize { + inline fn getIndex(self: Self, key: anytype, ctx: anytype) ?usize { comptime verifyContext(@TypeOf(ctx), @TypeOf(key), K, Hash); if (self.size == 0) { diff --git a/src/import_record.zig b/src/import_record.zig index ae09f8d2f..adb6ea625 100644 --- a/src/import_record.zig +++ b/src/import_record.zig @@ -65,6 +65,8 @@ pub const ImportRecord = struct { is_internal: bool = false, + // This tells the printer that we should print as export var $moduleID = ... + // Instead of using the path. is_bundled: bool = false, // Sometimes the parser creates an import record and decides it isn't needed. diff --git a/src/javascript/jsc/bindings/bindings.zig b/src/javascript/jsc/bindings/bindings.zig index b01690f2a..65a8bef94 100644 --- a/src/javascript/jsc/bindings/bindings.zig +++ b/src/javascript/jsc/bindings/bindings.zig @@ -92,7 +92,7 @@ pub const ZigString = extern struct { pub fn fromStringPointer(ptr: StringPointer, buf: string, to: *ZigString) void { to.* = ZigString{ .len = ptr.length, - .ptr = buf[ptr.offset..ptr.length].ptr, + .ptr = buf[ptr.offset..][0..ptr.length].ptr, }; } diff --git a/src/javascript/jsc/bindings/exports.zig b/src/javascript/jsc/bindings/exports.zig index d58435785..6a8c6328b 100644 --- a/src/javascript/jsc/bindings/exports.zig +++ b/src/javascript/jsc/bindings/exports.zig @@ -4,6 +4,7 @@ const Fs = @import("../../../fs.zig"); const CAPI = @import("../JavaScriptCore.zig"); const JS = @import("../javascript.zig"); const JSBase = @import("../base.zig"); +const ZigURL = @import("../../../query_string_map.zig").URL; const Handler = struct { pub export fn global_signal_handler_fn(sig: i32, info: *const std.os.siginfo_t, ctx_ptr: ?*const c_void) callconv(.C) void { var stdout = std.io.getStdOut(); @@ -347,8 +348,15 @@ pub const ZigStackFrame = extern struct { source_url: ZigString, position: ZigStackFramePosition, enable_color: bool, + origin: *const ZigURL, pub fn format(this: SourceURLFormatter, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { + try writer.writeAll(this.origin.displayProtocol()); + try writer.writeAll("://"); + try writer.writeAll(this.origin.displayHostname()); + try writer.writeAll(":"); + try writer.writeAll(this.origin.port); + try writer.writeAll("/blob:"); try writer.writeAll(this.source_url.slice()); if (this.position.line > -1 and this.position.column_start > -1) { try std.fmt.format(writer, ":{d}:{d}", .{ this.position.line + 1, this.position.column_start }); @@ -416,8 +424,8 @@ pub const ZigStackFrame = extern struct { return NameFormatter{ .function_name = this.function_name, .code_type = this.code_type, .enable_color = enable_color }; } - pub fn sourceURLFormatter(this: *const ZigStackFrame, comptime enable_color: bool) SourceURLFormatter { - return SourceURLFormatter{ .source_url = this.source_url, .position = this.position, .enable_color = enable_color }; + pub fn sourceURLFormatter(this: *const ZigStackFrame, origin: *const ZigURL, comptime enable_color: bool) SourceURLFormatter { + return SourceURLFormatter{ .source_url = this.source_url, .origin = origin, .position = this.position, .enable_color = enable_color }; } }; diff --git a/src/javascript/jsc/javascript.zig b/src/javascript/jsc/javascript.zig index 4a3c19964..fa942e4a2 100644 --- a/src/javascript/jsc/javascript.zig +++ b/src/javascript/jsc/javascript.zig @@ -34,6 +34,7 @@ pub const GlobalClasses = [_]type{ ResolveError.Class, Bun.Class, }; +const Blob = @import("../../blob.zig"); pub const Bun = struct { threadlocal var css_imports_list_strings: [512]ZigString = undefined; @@ -345,10 +346,12 @@ pub const VirtualMachine = struct { event_listeners: EventListenerMixin.Map, main: string = "", process: js.JSObjectRef = null, - + blobs: *Blob.Group = undefined, flush_list: std.ArrayList(string), entry_point: ServerEntryPoint = undefined, + has_loaded: bool = false, + pub var vm_loaded = false; pub var vm: *VirtualMachine = undefined; @@ -387,6 +390,7 @@ pub const VirtualMachine = struct { .node_modules = bundler.options.node_modules_bundle, .log = log, .flush_list = std.ArrayList(string).init(allocator), + .blobs = try Blob.Group.init(allocator), }; VirtualMachine.vm.bundler.configureLinker(); @@ -418,6 +422,11 @@ pub const VirtualMachine = struct { threadlocal var source_code_printer: js_printer.BufferPrinter = undefined; threadlocal var source_code_printer_loaded: bool = false; + pub fn preflush(this: *VirtualMachine) void { + // We flush on the next tick so that if there were any errors you can still see them + this.blobs.temporary.reset() catch {}; + } + pub fn flush(this: *VirtualMachine) void { for (this.flush_list.items) |item| { this.allocator.free(item); @@ -650,6 +659,7 @@ pub const VirtualMachine = struct { .stmt, ); ret.result = result; + const result_path = result.pathConst() orelse return error.ModuleNotFound; if (vm.node_modules != null and result.isLikelyNodeModule()) { const node_modules_bundle = vm.node_modules.?; @@ -682,7 +692,7 @@ pub const VirtualMachine = struct { const package_relative_path = vm.bundler.fs.relative( package_json.source.path.name.dirWithTrailingSlash(), - result.path_pair.primary.text, + result_path.text, ); if (node_modules_bundle.findModuleIDInPackage(package, package_relative_path) == null) break :node_module_checker; @@ -693,7 +703,7 @@ pub const VirtualMachine = struct { } } - ret.path = result.path_pair.primary.text; + ret.path = result_path.text; } pub fn resolve(res: *ErrorableZigString, global: *JSGlobalObject, specifier: ZigString, source: ZigString) void { @@ -751,6 +761,11 @@ pub const VirtualMachine = struct { return slice; } + pub fn promiseRejectionTracker(global: *JSGlobalObject, promise: *JSPromise, rejection: JSPromiseRejectionOperation) callconv(.C) JSValue { + VirtualMachine.vm.defaultErrorHandler(promise.result(global.vm())); + return JSValue.jsUndefined(); + } + const main_file_name: string = "bun:main"; threadlocal var errors_stack: [256]*c_void = undefined; pub fn fetch(ret: *ErrorableResolvedSource, global: *JSGlobalObject, specifier: ZigString, source: ZigString) callconv(.C) void { @@ -775,6 +790,13 @@ pub const VirtualMachine = struct { } ret.result.value = result; + + if (vm.has_loaded) { + vm.blobs.temporary.put(specifier.slice(), .{ .ptr = result.source_code.ptr, .len = result.source_code.len }) catch {}; + } else { + vm.blobs.persistent.put(specifier.slice(), .{ .ptr = result.source_code.ptr, .len = result.source_code.len }) catch {}; + } + ret.success = true; } @@ -977,7 +999,7 @@ pub const VirtualMachine = struct { " at {any} ({any})\n", allow_ansi_colors, ), - .{ frame.nameFormatter(allow_ansi_colors), frame.sourceURLFormatter(allow_ansi_colors) }, + .{ frame.nameFormatter(allow_ansi_colors), frame.sourceURLFormatter(&vm.bundler.options.origin, allow_ansi_colors) }, ); // if (!frame.position.isInvalid()) { diff --git a/src/js_parser/js_parser.zig b/src/js_parser/js_parser.zig index 760d3ec2b..83a1b0127 100644 --- a/src/js_parser/js_parser.zig +++ b/src/js_parser/js_parser.zig @@ -130,7 +130,7 @@ pub const ImportScanner = struct { // // const keep_unused_imports = !p.options.trim_unused_imports; var did_remove_star_loc = false; - const keep_unused_imports = false; + const keep_unused_imports = !p.options.ts; // TypeScript always trims unused imports. This is important for // correctness since some imports might be fake (only in the type @@ -205,11 +205,7 @@ pub const ImportScanner = struct { } } - if (items_end < st.items.len - 1) { - var list = List(js_ast.ClauseItem).fromOwnedSlice(p.allocator, st.items); - list.shrinkAndFree(items_end); - st.items = list.toOwnedSlice(); - } + st.items = st.items[0..items_end]; } // -- Original Comment -- @@ -285,7 +281,7 @@ pub const ImportScanner = struct { // it's really stupid to import all 1,000 components from that design system // when you just want