diff options
Diffstat (limited to 'src')
34 files changed, 222 insertions, 2204 deletions
diff --git a/src/api/schema.d.ts b/src/api/schema.d.ts index a982b910d..f1d5b5f62 100644 --- a/src/api/schema.d.ts +++ b/src/api/schema.d.ts @@ -547,9 +547,6 @@ export interface TransformOptions { target?: Target; serve?: boolean; extension_order?: string[]; - generate_node_module_bundle?: boolean; - node_modules_bundle_path?: string; - node_modules_bundle_path_server?: string; framework?: FrameworkConfig; router?: RouteConfig; no_summary?: boolean; diff --git a/src/api/schema.js b/src/api/schema.js index b28c5669c..4931cd716 100644 --- a/src/api/schema.js +++ b/src/api/schema.js @@ -1669,42 +1669,30 @@ function decodeTransformOptions(bb) { break; case 18: - result["generate_node_module_bundle"] = !!bb.readByte(); - break; - - case 19: - result["node_modules_bundle_path"] = bb.readString(); - break; - - case 20: - result["node_modules_bundle_path_server"] = bb.readString(); - break; - - case 21: result["framework"] = decodeFrameworkConfig(bb); break; - case 22: + case 19: result["router"] = decodeRouteConfig(bb); break; - case 23: + case 20: result["no_summary"] = !!bb.readByte(); break; - case 24: + case 21: result["disable_hmr"] = !!bb.readByte(); break; - case 25: + case 22: result["port"] = bb.readUint16(); break; - case 26: + case 23: result["logLevel"] = MessageLevel[bb.readVarUint()]; break; - case 27: + case 24: result["source_map"] = SourceMapMode[bb.readByte()]; break; @@ -1851,57 +1839,39 @@ function encodeTransformOptions(message, bb) { } } - var value = message["generate_node_module_bundle"]; - if (value != null) { - bb.writeByte(18); - bb.writeByte(value); - } - - var value = message["node_modules_bundle_path"]; - if (value != null) { - bb.writeByte(19); - bb.writeString(value); - } - - var value = message["node_modules_bundle_path_server"]; - if (value != null) { - bb.writeByte(20); - bb.writeString(value); - } - var value = message["framework"]; if (value != null) { - bb.writeByte(21); + bb.writeByte(18); encodeFrameworkConfig(value, bb); } var value = message["router"]; if (value != null) { - bb.writeByte(22); + bb.writeByte(19); encodeRouteConfig(value, bb); } var value = message["no_summary"]; if (value != null) { - bb.writeByte(23); + bb.writeByte(20); bb.writeByte(value); } var value = message["disable_hmr"]; if (value != null) { - bb.writeByte(24); + bb.writeByte(21); bb.writeByte(value); } var value = message["port"]; if (value != null) { - bb.writeByte(25); + bb.writeByte(22); bb.writeUint16(value); } var value = message["logLevel"]; if (value != null) { - bb.writeByte(26); + bb.writeByte(23); var encoded = MessageLevel[value]; if (encoded === void 0) throw new Error("Invalid value " + JSON.stringify(value) + ' for enum "MessageLevel"'); bb.writeVarUint(encoded); @@ -1909,7 +1879,7 @@ function encodeTransformOptions(message, bb) { var value = message["source_map"]; if (value != null) { - bb.writeByte(27); + bb.writeByte(24); var encoded = SourceMapMode[value]; if (encoded === void 0) throw new Error("Invalid value " + JSON.stringify(value) + ' for enum "SourceMapMode"'); bb.writeByte(encoded); diff --git a/src/api/schema.peechy b/src/api/schema.peechy index 53800e4f3..dc8e312a5 100644 --- a/src/api/schema.peechy +++ b/src/api/schema.peechy @@ -337,20 +337,15 @@ message TransformOptions { string[] extension_order = 17; - bool generate_node_module_bundle = 18; + FrameworkConfig framework = 18; + RouteConfig router = 19; + bool no_summary = 20; - string node_modules_bundle_path = 19; - string node_modules_bundle_path_server = 20; + bool disable_hmr = 21; - FrameworkConfig framework = 21; - RouteConfig router = 22; - bool no_summary = 23; - - bool disable_hmr = 24; - - uint16 port = 25; - MessageLevel logLevel = 26; - SourceMapMode source_map = 27; + uint16 port = 22; + MessageLevel logLevel = 23; + SourceMapMode source_map = 24; } smol SourceMapMode { diff --git a/src/api/schema.zig b/src/api/schema.zig index de46a813b..93b526d47 100644 --- a/src/api/schema.zig +++ b/src/api/schema.zig @@ -1732,15 +1732,6 @@ pub const Api = struct { /// 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, @@ -1823,33 +1814,24 @@ pub const Api = struct { 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 => { + 19 => { this.router = try reader.readValue(RouteConfig); }, - 23 => { + 20 => { this.no_summary = try reader.readValue(bool); }, - 24 => { + 21 => { this.disable_hmr = try reader.readValue(bool); }, - 25 => { + 22 => { this.port = try reader.readValue(u16); }, - 26 => { + 23 => { this.log_level = try reader.readValue(MessageLevel); }, - 27 => { + 24 => { this.source_map = try reader.readValue(SourceMapMode); }, else => { @@ -1929,44 +1911,32 @@ pub const Api = struct { 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(@as(u8, @intFromBool(generate_node_module_bundle))); - } - if (this.node_modules_bundle_path) |node_modules_bundle_path| { - try writer.writeFieldID(19); - try writer.writeValue(@TypeOf(node_modules_bundle_path), node_modules_bundle_path); - } - if (this.node_modules_bundle_path_server) |node_modules_bundle_path_server| { - try writer.writeFieldID(20); - try writer.writeValue(@TypeOf(node_modules_bundle_path_server), node_modules_bundle_path_server); - } if (this.framework) |framework| { - try writer.writeFieldID(21); + try writer.writeFieldID(18); try writer.writeValue(@TypeOf(framework), framework); } if (this.router) |router| { - try writer.writeFieldID(22); + try writer.writeFieldID(19); try writer.writeValue(@TypeOf(router), router); } if (this.no_summary) |no_summary| { - try writer.writeFieldID(23); + try writer.writeFieldID(20); try writer.writeInt(@as(u8, @intFromBool(no_summary))); } if (this.disable_hmr) |disable_hmr| { - try writer.writeFieldID(24); + try writer.writeFieldID(21); try writer.writeInt(@as(u8, @intFromBool(disable_hmr))); } if (this.port) |port| { - try writer.writeFieldID(25); + try writer.writeFieldID(22); try writer.writeInt(port); } if (this.log_level) |log_level| { - try writer.writeFieldID(26); + try writer.writeFieldID(23); try writer.writeEnum(log_level); } if (this.source_map) |source_map| { - try writer.writeFieldID(27); + try writer.writeFieldID(24); try writer.writeEnum(source_map); } try writer.endMessage(); diff --git a/src/bun.js/api/JSTranspiler.zig b/src/bun.js/api/JSTranspiler.zig index 5c5bee171..9376eb964 100644 --- a/src/bun.js/api/JSTranspiler.zig +++ b/src/bun.js/api/JSTranspiler.zig @@ -767,7 +767,6 @@ pub fn constructor( allocator, log, transpiler_options.transform, - null, JavaScript.VirtualMachine.get().bundler.env, ) catch |err| { if ((log.warnings + log.errors) > 0) { diff --git a/src/bun.js/api/bun.zig b/src/bun.js/api/bun.zig index d2349ce2b..6daff5fcc 100644 --- a/src/bun.js/api/bun.zig +++ b/src/bun.js/api/bun.zig @@ -14,7 +14,7 @@ const IdentityContext = @import("../../identity_context.zig").IdentityContext; const Fs = @import("../../fs.zig"); const Resolver = @import("../../resolver/resolver.zig"); const ast = @import("../../import_record.zig"); -const NodeModuleBundle = @import("../../node_module_bundle.zig").NodeModuleBundle; + const MacroEntryPoint = bun.bundler.MacroEntryPoint; const logger = @import("root").bun.logger; const Api = @import("../../api/schema.zig").Api; diff --git a/src/bun.js/api/ffi.zig b/src/bun.js/api/ffi.zig index 1ca44619d..8d02f6672 100644 --- a/src/bun.js/api/ffi.zig +++ b/src/bun.js/api/ffi.zig @@ -15,7 +15,7 @@ const IdentityContext = @import("../../identity_context.zig").IdentityContext; const Fs = @import("../../fs.zig"); const Resolver = @import("../../resolver/resolver.zig"); const ast = @import("../../import_record.zig"); -const NodeModuleBundle = @import("../../node_module_bundle.zig").NodeModuleBundle; + const MacroEntryPoint = bun.bundler.MacroEntryPoint; const logger = @import("root").bun.logger; const Api = @import("../../api/schema.zig").Api; diff --git a/src/bun.js/api/server.zig b/src/bun.js/api/server.zig index 195d2a3f3..4eca89cc6 100644 --- a/src/bun.js/api/server.zig +++ b/src/bun.js/api/server.zig @@ -14,7 +14,7 @@ const IdentityContext = @import("../../identity_context.zig").IdentityContext; const Fs = @import("../../fs.zig"); const Resolver = @import("../../resolver/resolver.zig"); const ast = @import("../../import_record.zig"); -const NodeModuleBundle = @import("../../node_module_bundle.zig").NodeModuleBundle; + const MacroEntryPoint = bun.bundler.MacroEntryPoint; const logger = @import("root").bun.logger; const Api = @import("../../api/schema.zig").Api; diff --git a/src/bun.js/config.zig b/src/bun.js/config.zig index 6e304d526..f95146c6d 100644 --- a/src/bun.js/config.zig +++ b/src/bun.js/config.zig @@ -13,7 +13,6 @@ const std = @import("std"); const Fs = @import("../fs.zig"); const resolver = @import("../resolver/resolver.zig"); const ast = @import("../import_record.zig"); -const NodeModuleBundle = @import("../node_module_bundle.zig").NodeModuleBundle; const logger = @import("root").bun.logger; const Api = @import("../api/schema.zig").Api; const options = @import("../options.zig"); @@ -36,7 +35,6 @@ pub fn configureTransformOptionsForBunVM(allocator: std.mem.Allocator, _args: Ap // args.serve = false; args.write = false; args.resolve = Api.ResolveMode.lazy; - args.generate_node_module_bundle = false; return try configureTransformOptionsForBun(allocator, args); } diff --git a/src/bun.js/javascript.zig b/src/bun.js/javascript.zig index 741affa6a..e7ed3e0ab 100644 --- a/src/bun.js/javascript.zig +++ b/src/bun.js/javascript.zig @@ -21,7 +21,6 @@ const IdentityContext = @import("../identity_context.zig").IdentityContext; const Fs = @import("../fs.zig"); const Resolver = @import("../resolver/resolver.zig"); const ast = @import("../import_record.zig"); -const NodeModuleBundle = @import("../node_module_bundle.zig").NodeModuleBundle; const MacroEntryPoint = bun.bundler.MacroEntryPoint; const ParseResult = bun.bundler.ParseResult; const logger = @import("root").bun.logger; @@ -383,7 +382,6 @@ pub const VirtualMachine = struct { global: *JSGlobalObject, allocator: std.mem.Allocator, has_loaded_constructors: bool = false, - node_modules: ?*NodeModuleBundle = null, bundler: Bundler, bun_dev_watcher: ?*http.Watcher = null, bun_watcher: ?*JSC.Watcher = null, @@ -398,7 +396,6 @@ pub const VirtualMachine = struct { entry_point: ServerEntryPoint = undefined, origin: URL = URL{}, node_fs: ?*Node.NodeFS = null, - has_loaded_node_modules: bool = false, timer: Bun.Timer = Bun.Timer{}, uws_event_loop: ?*uws.Loop = null, pending_unref_counter: i32 = 0, @@ -833,18 +830,17 @@ pub const VirtualMachine = struct { } const RuntimeTranspilerStore = JSC.RuntimeTranspilerStore; pub fn initWithModuleGraph( - allocator: std.mem.Allocator, - log: *logger.Log, - graph: *bun.StandaloneModuleGraph, + opts: Options, ) !*VirtualMachine { + const allocator = opts.allocator; VMHolder.vm = try allocator.create(VirtualMachine); var console = try allocator.create(ZigConsoleClient); console.* = ZigConsoleClient.init(Output.errorWriter(), Output.writer()); + var log = opts.log.?; const bundler = try Bundler.init( allocator, log, - std.mem.zeroes(Api.TransformOptions), - null, + opts.args, null, ); var vm = VMHolder.vm.?; @@ -857,7 +853,6 @@ pub const VirtualMachine = struct { .event_listeners = EventListenerMixin.Map.init(allocator), .bundler = bundler, .console = console, - .node_modules = bundler.options.node_modules_bundle, .log = log, .flush_list = std.ArrayList(string).init(allocator), .blobs = null, @@ -871,7 +866,7 @@ pub const VirtualMachine = struct { .ref_strings = JSC.RefString.Map.init(allocator), .ref_strings_mutex = Lock.init(), .file_blobs = JSC.WebCore.Blob.Store.Map.init(allocator), - .standalone_module_graph = graph, + .standalone_module_graph = opts.graph.?, .parser_arena = @import("root").bun.ArenaAllocator.init(allocator), }; vm.source_mappings = .{ .map = &vm.saved_source_map_table }; @@ -892,7 +887,7 @@ pub const VirtualMachine = struct { .onDependencyError = JSC.ModuleLoader.AsyncModule.Queue.onDependencyError, }; - vm.bundler.resolver.standalone_module_graph = graph; + vm.bundler.resolver.standalone_module_graph = opts.graph.?; // Avoid reading from tsconfig.json & package.json when we're in standalone mode vm.bundler.configureLinkerWithAutoJSX(false); @@ -925,17 +920,20 @@ pub const VirtualMachine = struct { return vm; } - pub fn init( + pub const Options = struct { allocator: std.mem.Allocator, - _args: Api.TransformOptions, - existing_bundle: ?*NodeModuleBundle, - _log: ?*logger.Log, - env_loader: ?*DotEnv.Loader, - store_fd: bool, - smol: bool, - ) !*VirtualMachine { + args: Api.TransformOptions = std.mem.zeroes(Api.TransformOptions), + log: ?*logger.Log = null, + env_loader: ?*DotEnv.Loader = null, + store_fd: bool = false, + smol: bool = false, + graph: ?*bun.StandaloneModuleGraph = null, + }; + + pub fn init(opts: Options) !*VirtualMachine { + const allocator = opts.allocator; var log: *logger.Log = undefined; - if (_log) |__log| { + if (opts.log) |__log| { log = __log; } else { log = try allocator.create(logger.Log); @@ -948,9 +946,8 @@ pub const VirtualMachine = struct { const bundler = try Bundler.init( allocator, log, - try Config.configureTransformOptionsForBunVM(allocator, _args), - existing_bundle, - env_loader, + try Config.configureTransformOptionsForBunVM(allocator, opts.args), + opts.env_loader, ); var vm = VMHolder.vm.?; @@ -962,10 +959,9 @@ pub const VirtualMachine = struct { .event_listeners = EventListenerMixin.Map.init(allocator), .bundler = bundler, .console = console, - .node_modules = bundler.options.node_modules_bundle, .log = log, .flush_list = std.ArrayList(string).init(allocator), - .blobs = if (_args.serve orelse false) try Blob.Group.init(allocator) else null, + .blobs = if (opts.args.serve orelse false) try Blob.Group.init(allocator) else null, .origin = bundler.options.origin, .saved_source_map_table = SavedSourceMap.HashTable.init(allocator), .source_mappings = undefined, @@ -987,7 +983,7 @@ pub const VirtualMachine = struct { vm.event_loop = &vm.regular_event_loop; vm.bundler.macro_context = null; - vm.bundler.resolver.store_fd = store_fd; + vm.bundler.resolver.store_fd = opts.store_fd; vm.bundler.resolver.prefer_module_field = false; vm.bundler.resolver.onWakePackageManager = .{ @@ -1001,7 +997,7 @@ pub const VirtualMachine = struct { vm.bundler.macro_context = js_ast.Macro.MacroContext.init(&vm.bundler); - if (_args.serve orelse false) { + if (opts.args.serve orelse false) { vm.bundler.linker.onImportCSS = Bun.onImportCSS; } @@ -1014,7 +1010,7 @@ pub const VirtualMachine = struct { @as(i32, @intCast(global_classes.len)), vm.console, -1, - smol, + opts.smol, null, ); vm.regular_event_loop.global = vm.global; @@ -1031,15 +1027,12 @@ pub const VirtualMachine = struct { } pub fn initWorker( - allocator: std.mem.Allocator, - _args: Api.TransformOptions, - _log: ?*logger.Log, - env_loader: ?*DotEnv.Loader, - store_fd: bool, worker: *WebWorker, + opts: Options, ) anyerror!*VirtualMachine { var log: *logger.Log = undefined; - if (_log) |__log| { + const allocator = opts.allocator; + if (opts.log) |__log| { log = __log; } else { log = try allocator.create(logger.Log); @@ -1052,9 +1045,8 @@ pub const VirtualMachine = struct { const bundler = try Bundler.init( allocator, log, - try Config.configureTransformOptionsForBunVM(allocator, _args), - null, - env_loader, + try Config.configureTransformOptionsForBunVM(allocator, opts.args), + opts.env_loader, ); var vm = VMHolder.vm.?; @@ -1066,10 +1058,9 @@ pub const VirtualMachine = struct { .event_listeners = EventListenerMixin.Map.init(allocator), .bundler = bundler, .console = console, - .node_modules = bundler.options.node_modules_bundle, .log = log, .flush_list = std.ArrayList(string).init(allocator), - .blobs = if (_args.serve orelse false) try Blob.Group.init(allocator) else null, + .blobs = if (opts.args.serve orelse false) try Blob.Group.init(allocator) else null, .origin = bundler.options.origin, .saved_source_map_table = SavedSourceMap.HashTable.init(allocator), .source_mappings = undefined, @@ -1093,7 +1084,7 @@ pub const VirtualMachine = struct { vm.event_loop = &vm.regular_event_loop; vm.hot_reload = worker.parent.hot_reload; vm.bundler.macro_context = null; - vm.bundler.resolver.store_fd = store_fd; + vm.bundler.resolver.store_fd = opts.store_fd; vm.bundler.resolver.prefer_module_field = false; vm.bundler.resolver.onWakePackageManager = .{ .context = &vm.modules, @@ -1106,7 +1097,7 @@ pub const VirtualMachine = struct { vm.bundler.macro_context = js_ast.Macro.MacroContext.init(&vm.bundler); - if (_args.serve orelse false) { + if (opts.args.serve orelse false) { vm.bundler.linker.onImportCSS = Bun.onImportCSS; } @@ -1286,7 +1277,6 @@ pub const VirtualMachine = struct { source: string, is_esm: bool, comptime is_a_file_path: bool, - comptime realpath: bool, ) !void { std.debug.assert(VirtualMachine.isLoaded()); // macOS threadlocal vars are very slow @@ -1294,12 +1284,9 @@ pub const VirtualMachine = struct { // so we can copy it here var jsc_vm = VirtualMachine.get(); - if (jsc_vm.node_modules == null and strings.eqlComptime(std.fs.path.basename(specifier), Runtime.Runtime.Imports.alt_name)) { + if (strings.eqlComptime(std.fs.path.basename(specifier), Runtime.Runtime.Imports.alt_name)) { ret.path = Runtime.Runtime.Imports.Name; return; - } else if (jsc_vm.node_modules != null and strings.eqlComptime(specifier, bun_file_import_path)) { - ret.path = bun_file_import_path; - return; } else if (strings.eqlComptime(specifier, main_file_name)) { ret.result = null; ret.path = jsc_vm.entry_point.source.path.text; @@ -1383,49 +1370,6 @@ pub const VirtualMachine = struct { ret.query_string = query_string; const result_path = result.pathConst() orelse return error.ModuleNotFound; jsc_vm.resolved_count += 1; - if (comptime !realpath) { - if (jsc_vm.node_modules != null and !strings.eqlComptime(result_path.namespace, "node") and result.isLikelyNodeModule()) { - const node_modules_bundle = jsc_vm.node_modules.?; - - node_module_checker: { - const package_json = result.package_json orelse brk: { - if (jsc_vm.bundler.resolver.packageJSONForResolvedNodeModule(&result)) |pkg| { - break :brk pkg; - } else { - break :node_module_checker; - } - }; - - if (node_modules_bundle.getPackageIDByName(package_json.name)) |possible_pkg_ids| { - const pkg_id: u32 = brk: { - for (possible_pkg_ids) |pkg_id| { - const pkg = node_modules_bundle.bundle.packages[pkg_id]; - if (pkg.hash == package_json.hash) { - break :brk pkg_id; - } - } - break :node_module_checker; - }; - - const package = &node_modules_bundle.bundle.packages[pkg_id]; - - if (Environment.isDebug) { - std.debug.assert(strings.eql(node_modules_bundle.str(package.name), package_json.name)); - } - - const package_relative_path = jsc_vm.bundler.fs.relative( - package_json.source.path.name.dirWithTrailingSlash(), - result_path.text, - ); - - if (node_modules_bundle.findModuleIDInPackage(package, package_relative_path) == null) break :node_module_checker; - - ret.path = bun_file_import_path; - return; - } - } - } - } ret.path = result_path.text; } @@ -1452,7 +1396,7 @@ pub const VirtualMachine = struct { query_string: *ZigString, is_esm: bool, ) void { - resolveMaybeNeedsTrailingSlash(res, global, specifier, source, query_string, is_esm, false, true); + resolveMaybeNeedsTrailingSlash(res, global, specifier, source, query_string, is_esm, false); } pub fn resolveFilePathForAPI( @@ -1463,7 +1407,7 @@ pub const VirtualMachine = struct { query_string: *ZigString, is_esm: bool, ) void { - resolveMaybeNeedsTrailingSlash(res, global, specifier, source, query_string, is_esm, true, true); + resolveMaybeNeedsTrailingSlash(res, global, specifier, source, query_string, is_esm, true); } pub fn resolve( @@ -1474,7 +1418,7 @@ pub const VirtualMachine = struct { query_string: *ZigString, is_esm: bool, ) void { - resolveMaybeNeedsTrailingSlash(res, global, specifier, source, query_string, is_esm, true, false); + resolveMaybeNeedsTrailingSlash(res, global, specifier, source, query_string, is_esm, true); } fn normalizeSource(source: []const u8) []const u8 { @@ -1493,7 +1437,6 @@ pub const VirtualMachine = struct { query_string: ?*ZigString, is_esm: bool, comptime is_a_file_path: bool, - comptime realpath: bool, ) void { var result = ResolveFunctionResult{ .path = "", .result = null }; var jsc_vm = VirtualMachine.get(); @@ -1527,7 +1470,6 @@ pub const VirtualMachine = struct { query_string, is_esm, is_a_file_path, - realpath, ); return; } @@ -1547,7 +1489,7 @@ pub const VirtualMachine = struct { jsc_vm.bundler.linker.log = old_log; jsc_vm.bundler.resolver.log = old_log; } - _resolve(&result, global, specifier_utf8.slice(), normalizeSource(source_utf8.slice()), is_esm, is_a_file_path, realpath) catch |err_| { + _resolve(&result, global, specifier_utf8.slice(), normalizeSource(source_utf8.slice()), is_esm, is_a_file_path) catch |err_| { var err = err_; const msg: logger.Msg = brk: { var msgs: []logger.Msg = log.msgs.items; @@ -1808,19 +1750,6 @@ pub const VirtualMachine = struct { var promise: *JSInternalPromise = undefined; if (!this.bundler.options.disable_transpilation) { - - // We first import the node_modules bundle. This prevents any potential TDZ issues. - // The contents of the node_modules bundle are lazy, so hopefully this should be pretty quick. - if (this.node_modules != null and !this.has_loaded_node_modules) { - this.has_loaded_node_modules = true; - promise = JSModuleLoader.loadAndEvaluateModule(this.global, &String.static(bun_file_import_path)); - this.waitForPromise(JSC.AnyPromise{ - .Internal = promise, - }); - if (promise.status(this.global.vm()) == .Rejected) - return promise; - } - { this.is_in_preload = true; defer this.is_in_preload = false; diff --git a/src/bun.js/module_loader.zig b/src/bun.js/module_loader.zig index ccee5208d..5d9158b58 100644 --- a/src/bun.js/module_loader.zig +++ b/src/bun.js/module_loader.zig @@ -20,7 +20,6 @@ const IdentityContext = @import("../identity_context.zig").IdentityContext; const Fs = @import("../fs.zig"); const Resolver = @import("../resolver/resolver.zig"); const ast = @import("../import_record.zig"); -const NodeModuleBundle = @import("../node_module_bundle.zig").NodeModuleBundle; const MacroEntryPoint = bun.bundler.MacroEntryPoint; const ParseResult = bun.bundler.ParseResult; const logger = @import("root").bun.logger; @@ -1994,23 +1993,7 @@ pub const ModuleLoader = struct { } pub fn fetchBuiltinModule(jsc_vm: *VirtualMachine, specifier: bun.String) !?ResolvedSource { - if (jsc_vm.node_modules != null and specifier.eqlComptime(JSC.bun_file_import_path)) { - // We kind of need an abstraction around this. - // Basically we should subclass JSC::SourceCode with: - // - hash - // - file descriptor for source input - // - file path + file descriptor for bytecode caching - // - separate bundles for server build vs browser build OR at least separate sections - const code = try jsc_vm.node_modules.?.readCodeAsStringSlow(jsc_vm.allocator); - - return ResolvedSource{ - .allocator = null, - .source_code = bun.String.init(code), - .specifier = bun.String.init(JSC.bun_file_import_path), - .source_url = ZigString.init(JSC.bun_file_import_path[1..]), - .hash = 0, // TODO - }; - } else if (jsc_vm.node_modules == null and specifier.eqlComptime(Runtime.Runtime.Imports.Name)) { + if (specifier.eqlComptime(Runtime.Runtime.Imports.Name)) { return ResolvedSource{ .allocator = null, .source_code = bun.String.init(Runtime.Runtime.sourceContentBun()), diff --git a/src/bun.js/web_worker.zig b/src/bun.js/web_worker.zig index bbe708d18..7fa9c6690 100644 --- a/src/bun.js/web_worker.zig +++ b/src/bun.js/web_worker.zig @@ -170,14 +170,11 @@ pub const WebWorker = struct { std.debug.assert(this.status == .start); std.debug.assert(this.vm == null); this.arena = try bun.MimallocArena.init(); - var vm = try JSC.VirtualMachine.initWorker( - this.arena.allocator(), - this.parent.bundler.options.transform_options, - null, - null, - this.store_fd, - this, - ); + var vm = try JSC.VirtualMachine.initWorker(this, .{ + .allocator = this.arena.allocator(), + .args = this.parent.bundler.options.transform_options, + .store_fd = this.store_fd, + }); vm.allocator = this.arena.allocator(); vm.arena = &this.arena; diff --git a/src/bun_js.zig b/src/bun_js.zig index 6bbf093ae..82a5a8612 100644 --- a/src/bun_js.zig +++ b/src/bun_js.zig @@ -25,7 +25,6 @@ const resolve_path = @import("./resolver/resolve_path.zig"); const configureTransformOptionsForBun = @import("./bun.js/config.zig").configureTransformOptionsForBun; const Command = @import("cli.zig").Command; const bundler = bun.bundler; -const NodeModuleBundle = @import("node_module_bundle.zig").NodeModuleBundle; const DotEnv = @import("env_loader.zig"); const which = @import("which.zig").which; const JSC = @import("root").bun.JSC; @@ -60,7 +59,11 @@ pub const Run = struct { } run = .{ - .vm = try VirtualMachine.initWithModuleGraph(arena.allocator(), ctx.log, graph_ptr), + .vm = try VirtualMachine.initWithModuleGraph(.{ + .allocator = arena.allocator(), + .log = ctx.log, + .graph = graph_ptr, + }), .arena = arena, .ctx = ctx, .entry_path = entry_path, @@ -144,13 +147,13 @@ pub const Run = struct { run = .{ .vm = try VirtualMachine.init( - arena.allocator(), - ctx.args, - null, - ctx.log, - null, - ctx.debug.hot_reload != .none, - ctx.runtime_options.smol, + .{ + .allocator = arena.allocator(), + .log = ctx.log, + .args = ctx.args, + .store_fd = ctx.debug.hot_reload != .none, + .smol = ctx.runtime_options.smol, + }, ), .arena = arena, .ctx = ctx, diff --git a/src/bundler.zig b/src/bundler.zig index b3e62a43d..b5912a96f 100644 --- a/src/bundler.zig +++ b/src/bundler.zig @@ -37,7 +37,6 @@ const runtime = @import("./runtime.zig"); const PackageJSON = @import("./resolver/package_json.zig").PackageJSON; const MacroRemap = @import("./resolver/package_json.zig").MacroMap; const DebugLogs = _resolver.DebugLogs; -const NodeModuleBundle = @import("./node_module_bundle.zig").NodeModuleBundle; const Router = @import("./router.zig"); const isPackagePath = _resolver.isPackagePath; const Css = @import("css_scanner.zig"); @@ -68,54 +67,6 @@ const default_macro_js_value = if (JSC.is_bindgen) MacroJSValueType{} else JSC.J const EntryPoints = @import("./bundler/entry_points.zig"); const SystemTimer = @import("./system_timer.zig").Timer; pub usingnamespace EntryPoints; -// How it works end-to-end -// 1. Resolve a file path from input using the resolver -// 2. Look at the extension of that file path, and determine a loader -// 3. If the loader is .js, .jsx, .ts, .tsx, or .json, run it through our JavaScript Parser -// IF serving via HTTP and it's parsed without errors: -// 4. If parsed without errors, generate a strong ETag & write the output to a buffer that sends to the in the Printer. -// 4. Else, write any errors to error page (which doesn't exist yet) -// IF writing to disk AND it's parsed without errors: -// 4. Write the output to a temporary file. -// Why? Two reasons. -// 1. At this point, we don't know what the best output path is. -// Most of the time, you want the shortest common path, which you can't know until you've -// built & resolved all paths. -// Consider this directory tree: -// - /Users/jarred/Code/app/src/index.tsx -// - /Users/jarred/Code/app/src/Button.tsx -// - /Users/jarred/Code/app/assets/logo.png -// - /Users/jarred/Code/app/src/Button.css -// - /Users/jarred/Code/app/node_modules/react/index.js -// - /Users/jarred/Code/app/node_modules/react/cjs/react.development.js -// Remember that we cannot know which paths need to be resolved without parsing the JavaScript. -// If we stopped here: /Users/jarred/Code/app/src/Button.tsx -// We would choose /Users/jarred/Code/app/src/ as the directory -// Then, that would result in a directory structure like this: -// - /Users/jarred/Code/app/src/Users/jarred/Code/app/node_modules/react/cjs/react.development.js -// Which is absolutely insane -// -// 2. We will need to write to disk at some point! -// - If we delay writing to disk, we need to print & allocate a potentially quite large -// buffer (react-dom.development.js is 550 KB) -// ^ This is how it used to work! -// - If we delay printing, we need to keep the AST around. Which breaks all our -// memory-saving recycling logic since that could be many many ASTs. -// 5. Once all files are written, determine the shortest common path -// 6. Move all the temporary files to their intended destinations -// IF writing to disk AND it's a file-like loader -// 4. Hash the contents -// - rewrite_paths.put(absolute_path, hash(file(absolute_path))) -// 5. Resolve any imports of this file to that hash(file(absolute_path)) -// 6. Append to the files array with the new filename -// 7. When parsing & resolving is over, just copy the file. -// - on macOS, ensure it does an APFS shallow clone so that doesn't use disk space (only possible if file doesn't already exist) -// fclonefile -// IF serving via HTTP AND it's a file-like loader: -// 4. Use os.sendfile so copying/reading the file happens in the kernel instead of in bun. -// This unfortunately means content hashing for HTTP server is unsupported, but metadata etags work -// For each imported file, GOTO 1. - pub const ParseResult = struct { source: logger.Source, loader: options.Loader, @@ -420,7 +371,6 @@ pub const Bundler = struct { allocator: std.mem.Allocator, log: *logger.Log, opts: Api.TransformOptions, - existing_bundle: ?*NodeModuleBundle, env_loader_: ?*DotEnv.Loader, ) !Bundler { js_ast.Expr.Data.Store.create(allocator); @@ -433,7 +383,6 @@ pub const Bundler = struct { fs, log, opts, - existing_bundle, ); var env_loader: *DotEnv.Loader = env_loader_ orelse DotEnv.instance orelse brk: { @@ -869,7 +818,7 @@ pub const Bundler = struct { &result.source, Writer, writer, - .cjs_ascii, + .cjs, is_source_map, source_map_handler, ), @@ -1181,27 +1130,7 @@ pub const Bundler = struct { enable_source_map, ), }, - .cjs_ascii => try js_printer.printCommonJS( - Writer, - writer, - ast, - js_ast.Symbol.Map.initList(symbols), - source, - true, - js_printer.Options{ - .externals = ast.externals, - .runtime_imports = ast.runtime_imports, - .require_ref = ast.require_ref, - .css_import_behavior = bundler.options.cssImportBehavior(), - .source_map_handler = source_map_context, - .minify_whitespace = bundler.options.minify_whitespace, - .minify_syntax = bundler.options.minify_syntax, - .minify_identifiers = bundler.options.minify_identifiers, - .transform_only = bundler.options.transform_only, - .module_type = if (ast.exports_kind == .cjs) .cjs else .esm, - }, - enable_source_map, - ), + else => unreachable, }; } @@ -1371,7 +1300,6 @@ pub const Bundler = struct { jsx.parse = loader.isJSX(); var opts = js_parser.Parser.Options.init(jsx, loader); - opts.enable_legacy_bundling = false; opts.legacy_transform_require_to_import = bundler.options.allow_runtime and !bundler.options.target.isBun(); opts.features.allow_runtime = bundler.options.allow_runtime; opts.features.trim_unused_imports = bundler.options.trim_unused_imports orelse loader.isTypeScript(); @@ -1385,19 +1313,9 @@ pub const Bundler = struct { opts.features.commonjs_at_runtime = this_parse.allow_commonjs; - opts.can_import_from_bundle = bundler.options.node_modules_bundle != null; - opts.tree_shaking = bundler.options.tree_shaking; opts.features.inlining = bundler.options.inlining; - // HMR is enabled when devserver is running - // unless you've explicitly disabled it - // or you're running in SSR - // or the file is a node_module - opts.features.hot_module_reloading = bundler.options.hot_module_reloading and - target.isNotBun() and - (!opts.can_import_from_bundle or - (opts.can_import_from_bundle and !path.isNodeModule())); opts.features.react_fast_refresh = opts.features.hot_module_reloading and jsx.parse and bundler.options.jsx.supports_fast_refresh; diff --git a/src/bundler/bundle_v2.zig b/src/bundler/bundle_v2.zig index db1f57c8a..b682b622f 100644 --- a/src/bundler/bundle_v2.zig +++ b/src/bundler/bundle_v2.zig @@ -89,7 +89,7 @@ const Timer = @import("../system_timer.zig"); const PackageJSON = @import("../resolver/package_json.zig").PackageJSON; const MacroRemap = @import("../resolver/package_json.zig").MacroMap; const DebugLogs = _resolver.DebugLogs; -const NodeModuleBundle = @import("../node_module_bundle.zig").NodeModuleBundle; + const Router = @import("../router.zig"); const isPackagePath = _resolver.isPackagePath; const Lock = @import("../lock.zig").Lock; @@ -1609,7 +1609,6 @@ pub const BundleV2 = struct { .main_fields = &.{}, .extension_order = &.{}, }, - null, completion.env, ); bundler.options.jsx = config.jsx; @@ -2586,7 +2585,6 @@ pub const ParseTask = struct { var opts = js_parser.Parser.Options.init(task.jsx, loader); opts.legacy_transform_require_to_import = false; - opts.can_import_from_bundle = false; opts.features.allow_runtime = !source.index.isRuntime(); opts.features.dynamic_require = target.isBun(); opts.warn_about_unbundled_modules = false; diff --git a/src/bunfig.zig b/src/bunfig.zig index 050c603de..5594b570c 100644 --- a/src/bunfig.zig +++ b/src/bunfig.zig @@ -502,11 +502,6 @@ pub const Bunfig = struct { if (json.get("bundle")) |_bun| { if (comptime cmd == .DevCommand or cmd == .BuildCommand or cmd == .RunCommand or cmd == .AutoCommand or cmd == .BuildCommand) { - if (_bun.get("saveTo")) |file| { - try this.expect(file, .e_string); - this.bunfig.node_modules_bundle_path = try file.data.e_string.string(allocator); - } - if (_bun.get("outdir")) |dir| { try this.expect(dir, .e_string); this.bunfig.output_dir = try dir.data.e_string.string(allocator); diff --git a/src/cli.zig b/src/cli.zig index 3400a70f4..1f88f3f97 100644 --- a/src/cli.zig +++ b/src/cli.zig @@ -35,8 +35,6 @@ const DotEnv = @import("./env_loader.zig"); const fs = @import("fs.zig"); const Router = @import("./router.zig"); -const NodeModuleBundle = @import("./node_module_bundle.zig").NodeModuleBundle; - const MacroMap = @import("./resolver/package_json.zig").MacroMap; const TestCommand = @import("./cli/test_command.zig").TestCommand; const Reporter = @import("./report.zig"); @@ -188,8 +186,6 @@ pub const Arguments = struct { pub const dev_params = [_]ParamType{ clap.parseParam("--disable-bun.js Disable bun.js from loading in the dev server") catch unreachable, clap.parseParam("--disable-react-fast-refresh Disable React Fast Refresh") catch unreachable, - clap.parseParam("--bunfile <STR> Use a .bun file (default: node_modules.bun)") catch unreachable, - clap.parseParam("--server-bunfile <STR> Use a .server.bun file (default: node_modules.server.bun)") catch unreachable, clap.parseParam("--public-dir <STR> Top-level directory for .html files, fonts or anything external. Defaults to \"<cwd>/public\", to match create-react-app and Next.js") catch unreachable, clap.parseParam("--disable-hmr Disable Hot Module Reloading (disables fast refresh too) in bun dev") catch unreachable, clap.parseParam("--use <STR> Choose a framework, e.g. \"--use next\". It checks first for a package named \"bun-framework-packagename\" and then \"packagename\".") catch unreachable, @@ -523,8 +519,7 @@ pub const Arguments = struct { const print_help = args.flag("--help"); if (print_help) { - const params_len = if (cmd == .BuildCommand) build_params_public.len else public_params.len; - clap.help(Output.writer(), params_to_use[0..params_len]) catch {}; + clap.help(Output.writer(), params_to_use[0..params_to_use.len]) catch {}; Output.prettyln("\n-------\n\n", .{}); Output.flush(); HelpCommand.printWithReason(.explicit); @@ -682,18 +677,6 @@ pub const Arguments = struct { } } - opts.node_modules_bundle_path = args.option("--bunfile") orelse opts.node_modules_bundle_path orelse brk: { - const node_modules_bundle_path_absolute = resolve_path.joinAbs(cwd, .auto, "node_modules.bun"); - - break :brk std.fs.realpathAlloc(allocator, node_modules_bundle_path_absolute) catch null; - }; - - opts.node_modules_bundle_path_server = args.option("--server-bunfile") orelse opts.node_modules_bundle_path_server orelse brk: { - const node_modules_bundle_path_absolute = resolve_path.joinAbs(cwd, .auto, "node_modules.server.bun"); - - break :brk std.fs.realpathAlloc(allocator, node_modules_bundle_path_absolute) catch null; - }; - if (args.option("--use")) |entry| { opts.framework = Api.FrameworkConfig{ .package = entry, @@ -786,7 +769,7 @@ pub const Arguments = struct { } if (cmd == .BuildCommand) { - if (opts.entry_points.len == 0 and opts.framework == null and opts.node_modules_bundle_path == null) { + if (opts.entry_points.len == 0 and opts.framework == null) { return error.MissingEntryPoint; } } @@ -919,33 +902,6 @@ pub const HelpCommand = struct { const AddCompletions = @import("./cli/add_completions.zig"); -pub const PrintBundleCommand = struct { - pub fn exec(ctx: Command.Context) !void { - @setCold(true); - - const entry_point = ctx.args.entry_points[0]; - var out_buffer: [bun.MAX_PATH_BYTES]u8 = undefined; - var stdout = std.io.getStdOut(); - - var input = try std.fs.openFileAbsolute(try std.os.realpath(entry_point, &out_buffer), .{ .mode = .read_only }); - const params = comptime [_]Arguments.ParamType{ - clap.parseParam("--summary Peek inside the .bun") catch unreachable, - }; - - var jsBundleArgs = clap.parse(clap.Help, ¶ms, .{ .allocator = ctx.allocator }) catch { - try NodeModuleBundle.printBundle(std.fs.File, input, @TypeOf(stdout), stdout); - return; - }; - - if (jsBundleArgs.flag("--summary")) { - NodeModuleBundle.printSummaryFromDisk(std.fs.File, input, @TypeOf(stdout), stdout, ctx.allocator) catch {}; - return; - } - - try NodeModuleBundle.printBundle(std.fs.File, input, @TypeOf(stdout), stdout); - } -}; - pub const Command = struct { var script_name_buf: [bun.MAX_PATH_BYTES]u8 = undefined; @@ -1456,11 +1412,6 @@ pub const Command = struct { @as([]const u8, ""); // KEYWORDS: open file argv argv0 if (ctx.args.entry_points.len == 1) { - if (strings.eqlComptime(extension, ".bun")) { - try PrintBundleCommand.exec(ctx); - return; - } - if (strings.eqlComptime(extension, ".lockb")) { for (std.os.argv) |arg| { if (strings.eqlComptime(std.mem.span(arg), "--hash")) { diff --git a/src/cli/build_command.zig b/src/cli/build_command.zig index bb4eb4313..f87d88930 100644 --- a/src/cli/build_command.zig +++ b/src/cli/build_command.zig @@ -26,7 +26,7 @@ const Api = @import("../api/schema.zig").Api; const resolve_path = @import("../resolver/resolve_path.zig"); const configureTransformOptionsForBun = @import("../bun.js/config.zig").configureTransformOptionsForBun; const bundler = bun.bundler; -const NodeModuleBundle = @import("../node_module_bundle.zig").NodeModuleBundle; + const DotEnv = @import("../env_loader.zig"); const fs = @import("../fs.zig"); @@ -48,7 +48,7 @@ pub const BuildCommand = struct { ctx.args.target = .bun; } - var this_bundler = try bundler.Bundler.init(allocator, log, ctx.args, null, null); + var this_bundler = try bundler.Bundler.init(allocator, log, ctx.args, null); this_bundler.options.source_map = options.SourceMapOption.fromApi(ctx.args.source_map); this_bundler.resolver.opts.source_map = options.SourceMapOption.fromApi(ctx.args.source_map); @@ -200,9 +200,6 @@ pub const BuildCommand = struct { router.config.static_dir_enabled = false; this_bundler.router = null; } - this_bundler.options.node_modules_bundle = null; - this_bundler.options.node_modules_bundle_pretty_path = ""; - this_bundler.options.node_modules_bundle_url = ""; }; this_bundler.options.jsx.development = !this_bundler.options.production; diff --git a/src/cli/create_command.zig b/src/cli/create_command.zig index aff34e9b4..544329a98 100644 --- a/src/cli/create_command.zig +++ b/src/cli/create_command.zig @@ -26,7 +26,7 @@ const resolve_path = @import("../resolver/resolve_path.zig"); const configureTransformOptionsForBun = @import("../bun.js/config.zig").configureTransformOptionsForBun; const Command = @import("../cli.zig").Command; const bundler = bun.bundler; -const NodeModuleBundle = @import("../node_module_bundle.zig").NodeModuleBundle; + const fs = @import("../fs.zig"); const URL = @import("../url.zig").URL; const HTTP = @import("root").bun.HTTP; diff --git a/src/cli/install_completions_command.zig b/src/cli/install_completions_command.zig index b51c75d60..7e19d12f1 100644 --- a/src/cli/install_completions_command.zig +++ b/src/cli/install_completions_command.zig @@ -25,7 +25,7 @@ const resolve_path = @import("../resolver/resolve_path.zig"); const configureTransformOptionsForBun = @import("../bun.js/config.zig").configureTransformOptionsForBun; const Command = @import("../cli.zig").Command; const bundler = bun.bundler; -const NodeModuleBundle = @import("../node_module_bundle.zig").NodeModuleBundle; + const fs = @import("../fs.zig"); const URL = @import("../url.zig").URL; const ParseJSON = @import("../json_parser.zig").ParseJSON; diff --git a/src/cli/run_command.zig b/src/cli/run_command.zig index b445ccdc4..ba05e1ddf 100644 --- a/src/cli/run_command.zig +++ b/src/cli/run_command.zig @@ -26,7 +26,7 @@ const resolve_path = @import("../resolver/resolve_path.zig"); const configureTransformOptionsForBun = @import("../bun.js/config.zig").configureTransformOptionsForBun; const Command = @import("../cli.zig").Command; const bundler = bun.bundler; -const NodeModuleBundle = @import("../node_module_bundle.zig").NodeModuleBundle; + const DotEnv = @import("../env_loader.zig"); const which = @import("../which.zig").which; const Run = @import("../bun_js.zig").Run; @@ -385,11 +385,8 @@ pub const RunCommand = struct { pub fn ls(ctx: Command.Context) !void { var args = ctx.args; - args.node_modules_bundle_path = null; - args.node_modules_bundle_path_server = null; - args.generate_node_module_bundle = false; - var this_bundler = try bundler.Bundler.init(ctx.allocator, ctx.log, args, null, null); + var this_bundler = try bundler.Bundler.init(ctx.allocator, ctx.log, args, null); this_bundler.options.env.behavior = Api.DotEnvBehavior.load_all; this_bundler.options.env.prefix = ""; @@ -471,10 +468,7 @@ pub const RunCommand = struct { force_using_bun: bool, ) !*DirInfo { var args = ctx.args; - args.node_modules_bundle_path = null; - args.node_modules_bundle_path_server = null; - args.generate_node_module_bundle = false; - this_bundler.* = try bundler.Bundler.init(ctx.allocator, ctx.log, args, null, env); + this_bundler.* = try bundler.Bundler.init(ctx.allocator, ctx.log, args, env); this_bundler.options.env.behavior = Api.DotEnvBehavior.load_all; this_bundler.env.quiet = true; this_bundler.options.env.prefix = ""; @@ -661,11 +655,8 @@ pub const RunCommand = struct { } var args = ctx.args; - args.node_modules_bundle_path = null; - args.node_modules_bundle_path_server = null; - args.generate_node_module_bundle = false; - var this_bundler = bundler.Bundler.init(ctx.allocator, ctx.log, args, null, null) catch return shell_out; + var this_bundler = bundler.Bundler.init(ctx.allocator, ctx.log, args, null) catch return shell_out; this_bundler.options.env.behavior = Api.DotEnvBehavior.load_all; this_bundler.options.env.prefix = ""; this_bundler.env.quiet = true; diff --git a/src/cli/test_command.zig b/src/cli/test_command.zig index 3a9411e08..fe9611d85 100644 --- a/src/cli/test_command.zig +++ b/src/cli/test_command.zig @@ -27,8 +27,7 @@ const Api = @import("../api/schema.zig").Api; const resolve_path = @import("../resolver/resolve_path.zig"); const configureTransformOptionsForBun = @import("../bun.js/config.zig").configureTransformOptionsForBun; const Command = @import("../cli.zig").Command; -const bundler = bun.bundler; -const NodeModuleBundle = @import("../node_module_bundle.zig").NodeModuleBundle; + const DotEnv = @import("../env_loader.zig"); const which = @import("../which.zig").which; const Run = @import("../bun_js.zig").Run; @@ -625,18 +624,19 @@ pub const TestCommand = struct { js_ast.Expr.Data.Store.create(default_allocator); js_ast.Stmt.Data.Store.create(default_allocator); var vm = try JSC.VirtualMachine.init( - ctx.allocator, - ctx.args, - null, - ctx.log, - env_loader, - // we must store file descriptors because we reuse them for - // iterating through the directory tree recursively - // - // in the future we should investigate if refactoring this to not - // rely on the dir fd yields a performance improvement - true, - ctx.runtime_options.smol, + .{ + .allocator = ctx.allocator, + .args = ctx.args, + .log = ctx.log, + .env_loader = env_loader, + // we must store file descriptors because we reuse them for + // iterating through the directory tree recursively + // + // in the future we should investigate if refactoring this to not + // rely on the dir fd yields a performance improvement + .store_fd = true, + .smol = ctx.runtime_options.smol, + }, ); vm.argv = ctx.passthrough; vm.preload = ctx.preloads; diff --git a/src/cli/upgrade_command.zig b/src/cli/upgrade_command.zig index b8bfd3f73..3fadfe5c2 100644 --- a/src/cli/upgrade_command.zig +++ b/src/cli/upgrade_command.zig @@ -25,7 +25,7 @@ const resolve_path = @import("../resolver/resolve_path.zig"); const configureTransformOptionsForBun = @import("../bun.js/config.zig").configureTransformOptionsForBun; const Command = @import("../cli.zig").Command; const bundler = bun.bundler; -const NodeModuleBundle = @import("../node_module_bundle.zig").NodeModuleBundle; + const fs = @import("../fs.zig"); const URL = @import("../url.zig").URL; const HTTP = @import("root").bun.HTTP; diff --git a/src/http.zig b/src/http.zig index 61cf99095..37a7158bb 100644 --- a/src/http.zig +++ b/src/http.zig @@ -27,7 +27,6 @@ const ErrorCSS = @import("./runtime.zig").ErrorCSS; const ErrorJS = @import("./runtime.zig").ErrorJS; const Runtime = @import("./runtime.zig").Runtime; const Css = @import("css_scanner.zig"); -const NodeModuleBundle = @import("./node_module_bundle.zig").NodeModuleBundle; const resolve_path = @import("./resolver/resolve_path.zig"); const OutputFile = Options.OutputFile; const DotEnv = @import("./env_loader.zig"); @@ -344,14 +343,13 @@ pub const RequestContext = struct { @as(?*bundler.FallbackEntryPoint, &fallback_entry_point), ); if (tmp) |*result| { - try bundler_.linker.linkAllowImportingFromBundle( + try bundler_.linker.link( fallback_entry_point.source.path, result, this.origin, .absolute_url, false, false, - false, ); var buffer_writer = try JSPrinter.BufferWriter.init(default_allocator); @@ -841,66 +839,6 @@ pub const RequestContext = struct { ctx.done(); } - pub fn sendJSB(ctx: *RequestContext) !void { - const node_modules_bundle = ctx.bundler.options.node_modules_bundle orelse unreachable; - if (ctx.header("Open-In-Editor") != null) { - if (http_editor_context.editor == null) { - http_editor_context.detectEditor(ctx.bundler.env); - } - - if (http_editor_context.editor.? != .none) { - var buf: string = ""; - - if (node_modules_bundle.code_string == null) { - buf = try node_modules_bundle.readCodeAsStringSlow(bun.default_allocator); - } else { - buf = node_modules_bundle.code_string.?.str; - } - - http_editor_context.openInEditor( - http_editor_context.editor.?, - buf, - std.fs.path.basename(ctx.url.path), - ctx.bundler.fs.tmpdir(), - ctx.header("Editor-Line") orelse "", - ctx.header("Editor-Column") orelse "", - ); - - if (http_editor_context.editor.? != .none) { - try ctx.sendNoContent(); - return; - } - } - } - - ctx.appendHeader("ETag", node_modules_bundle.bundle.etag); - ctx.appendHeader("Content-Type", "text/javascript"); - ctx.appendHeader("Cache-Control", "immutable, max-age=99999"); - - if (ctx.header("If-None-Match")) |etag_header| { - if (strings.eqlLong(node_modules_bundle.bundle.etag, etag_header, true)) { - try ctx.sendNotModified(); - return; - } - } - - defer ctx.done(); - - const content_length = node_modules_bundle.container.code_length.? - node_modules_bundle.codeStartOffset(); - try ctx.writeStatus(200); - try ctx.prepareToSendBody(content_length, false); - - _ = try std.os.sendfile( - ctx.conn.handle, - node_modules_bundle.fd, - node_modules_bundle.codeStartOffset(), - content_length, - &[_]std.os.iovec_const{}, - &[_]std.os.iovec_const{}, - 0, - ); - } - pub fn sendSinglePageHTML(ctx: *RequestContext) !void { std.debug.assert(ctx.bundler.options.routes.single_page_app_fd > 0); const file = std.fs.File{ .handle = ctx.bundler.options.routes.single_page_app_fd }; @@ -1205,7 +1143,6 @@ pub const RequestContext = struct { pub const HandlerThread = struct { args: Api.TransformOptions, framework: Options.Framework, - existing_bundle: ?*NodeModuleBundle, log: *logger.Log = undefined, watcher: *Watcher, env_loader: *DotEnv.Loader, @@ -1465,15 +1402,13 @@ pub const RequestContext = struct { js_ast.Stmt.Data.Store.create(bun.default_allocator); js_ast.Expr.Data.Store.create(bun.default_allocator); - var vm: *JavaScript.VirtualMachine = JavaScript.VirtualMachine.init( - bun.default_allocator, - handler.args, - null, - handler.log, - handler.env_loader, - true, - false, - ) catch |err| { + var vm: *JavaScript.VirtualMachine = JavaScript.VirtualMachine.init(.{ + .allocator = bun.default_allocator, + .args = handler.args, + .log = handler.log, + .env_loader = handler.env_loader, + .store_fd = true, + }) catch |err| { handler.handleJSError(.create_vm, err) catch {}; javascript_disabled = true; return; @@ -1627,32 +1562,16 @@ pub const RequestContext = struct { has_loaded_channel = true; channel = Channel.init(); - var transform_options = server.transform_options; - if (server.transform_options.node_modules_bundle_path_server) |bundle_path| { - transform_options.node_modules_bundle_path = bundle_path; - transform_options.node_modules_bundle_path_server = null; - handler_thread.* = HandlerThread{ - .args = transform_options, - .framework = server.bundler.options.framework.?, - .existing_bundle = null, - .log = undefined, - .watcher = server.watcher, - .env_loader = server.bundler.env, - .origin = server.bundler.options.origin, - .client_bundler = undefined, - }; - } else { - handler_thread.* = HandlerThread{ - .args = server.transform_options, - .framework = server.bundler.options.framework.?, - .existing_bundle = server.bundler.options.node_modules_bundle, - .watcher = server.watcher, - .env_loader = server.bundler.env, - .log = undefined, - .origin = server.bundler.options.origin, - .client_bundler = undefined, - }; - } + + handler_thread.* = HandlerThread{ + .args = server.transform_options, + .framework = server.bundler.options.framework.?, + .watcher = server.watcher, + .env_loader = server.bundler.env, + .log = undefined, + .origin = server.bundler.options.origin, + .client_bundler = undefined, + }; try server.bundler.clone(server.allocator, &handler_thread.client_bundler); handler_thread.log = try server.allocator.create(logger.Log); handler_thread.log.* = logger.Log.init(server.allocator); @@ -2698,14 +2617,6 @@ pub const RequestContext = struct { if (vm.blobs.?.get(id)) |blob| { break :brk blob; } - - if (strings.eqlComptime(id, "node_modules.server.bun")) { - if (vm.node_modules) |_bun| { - if (_bun.code_string) |code| { - break :brk Blob{ .ptr = code.str.ptr, .len = code.str.len }; - } - } - } } if (JavaScript.VirtualMachine.isLoaded()) { @@ -2713,14 +2624,6 @@ pub const RequestContext = struct { if (vm.blobs.?.get(id)) |blob| { break :brk blob; } - - if (strings.eqlComptime(id, "node_modules.server.bun")) { - if (vm.node_modules) |_bun| { - if (_bun.code_string) |code| { - break :brk Blob{ .ptr = code.str.ptr, .len = code.str.len }; - } - } - } } return try ctx.sendNotFound(); @@ -3080,11 +2983,6 @@ pub const RequestContext = struct { } pub fn handleReservedRoutes(ctx: *RequestContext, server: *Server) !bool { - if (strings.eqlComptime(ctx.url.extname, "bun") and ctx.bundler.options.node_modules_bundle != null) { - try ctx.sendJSB(); - return true; - } - if (strings.hasPrefixComptime(ctx.url.path, "blob:")) { try ctx.handleBlobURL(server); return true; @@ -3566,10 +3464,8 @@ pub const Server = struct { Output.flush(); - Analytics.Features.bun_bun = server.bundler.options.node_modules_bundle != null; Analytics.Features.framework = server.bundler.options.framework != null; Analytics.Features.filesystem_router = server.bundler.router != null; - Analytics.Features.bunjs = server.transform_options.node_modules_bundle_path_server != null; const UpgradeCheckerThread = @import("./cli/upgrade_command.zig").UpgradeCheckerThread; @@ -3904,15 +3800,6 @@ pub const Server = struct { defer this.bundler.resetStore(); - const runtime = this.bundler.options.jsx.refresh_runtime; - - // If there's a .bun, don't even read the filesystem - // Just use the .bun - if (this.bundler.options.node_modules_bundle) |node_modules_bundle| { - const package_name = runtime[0 .. strings.indexOfChar(runtime, '/') orelse runtime.len]; - if (node_modules_bundle.getPackageIDByName(package_name) != null) return; - } - _ = this.bundler.resolver.resolve(this.bundler.fs.top_level_dir, this.bundler.options.jsx.importSource(), .internal) catch { // if they don't have React, they can't use fast refresh this.bundler.options.jsx.supports_fast_refresh = false; @@ -3955,7 +3842,7 @@ pub const Server = struct { }; global_start_time = server.timer; server.bundler = try allocator.create(Bundler); - server.bundler.* = try Bundler.init(allocator, &server.log, options, null, null); + server.bundler.* = try Bundler.init(allocator, &server.log, options, null); server.bundler.configureLinker(); try server.bundler.configureRouter(true); Server.current = server; diff --git a/src/import_record.zig b/src/import_record.zig index d0df147db..8fd29e805 100644 --- a/src/import_record.zig +++ b/src/import_record.zig @@ -111,10 +111,6 @@ pub const ImportRecord = struct { calls_runtime_require: bool = false, - /// This tells the printer that we should print as export var $moduleID = ... - /// Instead of using the path. - is_legacy_bundled: bool = false, - /// Sometimes the parser creates an import record and decides it isn't needed. /// For example, TypeScript code may have import statements that later turn /// out to be type-only imports after analyzing the whole file. diff --git a/src/install/install.zig b/src/install/install.zig index 38f2ae11f..d444b62fc 100644 --- a/src/install/install.zig +++ b/src/install/install.zig @@ -26,7 +26,7 @@ const configureTransformOptionsForBun = @import("../bun.js/config.zig").configur const Command = @import("../cli.zig").Command; const BunArguments = @import("../cli.zig").Arguments; const bundler = bun.bundler; -const NodeModuleBundle = @import("../node_module_bundle.zig").NodeModuleBundle; + const DotEnv = @import("../env_loader.zig"); const which = @import("../which.zig").which; const Run = @import("../bun_js.zig").Run; diff --git a/src/install/lockfile.zig b/src/install/lockfile.zig index e293b2213..8eb411739 100644 --- a/src/install/lockfile.zig +++ b/src/install/lockfile.zig @@ -29,7 +29,7 @@ const configureTransformOptionsForBun = @import("../bun.js/config.zig").configur const Command = @import("../cli.zig").Command; const BunArguments = @import("../cli.zig").Arguments; const bundler = bun.bundler; -const NodeModuleBundle = @import("../node_module_bundle.zig").NodeModuleBundle; + const DotEnv = @import("../env_loader.zig"); const which = @import("../which.zig").which; const Run = @import("../bun_js.zig").Run; diff --git a/src/js_ast.zig b/src/js_ast.zig index 2682c7f2b..19e64336b 100644 --- a/src/js_ast.zig +++ b/src/js_ast.zig @@ -9630,18 +9630,13 @@ pub const Macro = struct { JavaScript.VirtualMachine.get() else brk: { var old_transform_options = resolver.opts.transform_options; - resolver.opts.transform_options.node_modules_bundle_path = null; - resolver.opts.transform_options.node_modules_bundle_path_server = null; defer resolver.opts.transform_options = old_transform_options; - var _vm = try JavaScript.VirtualMachine.init( - default_allocator, - resolver.opts.transform_options, - null, - log, - env, - false, - false, - ); + var _vm = try JavaScript.VirtualMachine.init(.{ + .allocator = default_allocator, + .args = resolver.opts.transform_options, + .log = log, + .env_loader = env, + }); _vm.enableMacroMode(); _vm.eventLoop().ensureWaker(); diff --git a/src/js_parser.zig b/src/js_parser.zig index 3e6143b18..0ba935c98 100644 --- a/src/js_parser.zig +++ b/src/js_parser.zig @@ -906,7 +906,7 @@ pub const ImportScanner = struct { } const namespace_ref = st.namespace_ref; - const convert_star_to_clause = !p.options.bundle and (!p.options.enable_legacy_bundling and !p.options.can_import_from_bundle and p.symbols.items[namespace_ref.innerIndex()].use_count_estimate == 0); + const convert_star_to_clause = !p.options.bundle and (p.symbols.items[namespace_ref.innerIndex()].use_count_estimate == 0); if (convert_star_to_clause and !keep_unused_imports) { st.star_name_loc = null; @@ -2618,7 +2618,6 @@ pub const Parser = struct { pub const Options = struct { jsx: options.JSX.Pragma, - can_import_from_bundle: bool = false, ts: bool = false, keep_names: bool = true, omit_runtime_for_tests: bool = false, @@ -2637,8 +2636,6 @@ pub const Parser = struct { warn_about_unbundled_modules: bool = true, - // Used when bundling node_modules - enable_legacy_bundling: bool = false, legacy_transform_require_to_import: bool = true, module_type: options.ModuleType = .unknown, @@ -3454,7 +3451,7 @@ pub const Parser = struct { .bun_js = {}, }; } else if (!p.options.bundle and !p.options.features.commonjs_at_runtime and (!p.options.transform_only or p.options.features.dynamic_require)) { - if (p.options.legacy_transform_require_to_import or (p.options.features.dynamic_require and !p.options.enable_legacy_bundling)) { + if (p.options.legacy_transform_require_to_import or p.options.features.dynamic_require) { var args = p.allocator.alloc(Expr, 2) catch unreachable; if (p.runtime_imports.__exportDefault == null and p.has_export_default) { @@ -3593,648 +3590,6 @@ pub const Parser = struct { }) catch unreachable; } - // Auto-import & post-process JSX - // if (!p.options.bundle) { - // switch (comptime ParserType.jsx_transform_type) { - // .react => { - // // const jsx_filename_symbol = if (p.options.jsx.development) - // // p.symbols.items[p.jsx_filename.ref.innerIndex()] - // // else - // // Symbol{ .original_name = "" }; - - // { - // const jsx_symbol = p.symbols.items[p.jsx_runtime.ref.innerIndex()]; - - // const jsx_fragment_symbol = p.symbols.items[p.jsx_fragment.ref.innerIndex()]; - // const jsx_factory_symbol = p.symbols.items[p.jsx_factory.ref.innerIndex()]; - - // // Currently, React (and most node_modules) ship a CJS version or a UMD version - // // but we should assume that it'll pretty much always be CJS - // // Given that, we can't directly call import {jsxDEV} from 'react'; - // // Instead, we must call require("react").default.jsxDEV - // // So a jsx_symbol usage means a jsx_factory_symbol usage - // // This is kind of a broken way of doing it because it wouldn't work if it was more than one level deep - // if (FeatureFlags.jsx_runtime_is_cjs) { - // if (jsx_symbol.use_count_estimate > 0) { - // p.recordUsage(p.jsx_automatic.ref); - // } - - // if (FeatureFlags.support_jsxs_in_jsx_transform) { - // const jsx_static_symbol = p.symbols.items[p.jsxs_runtime.ref.innerIndex()]; - - // if (jsx_static_symbol.use_count_estimate > 0) { - // p.recordUsage(p.jsx_automatic.ref); - // } - // } - - // if (jsx_fragment_symbol.use_count_estimate > 0) { - // p.recordUsage(p.jsx_classic.ref); - // } - - // if (jsx_factory_symbol.use_count_estimate > 0) { - // p.recordUsage(p.jsx_classic.ref); - // } - // } - // } - - // p.resolveStaticJSXSymbols(); - - // if (p.options.features.auto_import_jsx) { - // const jsx_classic_symbol = p.symbols.items[p.jsx_classic.ref.innerIndex()]; - // const jsx_automatic_symbol = p.symbols.items[p.jsx_automatic.ref.innerIndex()]; - // const react_element_symbol = if (p.options.features.jsx_optimization_inline) p.symbols.items[p.react_element_type.ref.innerIndex()] else Symbol{ - // .original_name = "IF_YOU_SEE_THIS_ITS_A_BUG_IN_BUN_WHERE_REACT_ELEMENT_SYMBOL_IS_BEING_ADDED_WHEN_IT_SHOULDNT_BE_PLEASE_REPORT_IT", - // }; - - // // JSX auto-imports - // // The classic runtime is a different import than the main import - // // There are cases where you can use both JSX runtimes in the same file. - // // 1. If you use a spread operator like this: <div foo bar key="foo" {...props} baz /> - // // 2. If you use a React.Fragment - // // So we have to support both. - // if (jsx_classic_symbol.use_count_estimate > 0 or jsx_automatic_symbol.use_count_estimate > 0 or react_element_symbol.use_count_estimate > 0) { - // // These must unfortunately be copied - // // p.symbols may grow during this scope - // // if it grows, the previous pointers are invalidated - // const jsx_symbol = p.symbols.items[p.jsx_runtime.ref.innerIndex()]; - // const jsx_static_symbol: Symbol = if (!FeatureFlags.support_jsxs_in_jsx_transform) - // undefined - // else - // p.symbols.items[p.jsxs_runtime.ref.innerIndex()]; - - // const jsx_fragment_symbol = p.symbols.items[p.jsx_fragment.ref.innerIndex()]; - // const jsx_factory_symbol = p.symbols.items[p.jsx_factory.ref.innerIndex()]; - - // const classic_namespace_ref = p.jsx_classic.ref; - // const automatic_namespace_ref = p.jsx_automatic.ref; - - // const decls_count: u32 = - // // "REACT_ELEMENT_TYPE" - // // "Symbol.for('react.element')" - // @intCast(u32, @intFromBool(react_element_symbol.use_count_estimate > 0)) * 2 + - - // // "JSX" - // @intCast(u32, @intFromBool(jsx_symbol.use_count_estimate > 0)) * 2 + - // @intCast(u32, @intFromBool(FeatureFlags.support_jsxs_in_jsx_transform and jsx_static_symbol.use_count_estimate > 0)) * 2 + - // @intCast(u32, @intFromBool(jsx_factory_symbol.use_count_estimate > 0)) + - // @intCast(u32, @intFromBool(jsx_fragment_symbol.use_count_estimate > 0)); - // // @intCast(u32, @intFromBool(jsx_filename_symbol.use_count_estimate > 0)); - - // const imports_count = - // @intCast(u32, @intFromBool(jsx_symbol.use_count_estimate > 0)) + - // @intCast(u32, @intFromBool(jsx_classic_symbol.use_count_estimate > 0)) + - // @intCast(u32, @intFromBool(jsx_fragment_symbol.use_count_estimate > 0)) + - // @intCast(u32, @intFromBool(p.options.features.react_fast_refresh)) + - // @intCast(u32, @intFromBool(FeatureFlags.support_jsxs_in_jsx_transform and jsx_static_symbol.use_count_estimate > 0)); - // const stmts_count = imports_count + 1; - // const symbols_count: u32 = imports_count + decls_count; - // const loc = logger.Loc{ .start = 0 }; - - // // Preallocate everything we'll need here - // var declared_symbols = DeclaredSymbol.List{}; - // try declared_symbols.ensureTotalCapacity(p.allocator, symbols_count); - // var decls = try p.allocator.alloc(G.Decl, decls_count); - // var jsx_part_stmts = try p.allocator.alloc(Stmt, stmts_count); - // // Use the same array for storing the require call target of potentially both JSX runtimes - // var require_call_args_base = p.allocator.alloc(Expr, if (p.options.can_import_from_bundle) 0 else imports_count) catch unreachable; - // var import_records = try p.allocator.alloc(u32, imports_count); - - // var decl_i: usize = 0; - // var import_record_i: usize = 0; - // var require_call_args_i: usize = 0; - // var stmt_i: usize = 0; - - // if (react_element_symbol.use_count_estimate > 0) { - // declared_symbols.appendAssumeCapacity(.{ .ref = p.react_element_type.ref, .is_top_level = true }); - // p.recordUsage(p.es6_symbol_global.ref); - // var call_args = p.allocator.alloc(Expr, 1) catch unreachable; - // call_args[0] = Expr{ .data = Prefill.Data.REACT_ELEMENT_TYPE, .loc = logger.Loc.Empty }; - - // decls[decl_i] = G.Decl{ - // .binding = p.b( - // B.Identifier{ - // .ref = p.react_element_type.ref, - // }, - // loc, - // ), - // .value = p.newExpr( - // E.Call{ - // // Symbol.for - // .target = p.newExpr( - // E.Dot{ - // .name = "for", - // .name_loc = logger.Loc.Empty, - // .target = p.newExpr( - // E.Identifier{ - // .ref = p.es6_symbol_global.ref, - // .can_be_removed_if_unused = true, - // .call_can_be_unwrapped_if_unused = true, - // }, - // logger.Loc.Empty, - // ), - // .can_be_removed_if_unused = true, - // .call_can_be_unwrapped_if_unused = true, - // }, - // logger.Loc.Empty, - // ), - // .args = ExprNodeList.init(call_args), - // .close_paren_loc = logger.Loc.Empty, - // .can_be_unwrapped_if_unused = true, - // }, - // logger.Loc.Empty, - // ), - // }; - // decl_i += 1; - // } - - // if (jsx_symbol.use_count_estimate > 0 or (FeatureFlags.support_jsxs_in_jsx_transform and jsx_static_symbol.use_count_estimate > 0)) { - // declared_symbols.appendAssumeCapacity(.{ .ref = automatic_namespace_ref, .is_top_level = true }); - - // const automatic_identifier = p.newExpr(E.ImportIdentifier{ .ref = automatic_namespace_ref }, loc); - - // // We do not mark this as .require becuase we are already wrapping it manually. - // // unless it's bun and you're not bundling - // const use_automatic_identifier = (p.options.can_import_from_bundle or p.options.enable_legacy_bundling or !p.options.features.allow_runtime); - // const import_record_kind = if (use_automatic_identifier) ImportKind.internal else ImportKind.require; - // const import_record_id = p.addImportRecord(import_record_kind, loc, p.options.jsx.import_source); - - // const dot_call_target = brk: { - // if (use_automatic_identifier) { - // break :brk automatic_identifier; - // } else if (p.options.features.dynamic_require) { - // break :brk p.newExpr(E.RequireString{ .import_record_index = import_record_id }, loc); - // } else { - // require_call_args_base[require_call_args_i] = automatic_identifier; - // require_call_args_i += 1; - // break :brk p.callUnbundledRequire(require_call_args_base[0..require_call_args_i]); - // } - // }; - - // if (jsx_symbol.use_count_estimate > 0) { - // declared_symbols.appendAssumeCapacity(.{ .ref = p.jsx_runtime.ref, .is_top_level = true }); - - // decls[decl_i] = G.Decl{ - // .binding = p.b( - // B.Identifier{ - // .ref = p.jsx_runtime.ref, - // }, - // loc, - // ), - // .value = p.newExpr( - // E.Dot{ - // .target = dot_call_target, - // .name = p.options.jsx.jsx, - // .name_loc = loc, - // .can_be_removed_if_unused = true, - // }, - // loc, - // ), - // }; - // decl_i += 1; - // } - - // if (FeatureFlags.support_jsxs_in_jsx_transform) { - // if (jsx_static_symbol.use_count_estimate > 0) { - // declared_symbols.appendAssumeCapacity(.{ .ref = p.jsxs_runtime.ref, .is_top_level = true }); - - // decls[decl_i] = G.Decl{ - // .binding = p.b( - // B.Identifier{ - // .ref = p.jsxs_runtime.ref, - // }, - // loc, - // ), - // .value = p.newExpr( - // E.Dot{ - // .target = dot_call_target, - // .name = p.options.jsx.jsx_static, - // .name_loc = loc, - // .can_be_removed_if_unused = true, - // }, - // loc, - // ), - // }; - - // decl_i += 1; - // } - // } - // // if (jsx_filename_symbol.use_count_estimate > 0) { - // // declared_symbols.appendAssumeCapacity(.{ .ref = p.jsx_filename.ref, .is_top_level = true }); - // // decls[decl_i] = G.Decl{ - // // .binding = p.b( - // // B.Identifier{ - // // .ref = p.jsx_filename.ref, - // // }, - // // loc, - // // ), - // // .value = p.newExpr(E.String{ .data = p.source.path.pretty }, loc), - // // }; - // // decl_i += 1; - // // } - - // p.import_records.items[import_record_id].tag = .jsx_import; - // if (dot_call_target.data != .e_require_string) { - // // When everything is CommonJS - // // We import JSX like this: - // // var {jsxDev} = require("react/jsx-dev") - // jsx_part_stmts[stmt_i] = p.s(S.Import{ - // .namespace_ref = automatic_namespace_ref, - // .star_name_loc = loc, - // .is_single_line = true, - // .import_record_index = import_record_id, - // }, loc); - - // stmt_i += 1; - // } - - // p.named_imports.put( - // automatic_namespace_ref, - // js_ast.NamedImport{ - // .alias = jsx_automatic_symbol.original_name, - // .alias_is_star = true, - // .alias_loc = loc, - // .namespace_ref = automatic_namespace_ref, - // .import_record_index = import_record_id, - // }, - // ) catch unreachable; - // p.is_import_item.put(p.allocator, automatic_namespace_ref, {}) catch unreachable; - // import_records[import_record_i] = import_record_id; - // import_record_i += 1; - // } - - // if (jsx_classic_symbol.use_count_estimate > 0) { - // const classic_identifier = p.newExpr(E.ImportIdentifier{ .ref = classic_namespace_ref }, loc); - // const import_record_id = p.addImportRecord(.require, loc, p.options.jsx.classic_import_source); - // const dot_call_target = brk: { - // // var react = $aopaSD123(); - - // if (p.options.can_import_from_bundle or p.options.enable_legacy_bundling or !p.options.features.allow_runtime) { - // break :brk classic_identifier; - // } else if (p.options.features.dynamic_require) { - // break :brk p.newExpr(E.RequireString{ .import_record_index = import_record_id }, loc); - // } else { - // const require_call_args_start = require_call_args_i; - // require_call_args_base[require_call_args_i] = classic_identifier; - // require_call_args_i += 1; - // break :brk p.callUnbundledRequire(require_call_args_base[require_call_args_start..][0..1]); - // } - // }; - - // if (jsx_factory_symbol.use_count_estimate > 0) { - // declared_symbols.appendAssumeCapacity(.{ .ref = p.jsx_factory.ref, .is_top_level = true }); - // decls[decl_i] = G.Decl{ - // .binding = p.b( - // B.Identifier{ - // .ref = p.jsx_factory.ref, - // }, - // loc, - // ), - // .value = p.memberExpression( - // loc, - // dot_call_target, - // if (p.options.jsx.factory.len > 1) p.options.jsx.factory[1..] else p.options.jsx.factory, - // ), - // }; - // decl_i += 1; - // } - - // if (jsx_fragment_symbol.use_count_estimate > 0) { - // declared_symbols.appendAssumeCapacity(.{ .ref = p.jsx_fragment.ref, .is_top_level = true }); - // decls[decl_i] = G.Decl{ - // .binding = p.b( - // B.Identifier{ - // .ref = p.jsx_fragment.ref, - // }, - // loc, - // ), - // .value = p.memberExpression( - // loc, - // dot_call_target, - // if (p.options.jsx.fragment.len > 1) p.options.jsx.fragment[1..] else p.options.jsx.fragment, - // ), - // }; - // decl_i += 1; - // } - - // if (dot_call_target.data != .e_require_string) { - // jsx_part_stmts[stmt_i] = p.s(S.Import{ - // .namespace_ref = classic_namespace_ref, - // .star_name_loc = loc, - // .is_single_line = true, - // .import_record_index = import_record_id, - // }, loc); - // stmt_i += 1; - // } - - // p.import_records.items[import_record_id].tag = .jsx_classic; - - // p.named_imports.put( - // classic_namespace_ref, - // js_ast.NamedImport{ - // .alias = jsx_classic_symbol.original_name, - // .alias_is_star = true, - // .alias_loc = loc, - // .namespace_ref = classic_namespace_ref, - // .import_record_index = import_record_id, - // }, - // ) catch unreachable; - // p.is_import_item.put(p.allocator, classic_namespace_ref, {}) catch unreachable; - // import_records[import_record_i] = import_record_id; - // declared_symbols.appendAssumeCapacity(.{ .ref = classic_namespace_ref, .is_top_level = true }); - // } - - // if (p.options.features.react_fast_refresh) { - // defer did_import_fast_refresh = true; - // p.resolveGeneratedSymbol(&p.jsx_refresh_runtime); - // if (!p.options.jsx.use_embedded_refresh_runtime) { - // const refresh_runtime_symbol: *const Symbol = &p.symbols.items[p.jsx_refresh_runtime.ref.innerIndex()]; - - // declared_symbols.appendAssumeCapacity(.{ .ref = p.jsx_refresh_runtime.ref, .is_top_level = true }); - - // const import_record_id = p.addImportRecord(.require, loc, p.options.jsx.refresh_runtime); - // p.import_records.items[import_record_id].tag = .react_refresh; - // jsx_part_stmts[stmt_i] = p.s(S.Import{ - // .namespace_ref = p.jsx_refresh_runtime.ref, - // .star_name_loc = loc, - // .is_single_line = true, - // .import_record_index = import_record_id, - // }, loc); - - // stmt_i += 1; - // p.named_imports.put( - // p.jsx_refresh_runtime.ref, - // js_ast.NamedImport{ - // .alias = refresh_runtime_symbol.original_name, - // .alias_is_star = true, - // .alias_loc = loc, - // .namespace_ref = p.jsx_refresh_runtime.ref, - // .import_record_index = import_record_id, - // }, - // ) catch unreachable; - // p.is_import_item.put(p.allocator, p.jsx_refresh_runtime.ref, {}) catch unreachable; - // import_records[import_record_i] = import_record_id; - // } - // p.recordUsage(p.jsx_refresh_runtime.ref); - // } - - // jsx_part_stmts[stmt_i] = p.s(S.Local{ .kind = .k_var, .decls = decls[0..decl_i] }, loc); - // stmt_i += 1; - - // before.append(js_ast.Part{ - // .stmts = jsx_part_stmts[0..stmt_i], - // .declared_symbols = declared_symbols, - // .import_record_indices = bun.BabyList(u32).init(import_records), - // .tag = .jsx_import, - // }) catch unreachable; - // } - // } else if (p.options.features.jsx_optimization_inline) { - // const react_element_symbol = p.symbols.items[p.react_element_type.ref.innerIndex()]; - - // if (react_element_symbol.use_count_estimate > 0) { - // var declared_symbols = DeclaredSymbol.List{}; - // try declared_symbols.ensureTotalCapacity(p.allocator, 1); - // var decls = try p.allocator.alloc(G.Decl, 1); - // var part_stmts = try p.allocator.alloc(Stmt, 1); - - // declared_symbols.appendAssumeCapacity(.{ .ref = p.react_element_type.ref, .is_top_level = true }); - // p.recordUsage(p.es6_symbol_global.ref); - // var call_args = p.allocator.alloc(Expr, 1) catch unreachable; - // call_args[0] = Expr{ .data = Prefill.Data.REACT_ELEMENT_TYPE, .loc = logger.Loc.Empty }; - - // decls[0] = G.Decl{ - // .binding = p.b( - // B.Identifier{ - // .ref = p.react_element_type.ref, - // }, - // logger.Loc.Empty, - // ), - // .value = p.newExpr( - // E.Call{ - // // Symbol.for - // .target = p.newExpr( - // E.Dot{ - // .name = "for", - // .name_loc = logger.Loc.Empty, - // .target = p.newExpr( - // E.Identifier{ - // .ref = p.es6_symbol_global.ref, - // .can_be_removed_if_unused = true, - // .call_can_be_unwrapped_if_unused = true, - // }, - // logger.Loc.Empty, - // ), - // .can_be_removed_if_unused = true, - // .call_can_be_unwrapped_if_unused = true, - // }, - // logger.Loc.Empty, - // ), - // .args = ExprNodeList.init(call_args), - // .close_paren_loc = logger.Loc.Empty, - // .can_be_unwrapped_if_unused = true, - // }, - // logger.Loc.Empty, - // ), - // }; - // part_stmts[0] = p.s(S.Local{ .kind = .k_var, .decls = decls }, logger.Loc.Empty); - // before.append(js_ast.Part{ - // .stmts = part_stmts, - // .declared_symbols = declared_symbols, - // .tag = .jsx_import, - // }) catch unreachable; - // } - // } else { - // const jsx_fragment_symbol: Symbol = p.symbols.items[p.jsx_fragment.ref.innerIndex()]; - // const jsx_factory_symbol: Symbol = p.symbols.items[p.jsx_factory.ref.innerIndex()]; - - // // inject - // // var jsxFrag = - // if (jsx_fragment_symbol.use_count_estimate + jsx_factory_symbol.use_count_estimate > 0) { - // const total = @as(usize, @intFromBool(jsx_fragment_symbol.use_count_estimate > 0)) + @as(usize, @intFromBool(jsx_factory_symbol.use_count_estimate > 0)); - // var declared_symbols = DeclaredSymbol.List{}; - // try declared_symbols.ensureTotalCapacity(p.allocator, total); - // var decls = try std.ArrayList(G.Decl).initCapacity(p.allocator, total); - // var part_stmts = try p.allocator.alloc(Stmt, 1); - - // if (jsx_fragment_symbol.use_count_estimate > 0) declared_symbols.appendAssumeCapacity(.{ .ref = p.jsx_fragment.ref, .is_top_level = true }); - // if (jsx_factory_symbol.use_count_estimate > 0) declared_symbols.appendAssumeCapacity(.{ .ref = p.jsx_factory.ref, .is_top_level = true }); - - // if (jsx_fragment_symbol.use_count_estimate > 0) - // decls.appendAssumeCapacity(G.Decl{ - // .binding = p.b( - // B.Identifier{ - // .ref = p.jsx_fragment.ref, - // }, - // logger.Loc.Empty, - // ), - // .value = try p.jsxStringsToMemberExpression(logger.Loc.Empty, p.options.jsx.fragment), - // }); - - // if (jsx_factory_symbol.use_count_estimate > 0) - // decls.appendAssumeCapacity(G.Decl{ - // .binding = p.b( - // B.Identifier{ - // .ref = p.jsx_factory.ref, - // }, - // logger.Loc.Empty, - // ), - // .value = try p.jsxStringsToMemberExpression(logger.Loc.Empty, p.options.jsx.factory), - // }); - // part_stmts[0] = p.s(S.Local{ .kind = .k_var, .decls = decls.items }, logger.Loc.Empty); - // before.append(js_ast.Part{ - // .stmts = part_stmts, - // .declared_symbols = declared_symbols, - // .tag = .jsx_import, - // }) catch unreachable; - // } - // } - - // if (!did_import_fast_refresh and p.options.features.react_fast_refresh) { - // p.resolveGeneratedSymbol(&p.jsx_refresh_runtime); - // p.recordUsage(p.jsx_refresh_runtime.ref); - - // if (!p.options.jsx.use_embedded_refresh_runtime) { - // if (comptime Environment.allow_assert) - // assert(!p.options.enable_legacy_bundling); - // var declared_symbols = DeclaredSymbol.List{}; - // try declared_symbols.ensureTotalCapacity(p.allocator, 1); - // const loc = logger.Loc.Empty; - // const import_record_id = p.addImportRecord(.require, loc, p.options.jsx.refresh_runtime); - // p.import_records.items[import_record_id].tag = .react_refresh; - - // var import_stmt = p.s(S.Import{ - // .namespace_ref = p.jsx_refresh_runtime.ref, - // .star_name_loc = loc, - // .is_single_line = true, - // .import_record_index = import_record_id, - // }, loc); - - // const refresh_runtime_symbol: *const Symbol = &p.symbols.items[p.jsx_refresh_runtime.ref.innerIndex()]; - - // p.named_imports.put( - // p.jsx_refresh_runtime.ref, - // js_ast.NamedImport{ - // .alias = refresh_runtime_symbol.original_name, - // .alias_is_star = true, - // .alias_loc = loc, - // .namespace_ref = p.jsx_refresh_runtime.ref, - // .import_record_index = import_record_id, - // }, - // ) catch unreachable; - // p.is_import_item.put(p.allocator, p.jsx_refresh_runtime.ref, {}) catch unreachable; - // var import_records = try p.allocator.alloc(@TypeOf(import_record_id), 1); - // import_records[0] = import_record_id; - // declared_symbols.appendAssumeCapacity(.{ .ref = p.jsx_refresh_runtime.ref, .is_top_level = true }); - // var part_stmts = try p.allocator.alloc(Stmt, 1); - // part_stmts[0] = import_stmt; - - // before.append(js_ast.Part{ - // .stmts = part_stmts, - // .declared_symbols = declared_symbols, - // .import_record_indices = bun.BabyList(u32).init(import_records), - // .tag = .react_fast_refresh, - // }) catch unreachable; - // } - // } - // }, - // else => {}, - // } - - // if (p.options.enable_legacy_bundling) p.resolveBundlingSymbols(); - // } - - // if (!p.options.bundle) { - // // "did they actually use require?" - // // well, if they didn't, in the linker later, we might need to inject it - // // but we don't know what name we can use there - // // so instead, we pessimistically assume they did in fact use require _somewhere_ - // // and we set the name to something that won't conflict. - // // however, at this stage, we don't want to inject the import statement for the require - // // since it won't be actually used yet. - // const had_require = p.runtime_imports.contains("__require"); - // p.resolveCommonJSSymbols(); - - // const copy_of_runtime_require = p.runtime_imports.__require; - // if (!had_require) { - // p.runtime_imports.__require = null; - // } - // defer { - // if (!had_require) { - // p.runtime_imports.__require = copy_of_runtime_require; - // } - // } - - // // - don't import runtime if we're bundling, it's already included - // // - when HMR is enabled, we always need to import the runtime for HMRClient and HMRModule. - // // - when HMR is not enabled, we only need any runtime imports if we're importing require() - // if (p.options.features.allow_runtime and - // !p.options.enable_legacy_bundling and - // (p.has_called_runtime or p.options.features.hot_module_reloading or has_cjs_imports)) - // { - // var runtime_imports_iter = p.runtime_imports.iter(); - - // const before_start = before.items.len; - // if (p.options.features.hot_module_reloading) { - // p.resolveHMRSymbols(); - - // if (runtime_imports_iter.next()) |entry| { - // std.debug.assert(entry.key == 0); - - // // HMRClient.activate(true) - // var args_list: []Expr = if (Environment.isDebug) &Prefill.HotModuleReloading.DebugEnabledArgs else &Prefill.HotModuleReloading.DebugDisabled; - - // var hmr_module_class_ident = p.newExpr(E.Identifier{ .ref = p.runtime_imports.__HMRClient.?.ref }, logger.Loc.Empty); - // const imports = [_]u16{entry.key}; - // // TODO: remove these unnecessary allocations - // p.generateImportStmt( - // RuntimeImports.Name, - // &imports, - // &before, - // p.runtime_imports, - // p.s( - // S.SExpr{ - // .value = p.newExpr(E.Call{ - // .target = p.newExpr(E.Dot{ - // .target = hmr_module_class_ident, - // .name = "activate", - // .name_loc = logger.Loc.Empty, - // }, logger.Loc.Empty), - // .args = ExprNodeList.init(args_list), - // }, logger.Loc.Empty), - // }, - // logger.Loc.Empty, - // ), - // "import_", - // true, - // ) catch unreachable; - // } - // } - - // while (runtime_imports_iter.next()) |entry| { - // const imports = [_]u16{entry.key}; - // // TODO: remove these unnecessary allocations - // p.generateImportStmt( - // RuntimeImports.Name, - // &imports, - // &before, - // p.runtime_imports, - // null, - // "import_", - // true, - // ) catch unreachable; - // } - // // If we import JSX, we might call require. - // // We need to import require before importing JSX. - // // But a runtime import may not be necessary until we import JSX. - // // So we have to swap it after the fact, instead of just moving this above the JSX import. - // if (before_start > 0) { - // var j: usize = 0; - // while (j < before_start) : (j += 1) { - // std.mem.swap(js_ast.Part, &before.items[j], &before.items[before.items.len - j - 1]); - // } - // } - // } - // } - if (p.legacy_cjs_import_stmts.items.len > 0 and p.options.legacy_transform_require_to_import) { var import_records = try bun.BabyList(u32).initCapacity(p.allocator, p.legacy_cjs_import_stmts.items.len); var declared_symbols = DeclaredSymbol.List{}; @@ -21121,10 +20476,6 @@ fn NewParser_( } } - // if (p.options.tree_shaking) { - // p.treeShake(&parts, commonjs_wrapper_expr != null or p.options.features.hot_module_reloading or p.options.enable_legacy_bundling); - // } - switch (commonjs_wrapper_expr) { .bun_dev => |commonjs_wrapper| { var require_function_args = allocator.alloc(Arg, 2) catch unreachable; diff --git a/src/js_printer.zig b/src/js_printer.zig index 56b6d0fae..0cdf21f08 100644 --- a/src/js_printer.zig +++ b/src/js_printer.zig @@ -2194,14 +2194,7 @@ fn NewPrinter( } }, .e_require_string => |e| { - if (rewrite_esm_to_cjs and p.importRecord(e.import_record_index).is_legacy_bundled) { - p.printIndent(); - p.printBundledRequire(e); - p.printSemicolonIfNeeded(); - return; - } - - if (!rewrite_esm_to_cjs or !p.importRecord(e.import_record_index).is_legacy_bundled) { + if (!rewrite_esm_to_cjs) { p.printRequireOrImportExpr(e.import_record_index, e.unwrapped_id != std.math.maxInt(u32), &([_]G.Comment{}), level, flags); } }, @@ -2734,7 +2727,7 @@ fn NewPrinter( } else if (symbol.namespace_alias) |namespace| { if (namespace.import_record_index < p.import_records.len) { const import_record = p.importRecord(namespace.import_record_index); - if (import_record.is_legacy_bundled or namespace.was_originally_property_access) { + if (namespace.was_originally_property_access) { var wrap = false; didPrint = true; @@ -3847,7 +3840,7 @@ fn NewPrinter( if (symbol.namespace_alias) |namespace| { const import_record = p.importRecord(namespace.import_record_index); - if (import_record.is_legacy_bundled or namespace.was_originally_property_access) { + if (namespace.was_originally_property_access) { p.printIdentifier(name); p.print(": () => "); p.printNamespaceAlias(import_record.*, namespace); @@ -3912,7 +3905,7 @@ fn NewPrinter( if (p.symbols().get(item.name.ref.?)) |symbol| { if (symbol.namespace_alias) |namespace| { const import_record = p.importRecord(namespace.import_record_index); - if (import_record.is_legacy_bundled or namespace.was_originally_property_access) { + if (namespace.was_originally_property_access) { p.print("var "); p.printSymbol(item.name.ref.?); p.@"print = "(); @@ -4487,53 +4480,6 @@ fn NewPrinter( } return; - } else if (record.is_legacy_bundled) { - if (!record.path.is_disabled) { - if (!p.has_printed_bundled_import_statement) { - p.has_printed_bundled_import_statement = true; - p.printWhitespacer(ws("import { ")); - - const last = p.import_records.len - 1; - var needs_comma = false; - // This might be a determinsim issue - // But, it's not random - skip: for (p.import_records, 0..) |_record, i| { - if (!_record.is_legacy_bundled or _record.module_id == 0) continue; - - if (i < last) { - // Prevent importing the same module ID twice - // We could use a map but we want to avoid allocating - // and this should be pretty quick since it's just comparing a uint32 - for (p.import_records[i + 1 ..]) |_record2| { - if (_record2.is_legacy_bundled and _record2.module_id > 0 and _record2.module_id == _record.module_id) { - continue :skip; - } - } - } - - if (needs_comma) { - p.print(","); - p.printSpace(); - } - p.printLoadFromBundleWithoutCall(@as(u32, @truncate(i))); - needs_comma = true; - } - - p.printWhitespacer(ws("} from ")); - - p.printQuotedUTF8(record.path.text, false); - p.printSemicolonAfterStatement(); - } - } else { - p.print("var "); - - p.printLoadFromBundleWithoutCall(s.import_record_index); - p.printWhitespacer(ws("= () => ({ default: {}})")); - p.printSemicolonAfterStatement(); - } - - p.printBundledImport(record.*, s); - return; } if (record.handles_import_errors and record.path.is_disabled and record.kind.isCommonJS()) { @@ -6020,100 +5966,3 @@ pub fn printCommonJS( return @as(usize, @intCast(@max(printer.writer.written, 0))); } - -// pub fn printChunk( -// comptime Writer: type, -// _writer: Writer, -// tree: Ast, -// symbols: js_ast.Symbol.Map, -// opts: Options, -// ) PrintResult { -// var writer = _writer; -// var printer = PrinterType.init( -// writer, -// &tree, -// source, -// symbols, -// opts, -// undefined, -// ); -// } - -pub fn printCommonJSThreaded( - comptime Writer: type, - _writer: Writer, - tree: Ast, - symbols: js_ast.Symbol.Map, - source: *const logger.Source, - comptime ascii_only: bool, - opts: Options, - lock: *Lock, - comptime GetPosType: type, - getter: GetPosType, - comptime getPos: fn (ctx: GetPosType) anyerror!u64, - end_off_ptr: *u32, -) !WriteResult { - const PrinterType = NewPrinter(ascii_only, Writer, true, ascii_only, false, false); - var writer = _writer; - var renamer = rename.NoOpRenamer.init(symbols, source); - var printer = PrinterType.init( - writer, - tree.import_records.slice(), - opts, - renamer.toRenamer(), - undefined, - ); - - defer { - imported_module_ids_list = printer.imported_module_ids; - } - if (tree.prepend_part) |part| { - for (part.stmts) |stmt| { - try printer.printStmt(stmt); - if (printer.writer.getError()) {} else |err| { - return err; - } - printer.printSemicolonIfNeeded(); - } - } - - for (tree.parts.slice()) |part| { - for (part.stmts) |stmt| { - try printer.printStmt(stmt); - if (printer.writer.getError()) {} else |err| { - return err; - } - printer.printSemicolonIfNeeded(); - } - } - - // Add a couple extra newlines at the end - printer.writer.print(@TypeOf("\n\n"), "\n\n"); - - var result: WriteResult = .{ .off = 0, .len = 0, .end_off = 0 }; - { - defer lock.unlock(); - lock.lock(); - result.off = @as(u32, @truncate(try getPos(getter))); - if (comptime Environment.isLinux) { - if (printer.writer.written > C.preallocate_length) { - // on mac, it's relative to current position in file handle - // on linux, it's absolute - try C.preallocate_file( - getter.handle, - @as(std.os.off_t, @intCast(if (comptime Environment.isMac) 0 else result.off)), - @as(std.os.off_t, @intCast(printer.writer.written)), - ); - } - } - - try printer.writer.done(); - @fence(.SeqCst); - result.end_off = @as(u32, @truncate(try getPos(getter))); - @atomicStore(u32, end_off_ptr, result.end_off, .SeqCst); - } - - result.len = @as(usize, @intCast(@max(printer.writer.written, 0))); - - return result; -} diff --git a/src/linker.zig b/src/linker.zig index ac902d75a..d3d620f4b 100644 --- a/src/linker.zig +++ b/src/linker.zig @@ -203,82 +203,9 @@ pub const Linker = struct { comptime ignore_runtime: bool, comptime is_bun: bool, ) !void { - return linkAllowImportingFromBundle(linker, file_path, result, origin, import_path_format, ignore_runtime, true, is_bun); - } - - fn whenModuleNotFound( - linker: *ThisLinker, - import_record: *ImportRecord, - result: *_bundler.ParseResult, - comptime is_bun: bool, - ) !bool { - if (import_record.handles_import_errors) { - import_record.path.is_disabled = true; - return false; - } - - if (comptime is_bun) { - // make these happen at runtime - if (import_record.kind == .require or import_record.kind == .require_resolve) { - return false; - } - } - - if (import_record.path.text.len > 0 and Resolver.isPackagePath(import_record.path.text)) { - if (linker.options.target.isWebLike() and Options.ExternalModules.isNodeBuiltin(import_record.path.text)) { - try linker.log.addResolveError( - &result.source, - import_record.range, - linker.allocator, - "Could not resolve: \"{s}\". Try setting --target=\"node\"", - .{import_record.path.text}, - import_record.kind, - error.ModuleNotFound, - ); - } else { - try linker.log.addResolveError( - &result.source, - import_record.range, - linker.allocator, - "Could not resolve: \"{s}\". Maybe you need to \"bun install\"?", - .{import_record.path.text}, - import_record.kind, - error.ModuleNotFound, - ); - } - } else { - try linker.log.addResolveError( - &result.source, - import_record.range, - linker.allocator, - "Could not resolve: \"{s}\"", - .{ - import_record.path.text, - }, - import_record.kind, - error.ModuleNotFound, - ); - } - return true; - } - - pub fn linkAllowImportingFromBundle( - linker: *ThisLinker, - file_path: Fs.Path, - result: *_bundler.ParseResult, - origin: URL, - comptime import_path_format: Options.BundleOptions.ImportPathFormat, - comptime ignore_runtime: bool, - comptime allow_import_from_bundle: bool, - comptime is_bun: bool, - ) !void { const source_dir = file_path.sourceDir(); var externals = std.ArrayList(u32).init(linker.allocator); - var needs_bundle = false; var had_resolve_errors = false; - var needs_require = false; - _ = needs_require; - var node_module_bundle_import_path: ?string = null; const is_deferred = result.pending_imports.len > 0; @@ -300,29 +227,21 @@ pub const Linker = struct { const record_index = record_i; if (comptime !ignore_runtime) { if (strings.eqlComptime(import_record.path.namespace, "runtime")) { - // runtime is included in the bundle, so we don't need to dynamically import it - if (linker.options.node_modules_bundle != null) { - node_module_bundle_import_path = node_module_bundle_import_path orelse - linker.nodeModuleBundleImportPath(origin); - import_record.path.text = node_module_bundle_import_path.?; - result.ast.runtime_import_record_id = record_index; + if (import_path_format == .absolute_url) { + import_record.path = Fs.Path.initWithNamespace(try origin.joinAlloc(linker.allocator, "", "", "bun:wrap", "", ""), "bun"); } else { - if (import_path_format == .absolute_url) { - import_record.path = Fs.Path.initWithNamespace(try origin.joinAlloc(linker.allocator, "", "", "bun:wrap", "", ""), "bun"); - } else { - import_record.path = try linker.generateImportPath( - source_dir, - Linker.runtime_source_path, - false, - "bun", - origin, - import_path_format, - ); - } - - result.ast.runtime_import_record_id = record_index; - result.ast.needs_runtime = true; + import_record.path = try linker.generateImportPath( + source_dir, + Linker.runtime_source_path, + false, + "bun", + origin, + import_path_format, + ); } + + result.ast.runtime_import_record_id = record_index; + result.ast.needs_runtime = true; continue; } } @@ -413,66 +332,11 @@ pub const Linker = struct { } } - if (comptime allow_import_from_bundle) { - if (linker.options.node_modules_bundle) |node_modules_bundle| { - if (Resolver.isPackagePath(import_record.path.text)) { - const text = import_record.path.text; - - var package_name = text; - if (text[0] == '@') { - if (std.mem.indexOfScalar(u8, text, '/')) |i| { - if (std.mem.indexOfScalar(u8, text[i + 1 ..], '/')) |j| { - package_name = text[0 .. i + 1 + j]; - } - } - } else { - if (std.mem.indexOfScalar(u8, text, '/')) |i| { - package_name = text[0..i]; - } - } - if (package_name.len != text.len) { - if (node_modules_bundle.getPackage(package_name)) |pkg| { - const import_path = text[@min(text.len, package_name.len + 1)..]; - if (node_modules_bundle.findModuleIDInPackageIgnoringExtension(pkg, import_path)) |found_module| { - node_module_bundle_import_path = node_module_bundle_import_path orelse - linker.nodeModuleBundleImportPath(origin); - - import_record.path.text = node_module_bundle_import_path.?; - import_record.module_id = node_modules_bundle.bundle.modules[found_module].id; - needs_bundle = true; - continue :outer; - } - } - } - } - } - } - var resolved_import_: anyerror!Resolver.Result = brk: { switch (import_record.tag) { else => {}, // for fast refresh, attempt to read the version directly from the bundle instead of resolving it .react_refresh => { - if (comptime allow_import_from_bundle) { - if (linker.options.node_modules_bundle) |node_modules_bundle| { - const runtime = linker.options.jsx.refresh_runtime; - const package_name = runtime[0 .. strings.indexOfChar(runtime, '/') orelse runtime.len]; - - if (node_modules_bundle.getPackage(package_name)) |pkg| { - const import_path = runtime[@min(runtime.len, package_name.len + 1)..]; - if (node_modules_bundle.findModuleInPackage(pkg, import_path)) |found_module| { - node_module_bundle_import_path = node_module_bundle_import_path orelse - linker.nodeModuleBundleImportPath(origin); - - import_record.path.text = node_module_bundle_import_path.?; - import_record.module_id = found_module.id; - needs_bundle = true; - continue :outer; - } - } - } - } - if (linker.options.jsx.use_embedded_refresh_runtime) { import_record.path = Fs.Path.initWithNamespace(try origin.joinAlloc(linker.allocator, "", "", linker.options.jsx.refresh_runtime, "", ""), "bun"); continue :outer; @@ -548,60 +412,6 @@ pub const Linker = struct { }; const loader = linker.options.loader(path.name.ext); - if (loader.isJavaScriptLikeOrJSON()) { - if (comptime allow_import_from_bundle) { - bundled: { - if (linker.options.node_modules_bundle) |node_modules_bundle| { - const package_json = resolved_import.package_json orelse break :bundled; - const package_base_dir = package_json.source.path.sourceDir(); - if (node_modules_bundle.getPackageIDByHash(package_json.hash)) |pkg_id| { - const package = node_modules_bundle.bundle.packages[pkg_id]; - - if (comptime Environment.isDebug) { - std.debug.assert(strings.eql(node_modules_bundle.str(package.name), package_json.name)); - std.debug.assert(strings.eql(node_modules_bundle.str(package.version), package_json.version)); - } - - const package_relative_path = linker.fs.relative( - package_base_dir, - if (!strings.eqlComptime(path.namespace, "node")) path.pretty else path.text, - ); - - const found_module = node_modules_bundle.findModuleInPackage(&package, package_relative_path) orelse { - // linker.log.addErrorFmt( - // null, - // logger.Loc.Empty, - // linker.allocator, - // "New dependency import: \"{s}/{s}\"\nPlease run `bun bun` to update the .bun.", - // .{ - // package_json.name, - // package_relative_path, - // }, - // ) catch {}; - break :bundled; - }; - - if (comptime Environment.isDebug) { - const module_path = node_modules_bundle.str(found_module.path); - std.debug.assert( - strings.eql( - module_path, - package_relative_path, - ), - ); - } - - node_module_bundle_import_path = node_module_bundle_import_path orelse - linker.nodeModuleBundleImportPath(origin); - import_record.path.text = node_module_bundle_import_path.?; - import_record.module_id = found_module.id; - needs_bundle = true; - continue; - } - } - } - } - } linker.processImportRecord( loader, @@ -805,68 +615,62 @@ pub const Linker = struct { } if (had_resolve_errors) return error.ResolveMessage; result.ast.externals = try externals.toOwnedSlice(); + } + + fn whenModuleNotFound( + linker: *ThisLinker, + import_record: *ImportRecord, + result: *_bundler.ParseResult, + comptime is_bun: bool, + ) !bool { + if (import_record.handles_import_errors) { + import_record.path.is_disabled = true; + return false; + } + + if (comptime is_bun) { + // make these happen at runtime + if (import_record.kind == .require or import_record.kind == .require_resolve) { + return false; + } + } - // if (result.ast.needs_runtime and (result.ast.runtime_import_record_id == null or import_records.items.len == 0)) { - // var new_import_records = try linker.allocator.alloc(ImportRecord, import_records.items.len + 1); - // bun.copy(ImportRecord, new_import_records, import_records.items); - - // new_import_records[new_import_records.len - 1] = ImportRecord{ - // .kind = .stmt, - // .path = if (linker.options.node_modules_bundle != null) - // Fs.Path.init(node_module_bundle_import_path orelse linker.nodeModuleBundleImportPath(origin)) - // else if (import_path_format == .absolute_url) - // Fs.Path.initWithNamespace(try origin.joinAlloc(linker.allocator, "", "", "bun:wrap", "", ""), "bun") - // else - // try linker.generateImportPath(source_dir, Linker.runtime_source_path, false, "bun", origin, import_path_format), - - // .range = logger.Range{ .loc = logger.Loc{ .start = 0 }, .len = 0 }, - // }; - // result.ast.runtime_import_record_id = @truncate(u32, import_records.items.len - 1); - // import_records.items = new_import_records; - // import_records.capacity = new_import_records.len; - // } - - // // We _assume_ you're importing ESM. - // // But, that assumption can be wrong without parsing code of the imports. - // // That's where in here, we inject - // // > import {require} from 'bun:wrap'; - // // Since they definitely aren't using require, we don't have to worry about the symbol being renamed. - // if (needs_require and !result.ast.uses_require_ref) { - // result.ast.uses_require_ref = true; - // const PrependPart = struct { - // stmts: [1]js_ast.Stmt, - // import_statement: js_ast.S.Import, - // clause_items: [1]js_ast.ClauseItem, - // }; - // var prepend = linker.allocator.create(PrependPart) catch unreachable; - - // prepend.* = .{ - // .clause_items = .{ - // .{ - // .alias = require_alias, - // .original_name = "", - // .alias_loc = logger.Loc.Empty, - // .name = js_ast.LocRef{ - // .loc = logger.Loc.Empty, - // .ref = result.ast.require_ref, - // }, - // }, - // }, - // .import_statement = .{ - // .namespace_ref = Ref.None, - // .items = &prepend.clause_items, - // .import_record_index = result.ast.runtime_import_record_id.?, - // }, - // .stmts = undefined, - // }; - - // prepend.stmts[0] = .{ - // .data = .{ .s_import = &prepend.import_statement }, - // .loc = logger.Loc.Empty, - // }; - - // result.ast.prepend_part = js_ast.Part{ .stmts = &prepend.stmts }; - // } + if (import_record.path.text.len > 0 and Resolver.isPackagePath(import_record.path.text)) { + if (linker.options.target.isWebLike() and Options.ExternalModules.isNodeBuiltin(import_record.path.text)) { + try linker.log.addResolveError( + &result.source, + import_record.range, + linker.allocator, + "Could not resolve: \"{s}\". Try setting --target=\"node\"", + .{import_record.path.text}, + import_record.kind, + error.ModuleNotFound, + ); + } else { + try linker.log.addResolveError( + &result.source, + import_record.range, + linker.allocator, + "Could not resolve: \"{s}\". Maybe you need to \"bun install\"?", + .{import_record.path.text}, + import_record.kind, + error.ModuleNotFound, + ); + } + } else { + try linker.log.addResolveError( + &result.source, + import_record.range, + linker.allocator, + "Could not resolve: \"{s}\"", + .{ + import_record.path.text, + }, + import_record.kind, + error.ModuleNotFound, + ); + } + return true; } const ImportPathsList = allocators.BSSStringList(512, 128); diff --git a/src/node_module_bundle.zig b/src/node_module_bundle.zig deleted file mode 100644 index e785cb61d..000000000 --- a/src/node_module_bundle.zig +++ /dev/null @@ -1,479 +0,0 @@ -const schema = @import("./api/schema.zig"); -const Api = schema.Api; -const std = @import("std"); -const Fs = @import("./fs.zig"); -const bun = @import("root").bun; -const string = bun.string; -const Output = bun.Output; -const Global = bun.Global; -const Environment = bun.Environment; -const strings = bun.strings; -const MutableString = bun.MutableString; -const FileDescriptorType = bun.FileDescriptor; -const StoredFileDescriptorType = bun.StoredFileDescriptorType; -const stringZ = bun.stringZ; -const default_allocator = bun.default_allocator; -const C = bun.C; - -pub fn modulesIn(bundle: *const Api.JavascriptBundle, pkg: *const Api.JavascriptBundledPackage) []const Api.JavascriptBundledModule { - return bundle.modules[pkg.modules_offset .. pkg.modules_offset + pkg.modules_length]; -} - -// This corresponds to Api.JavascriptBundledPackage.hash -pub const BundledPackageHash = u32; -// This is the offset in the array of packages -pub const BundledPackageID = u32; - -const PackageIDMap = std.AutoHashMap(BundledPackageHash, BundledPackageID); - -const PackageNameMap = bun.StringHashMap([]BundledPackageID); - -pub const AllocatedString = struct { - str: string, - len: u32, - allocator: std.mem.Allocator, -}; - -pub const NodeModuleBundle = struct { - container: Api.JavascriptBundleContainer, - bundle: Api.JavascriptBundle, - allocator: std.mem.Allocator, - bytes_ptr: []u8 = undefined, - bytes: []u8 = &[_]u8{}, - fd: FileDescriptorType = 0, - code_end_pos: u32 = 0, - - // Lookup packages by ID - hash(name@version) - package_id_map: PackageIDMap, - - // Lookup packages by name. Remember that you can have multiple versions of the same package. - package_name_map: PackageNameMap, - - // This is stored as a single pre-allocated, flat array so we can avoid dynamic allocations. - package_name_ids_ptr: []BundledPackageID = &([_]BundledPackageID{}), - - code_string: ?AllocatedString = null, - - pub const magic_bytes = "#!/usr/bin/env bun\n\n"; - threadlocal var jsbundle_prefix: [magic_bytes.len + 5]u8 = undefined; - - // TODO: support preact-refresh, others by not hard coding - pub fn hasFastRefresh(this: *const NodeModuleBundle) bool { - return this.package_name_map.contains("react-refresh"); - } - - pub fn readCodeAsStringSlow(this: *NodeModuleBundle, allocator: std.mem.Allocator) !string { - if (this.code_string) |code| { - return code.str; - } - - var file = std.fs.File{ .handle = this.fd }; - - var buf = try allocator.alloc(u8, this.code_end_pos); - const count = try file.preadAll(buf, this.codeStartOffset()); - this.code_string = AllocatedString{ .str = buf[0..count], .len = @as(u32, @truncate(buf.len)), .allocator = allocator }; - return this.code_string.?.str; - } - - pub fn loadPackageMap(this: *NodeModuleBundle) !void { - this.package_name_map = PackageNameMap.init(this.allocator); - this.package_id_map = PackageIDMap.init(this.allocator); - - const package_count = @as(u32, @truncate(this.bundle.packages.len)); - - // this.package_has_multiple_versions = try std.bit_set.DynamicBitSet.initFull(package_count, this.allocator); - - try this.package_id_map.ensureTotalCapacity( - package_count, - ); - this.package_name_ids_ptr = try this.allocator.alloc(BundledPackageID, this.bundle.packages.len); - var remaining_names = this.package_name_ids_ptr; - try this.package_name_map.ensureTotalCapacity( - package_count, - ); - var prev_package_ids_for_name: []u32 = &[_]u32{}; - - for (this.bundle.packages, 0..) |package, _package_id| { - const package_id = @as(u32, @truncate(_package_id)); - std.debug.assert(package.hash != 0); - this.package_id_map.putAssumeCapacityNoClobber(package.hash, @as(u32, @truncate(package_id))); - - const package_name = this.str(package.name); - var entry = this.package_name_map.getOrPutAssumeCapacity(package_name); - - if (entry.found_existing) { - // this.package_has_multiple_versions.set(prev_package_ids_for_name[prev_package_ids_for_name.len - 1]); - // Assert that multiple packages with the same name come immediately after another - // This catches any issues with the sorting order, which would cause all sorts of weird bugs - // This also allows us to simply extend the length of the previous slice to the new length - // Saving us an allocation - if (@intFromPtr(prev_package_ids_for_name.ptr) != @intFromPtr(entry.value_ptr.ptr)) { - Output.prettyErrorln( - \\<r><red>Fatal<r>: incorrect package sorting order detected in .bun file.\n - \\This is a bug! Please create an issue.\n - \\If this bug blocks you from doing work, for now - \\please <b>avoid having multiple versions of <cyan>"{s}"<r> in the same bundle.\n - \\\n - \\- Jarred" - , - .{ - package_name, - }, - ); - Global.crash(); - } - - const end = prev_package_ids_for_name.len + 1; - // Assert we have enough room to add another package - std.debug.assert(end < remaining_names.len); - entry.value_ptr.* = prev_package_ids_for_name.ptr[0..end]; - entry.value_ptr.*[end - 1] = package_id; - } else { - prev_package_ids_for_name = remaining_names[0..1]; - prev_package_ids_for_name[0] = package_id; - entry.value_ptr.* = prev_package_ids_for_name; - remaining_names = remaining_names[1..]; - } - } - } - - pub fn getPackageIDByHash(this: *const NodeModuleBundle, hash: BundledPackageID) ?u32 { - return this.package_id_map.get(hash); - } - - pub fn getPackageIDByName(this: *const NodeModuleBundle, name: string) ?[]u32 { - return this.package_name_map.get(name); - } - - pub fn getPackage(this: *const NodeModuleBundle, name: string) ?*const Api.JavascriptBundledPackage { - const package_id = this.getPackageIDByName(name) orelse return null; - return &this.bundle.packages[@as(usize, @intCast(package_id[0]))]; - } - - pub fn hasModule(this: *const NodeModuleBundle, name: string) ?*const Api.JavascriptBundledPackage { - const package_id = this.getPackageID(name) orelse return null; - return &this.bundle.packages[@as(usize, @intCast(package_id))]; - } - - pub const ModuleQuery = struct { - package: *const Api.JavascriptBundledPackage, - relative_path: string, - extensions: []string, - }; - - pub fn allocModuleImport( - this: *const NodeModuleBundle, - to: *const Api.JavascriptBundledModule, - allocator: std.mem.Allocator, - ) !string { - const fmt = bun.fmt.hexIntLower(this.bundle.packages[to.package_id].hash); - return try std.fmt.allocPrint( - allocator, - "{any}/{s}", - .{ - fmt, - this.str(to.path), - 123, - }, - ); - } - - pub fn findModuleInPackage( - this: *const NodeModuleBundle, - package: *const Api.JavascriptBundledPackage, - _query: string, - ) ?*const Api.JavascriptBundledModule { - if (this.findModuleIDInPackage(package, _query)) |id| { - return &this.bundle.modules[id]; - } - - return null; - } - - pub fn findModuleIDInPackageStupid( - this: *const NodeModuleBundle, - package: *const Api.JavascriptBundledPackage, - _query: string, - ) ?u32 { - for (modulesIn(&this.bundle, package), 0..) |mod, i| { - if (strings.eql(this.str(mod.path), _query)) { - return @as(u32, @truncate(i + package.modules_offset)); - } - } - - return null; - } - - pub fn findModuleIDInPackage( - this: *const NodeModuleBundle, - package: *const Api.JavascriptBundledPackage, - _query: string, - ) ?u32 { - const ModuleFinder = struct { - const Self = @This(); - ctx: *const NodeModuleBundle, - pkg: *const Api.JavascriptBundledPackage, - query: string, - - // Since the module doesn't necessarily exist, we use an integer overflow as the module name - pub fn moduleName(context: *const Self, module: *const Api.JavascriptBundledModule) string { - return if (module.path.offset == context.ctx.bundle.manifest_string.len) context.query else context.ctx.str(module.path); - } - - pub fn cmpAsc(context: Self, lhs: Api.JavascriptBundledModule, rhs: Api.JavascriptBundledModule) std.math.Order { - // Comapre the module name - const lhs_name = context.moduleName(&lhs); - const rhs_name = context.moduleName(&rhs); - - const traversal_length = @min(lhs_name.len, rhs_name.len); - - for (lhs_name[0..traversal_length], 0..) |char, i| { - switch (std.math.order(char, rhs_name[i])) { - .lt, .gt => |order| { - return order; - }, - .eq => {}, - } - } - - return std.math.order(lhs_name.len, rhs_name.len); - } - }; - var to_find = Api.JavascriptBundledModule{ - .package_id = 0, - .code = .{}, - .path = .{ - .offset = @as(u32, @truncate(this.bundle.manifest_string.len)), - }, - }; - - var finder = ModuleFinder{ .ctx = this, .pkg = package, .query = _query }; - - const modules = modulesIn(&this.bundle, package); - return @as(u32, @intCast(std.sort.binarySearch( - Api.JavascriptBundledModule, - to_find, - modules, - finder, - ModuleFinder.cmpAsc, - ) orelse return null)) + package.modules_offset; - } - - pub fn findModuleIDInPackageIgnoringExtension( - this: *const NodeModuleBundle, - package: *const Api.JavascriptBundledPackage, - _query: string, - ) ?u32 { - const ModuleFinder = struct { - const Self = @This(); - ctx: *const NodeModuleBundle, - pkg: *const Api.JavascriptBundledPackage, - query: string, - - // Since the module doesn't necessarily exist, we use an integer overflow as the module name - pub fn moduleName(context: *const Self, module: *const Api.JavascriptBundledModule) string { - return if (module.path.offset == context.ctx.bundle.manifest_string.len) context.query else context.ctx.str(.{ - .offset = module.path.offset, - .length = module.path.length - @as(u32, module.path_extname_length), - }); - } - - pub fn cmpAsc(context: Self, lhs: Api.JavascriptBundledModule, rhs: Api.JavascriptBundledModule) std.math.Order { - // Comapre the module name - const lhs_name = context.moduleName(&lhs); - const rhs_name = context.moduleName(&rhs); - - const traversal_length = @min(lhs_name.len, rhs_name.len); - - for (lhs_name[0..traversal_length], 0..) |char, i| { - switch (std.math.order(char, rhs_name[i])) { - .lt, .gt => |order| { - return order; - }, - .eq => {}, - } - } - - return std.math.order(lhs_name.len, rhs_name.len); - } - }; - var to_find = Api.JavascriptBundledModule{ - .package_id = 0, - .code = .{}, - .path = .{ - .offset = @as(u32, @truncate(this.bundle.manifest_string.len)), - }, - }; - - var finder = ModuleFinder{ .ctx = this, .pkg = package, .query = _query[0 .. _query.len - std.fs.path.extension(_query).len] }; - - const modules = modulesIn(&this.bundle, package); - return @as(u32, @intCast(std.sort.binarySearch( - Api.JavascriptBundledModule, - to_find, - modules, - finder, - ModuleFinder.cmpAsc, - ) orelse return null)) + package.modules_offset; - } - - pub fn init(container: Api.JavascriptBundleContainer, allocator: std.mem.Allocator) NodeModuleBundle { - return NodeModuleBundle{ - .container = container, - .bundle = container.bundle.?, - .allocator = allocator, - .package_id_map = undefined, - .package_name_map = undefined, - .package_name_ids_ptr = undefined, - }; - } - - pub fn getCodeEndPosition(stream: anytype, comptime needs_seek: bool) !u32 { - if (needs_seek) try stream.seekTo(0); - - const read_bytes = try stream.read(&jsbundle_prefix); - if (read_bytes != jsbundle_prefix.len) { - return error.JSBundleBadHeaderTooShort; - } - - return std.mem.readIntNative(u32, jsbundle_prefix[magic_bytes.len .. magic_bytes.len + 4]); - } - - pub fn loadBundle(allocator: std.mem.Allocator, stream: anytype) !NodeModuleBundle { - const end = try getCodeEndPosition(stream, false); - try stream.seekTo(end); - const file_end = try stream.getEndPos(); - var file_bytes = try allocator.alloc(u8, file_end - end); - var read_count = try stream.read(file_bytes); - var read_bytes = file_bytes[0..read_count]; - var reader = schema.Reader.init(read_bytes, allocator); - var container = try Api.JavascriptBundleContainer.decode(&reader); - if (container.bundle == null) return error.InvalidBundle; - var bundle = NodeModuleBundle{ - .allocator = allocator, - .container = container, - .bundle = container.bundle.?, - .fd = stream.handle, - // sorry you can't have 4 GB of node_modules - .code_end_pos = end - @as(u32, @intCast(jsbundle_prefix.len)), - .bytes = read_bytes, - .bytes_ptr = file_bytes, - .package_id_map = undefined, - .package_name_map = undefined, - .package_name_ids_ptr = undefined, - }; - try bundle.loadPackageMap(); - return bundle; - } - - pub fn str(bundle: *const NodeModuleBundle, pointer: Api.StringPointer) string { - return bundle.bundle.manifest_string[pointer.offset .. pointer.offset + pointer.length]; - } - - pub fn printSummary(this: *const NodeModuleBundle) void { - const indent = comptime " "; - for (this.bundle.packages) |pkg| { - const modules = this.bundle.modules[pkg.modules_offset .. pkg.modules_offset + pkg.modules_length]; - - Output.prettyln( - "<r><blue><b>{s}</r> v{s}", - .{ this.str(pkg.name), this.str(pkg.version) }, - ); - - for (modules, 0..) |module, module_i| { - const size_level: SizeLevel = - switch (module.code.length) { - 0...5_000 => .good, - 5_001...74_999 => .neutral, - else => .bad, - }; - - Output.print(indent, .{}); - prettySize(module.code.length, size_level, ">"); - Output.prettyln( - indent ++ "<d>{s}</r>" ++ std.fs.path.sep_str ++ "{s} <r><d>[{d}]<r>\n", - .{ - this.str(pkg.name), - this.str(module.path), - module_i + pkg.modules_offset, - }, - ); - } - - Output.print("\n", .{}); - } - const source_code_size = this.container.code_length.? - @as(u32, @intCast(jsbundle_prefix.len)); - - Output.pretty("<b>", .{}); - prettySize(source_code_size, .neutral, ">"); - Output.prettyln("<b> JavaScript<r>", .{}); - Output.prettyln(indent ++ "<b>{d:6} modules", .{this.bundle.modules.len}); - Output.prettyln(indent ++ "<b>{d:6} packages", .{this.bundle.packages.len}); - } - - pub inline fn codeStartOffset(_: *const NodeModuleBundle) u32 { - return @as(u32, @intCast(jsbundle_prefix.len)); - } - - pub fn printSummaryFromDisk( - comptime StreamType: type, - input: StreamType, - comptime DestinationStreamType: type, - _: DestinationStreamType, - allocator: std.mem.Allocator, - ) !void { - const this = try loadBundle(allocator, input); - this.printSummary(); - } - - const SizeLevel = enum { good, neutral, bad }; - fn prettySize(size: u32, level: SizeLevel, comptime align_char: []const u8) void { - switch (size) { - 0...1024 * 1024 => { - switch (level) { - .bad => Output.pretty("<red>{d: " ++ align_char ++ "6.2} KB</r>", .{@as(f64, @floatFromInt(size)) / 1024.0}), - .neutral => Output.pretty("{d: " ++ align_char ++ "6.2} KB</r>", .{@as(f64, @floatFromInt(size)) / 1024.0}), - .good => Output.pretty("<green>{d: " ++ align_char ++ "6.2} KB</r>", .{@as(f64, @floatFromInt(size)) / 1024.0}), - } - }, - else => { - switch (level) { - .bad => Output.pretty("<red>{d: " ++ align_char ++ "6.2} MB</r>", .{@as(f64, @floatFromInt(size)) / (1024 * 1024.0)}), - .neutral => Output.pretty("{d: " ++ align_char ++ "6.2} MB</r>", .{@as(f64, @floatFromInt(size)) / (1024 * 1024.0)}), - .good => Output.pretty("<green>{d: " ++ align_char ++ "6.2} MB</r>", .{@as(f64, @floatFromInt(size)) / (1024 * 1024.0)}), - } - }, - } - } - - pub fn printBundle( - comptime StreamType: type, - input: StreamType, - comptime DestinationStreamType: type, - output: DestinationStreamType, - ) !void { - const BufferStreamContext = struct { - pub fn run(in: StreamType, out: DestinationStreamType, end_at: u32) !void { - var buf: [4096]u8 = undefined; - var remain = @as(i64, @intCast(end_at)); - var read_amount: i64 = 99999; - while (remain > 0 and read_amount > 0) { - read_amount = @as(i64, @intCast(in.read(&buf) catch 0)); - remain -= @as(i64, @intCast(try out.write(buf[0..@as(usize, @intCast(@min(read_amount, remain)))]))); - } - } - }; - - if (comptime Environment.isMac) { - // darwin only allows reading ahead on/off, not specific amount - _ = std.os.fcntl(input.handle, std.os.F.RDAHEAD, 1) catch 0; - } - const end = (try getCodeEndPosition(input, false)) - @as(u32, @intCast(jsbundle_prefix.len)); - - try BufferStreamContext.run( - input, - output, - end, - ); - } -}; diff --git a/src/options.zig b/src/options.zig index 647c83786..6915ca1d9 100644 --- a/src/options.zig +++ b/src/options.zig @@ -9,7 +9,6 @@ const api = @import("./api/schema.zig"); const Api = api.Api; const defines = @import("./defines.zig"); const resolve_path = @import("./resolver/resolve_path.zig"); -const NodeModuleBundle = @import("./node_module_bundle.zig").NodeModuleBundle; const URL = @import("./url.zig").URL; const ConditionsMap = @import("./resolver/package_json.zig").ESModule.ConditionsMap; const bun = @import("root").bun; @@ -1377,7 +1376,6 @@ pub const BundleOptions = struct { preserve_symlinks: bool = false, preserve_extensions: bool = false, timings: Timings = Timings{}, - node_modules_bundle: ?*NodeModuleBundle = null, production: bool = false, serve: bool = false, @@ -1576,7 +1574,6 @@ pub const BundleOptions = struct { fs: *Fs.FileSystem, log: *logger.Log, transform: Api.TransformOptions, - node_modules_bundle_existing: ?*NodeModuleBundle, ) !BundleOptions { var opts: BundleOptions = BundleOptions{ .log = log, @@ -1656,72 +1653,6 @@ pub const BundleOptions = struct { else => {}, } - const is_generating_bundle = (transform.generate_node_module_bundle orelse false); - - if (!is_generating_bundle) { - if (node_modules_bundle_existing) |node_mods| { - opts.node_modules_bundle = node_mods; - const pretty_path = fs.relativeTo(transform.node_modules_bundle_path.?); - opts.node_modules_bundle_url = try std.fmt.allocPrint(allocator, "{s}{s}", .{ - opts.origin.href, - pretty_path, - }); - } else if (transform.node_modules_bundle_path) |bundle_path| { - if (bundle_path.len > 0) { - load_bundle: { - const pretty_path = fs.relativeTo(bundle_path); - var bundle_file = std.fs.openFileAbsolute(bundle_path, .{ .mode = .read_write }) catch |err| { - Output.disableBuffering(); - defer Output.enableBuffering(); - Output.prettyErrorln("<r>error opening <d>\"<r><b>{s}<r><d>\":<r> <b><red>{s}<r>", .{ pretty_path, @errorName(err) }); - break :load_bundle; - }; - - const time_start = std.time.nanoTimestamp(); - if (NodeModuleBundle.loadBundle(allocator, bundle_file)) |bundle| { - var node_module_bundle = try allocator.create(NodeModuleBundle); - node_module_bundle.* = bundle; - opts.node_modules_bundle = node_module_bundle; - - if (opts.origin.isAbsolute()) { - opts.node_modules_bundle_url = try opts.origin.joinAlloc( - allocator, - "", - "", - node_module_bundle.bundle.import_from_name, - "", - "", - ); - opts.node_modules_bundle_pretty_path = opts.node_modules_bundle_url[opts.node_modules_bundle_url.len - node_module_bundle.bundle.import_from_name.len - 1 ..]; - } else { - opts.node_modules_bundle_pretty_path = try allocator.dupe(u8, pretty_path); - } - - const elapsed = @as(f64, @floatFromInt((std.time.nanoTimestamp() - time_start))) / std.time.ns_per_ms; - Output.printElapsed(elapsed); - Output.prettyErrorln( - " <b><d>\"{s}\"<r><d> - {d} modules, {d} packages<r>", - .{ - pretty_path, - bundle.bundle.modules.len, - bundle.bundle.packages.len, - }, - ); - Output.flush(); - } else |err| { - Output.disableBuffering(); - Output.prettyErrorln( - "<r>error reading <d>\"<r><b>{s}<r><d>\":<r> <b><red>{s}<r>, <b>deleting it<r> so you don't keep seeing this message.", - .{ pretty_path, @errorName(err) }, - ); - bundle_file.close(); - } - } - } - } - } - // } - if (transform.framework) |_framework| { opts.framework = try Framework.fromApi(_framework, allocator); } @@ -1734,9 +1665,6 @@ pub const BundleOptions = struct { opts.main_fields = transform.main_fields; } - if (opts.framework == null and is_generating_bundle) - opts.env.behavior = .load_all; - opts.external = ExternalModules.init(allocator, &fs.fs, fs.top_level_dir, transform.external, log, opts.target); opts.out_extensions = opts.target.outExtensions(allocator); @@ -1891,7 +1819,7 @@ pub const BundleOptions = struct { opts.source_map = SourceMapOption.fromApi(transform.source_map orelse Api.SourceMapMode._none); } - opts.tree_shaking = opts.serve or opts.target.isBun() or opts.production or is_generating_bundle; + opts.tree_shaking = opts.serve or opts.target.isBun() or opts.production; opts.inlining = opts.tree_shaking; if (opts.inlining) opts.minify_syntax = true; @@ -1911,8 +1839,6 @@ pub const BundleOptions = struct { Analytics.Features.filesystem_router = Analytics.Features.filesystem_router or opts.routes.routes_enabled; Analytics.Features.origin = Analytics.Features.origin or transform.origin != null; Analytics.Features.public_folder = Analytics.Features.public_folder or opts.routes.static_dir_enabled; - Analytics.Features.bun_bun = Analytics.Features.bun_bun or transform.node_modules_bundle_path != null; - Analytics.Features.bunjs = Analytics.Features.bunjs or transform.node_modules_bundle_path_server != null; Analytics.Features.macros = Analytics.Features.macros or opts.target == .bun_macro; Analytics.Features.external = Analytics.Features.external or transform.external.len > 0; Analytics.Features.single_page_app_routing = Analytics.Features.single_page_app_routing or opts.routes.single_page_app_routing; diff --git a/src/resolver/resolver.zig b/src/resolver/resolver.zig index 4aa4239dd..fa7fd7d16 100644 --- a/src/resolver/resolver.zig +++ b/src/resolver/resolver.zig @@ -37,7 +37,7 @@ const JSC = bun.JSC; const allocators = @import("../allocators.zig"); const Msg = logger.Msg; const Path = Fs.Path; -const NodeModuleBundle = @import("../node_module_bundle.zig").NodeModuleBundle; + const PackageManager = @import("../install/install.zig").PackageManager; const Dependency = @import("../install/dependency.zig"); const Install = @import("../install/install.zig"); @@ -464,7 +464,6 @@ pub const Resolver = struct { fs: *Fs.FileSystem, log: *logger.Log, allocator: std.mem.Allocator, - node_module_bundle: ?*NodeModuleBundle, extension_order: []const string = undefined, timer: Timer = undefined, @@ -577,7 +576,6 @@ pub const Resolver = struct { .opts = opts, .timer = Timer.start() catch @panic("Timer fail"), .fs = _fs, - .node_module_bundle = opts.node_modules_bundle, .log = log, .extension_order = opts.extension_order, .care_about_browser_field = opts.target.isWebLike(), |