diff options
author | 2022-10-06 14:21:14 -0700 | |
---|---|---|
committer | 2022-10-06 14:21:14 -0700 | |
commit | 5c3ce875b2f0fefb40f4ec9c4a800160b682f716 (patch) | |
tree | db396afec9e49604e8b9a818540e5b7342ebb52d | |
parent | 6970250b57918a9bc2199f40496dad59a5f622ce (diff) | |
download | bun-5c3ce875b2f0fefb40f4ec9c4a800160b682f716.tar.gz bun-5c3ce875b2f0fefb40f4ec9c4a800160b682f716.tar.zst bun-5c3ce875b2f0fefb40f4ec9c4a800160b682f716.zip |
Automatically support hot reloading with `export default { fetch() }`
-rw-r--r-- | src/bun.js/javascript.zig | 2 | ||||
-rw-r--r-- | src/bundler/entry_points.zig | 106 |
2 files changed, 76 insertions, 32 deletions
diff --git a/src/bun.js/javascript.zig b/src/bun.js/javascript.zig index c337372e5..336a24a81 100644 --- a/src/bun.js/javascript.zig +++ b/src/bun.js/javascript.zig @@ -1476,7 +1476,7 @@ pub const VirtualMachine = struct { pub fn loadEntryPoint(this: *VirtualMachine, entry_path: string) !*JSInternalPromise { this.main = entry_path; - try this.entry_point.generate(@TypeOf(this.bundler), &this.bundler, Fs.PathName.init(entry_path), main_file_name); + try this.entry_point.generate(this.bun_watcher != null, Fs.PathName.init(entry_path), main_file_name); this.eventLoop().ensureWaker(); var promise: *JSInternalPromise = undefined; diff --git a/src/bundler/entry_points.zig b/src/bundler/entry_points.zig index fec040bd2..0eecda6a9 100644 --- a/src/bundler/entry_points.zig +++ b/src/bundler/entry_points.zig @@ -163,8 +163,7 @@ pub const ServerEntryPoint = struct { pub fn generate( entry: *ServerEntryPoint, - comptime BundlerType: type, - _: *BundlerType, + is_hot_reload_enabled: bool, original_path: Fs.PathName, name: string, ) !void { @@ -180,35 +179,80 @@ pub const ServerEntryPoint = struct { // we want it to go through the linker and the rest of the transpilation process const dir_to_use: string = original_path.dirWithTrailingSlash(); - const code = try std.fmt.bufPrint( - &entry.code_buffer, - \\//Auto-generated file - \\var cjsSymbol = Symbol.for("CommonJS"); - \\import * as start from '{s}{s}'; - \\export * from '{s}{s}'; - \\var entryNamespace = start; - \\var cjs = start?.default; - \\if (cjs && typeof cjs === 'function' && cjsSymbol in cjs) {{ - \\ entryNamespace = cjs(); - \\}} - \\if (typeof entryNamespace?.then === 'function') {{ - \\ entryNamespace = entryNamespace.then((entryNamespace) => {{ - \\ if(typeof entryNamespace?.default?.fetch === 'function') {{ - \\ Bun.serve(entryNamespace.default); - \\ }} - \\ }}, reportError); - \\}} else if (typeof entryNamespace?.default?.fetch === 'function') {{ - \\ Bun.serve(entryNamespace.default); - \\}} - \\ - , - .{ - dir_to_use, - original_path.filename, - dir_to_use, - original_path.filename, - }, - ); + + const code = brk: { + if (is_hot_reload_enabled) { + break :brk try std.fmt.bufPrint( + &entry.code_buffer, + \\//Auto-generated file + \\var cjsSymbol = Symbol.for("CommonJS"); + \\var hmrSymbol = Symbol.for("BunServerHMR"); + \\import * as start from '{s}{s}'; + \\export * from '{s}{s}'; + \\var entryNamespace = start; + \\var cjs = start?.default; + \\if (cjs && typeof cjs === 'function' && cjsSymbol in cjs) {{ + \\ entryNamespace = cjs(); + \\}} + \\if (typeof entryNamespace?.then === 'function') {{ + \\ entryNamespace = entryNamespace.then((entryNamespace) => {{ + \\ if(typeof entryNamespace?.default?.fetch === 'function') {{ + \\ var server = globalThis[hmrSymbol]; + \\ if (server) {{ + \\ server.reload(entryNamespace.default); + \\ }} else {{ + \\ globalThis[hmrSymbol] = Bun.serve(entryNamespace.default)) + \\ }} + \\ }} + \\ }}, reportError); + \\}} else if (typeof entryNamespace?.default?.fetch === 'function') {{ + \\ var server = globalThis[hmrSymbol]; + \\ if (server) {{ + \\ server.reload(entryNamespace.default); + \\ }} else {{ + \\ globalThis[hmrSymbol] = Bun.serve(entryNamespace.default)) + \\ }} + \\}} + \\ + , + .{ + dir_to_use, + original_path.filename, + dir_to_use, + original_path.filename, + }, + ); + } + break :brk try std.fmt.bufPrint( + &entry.code_buffer, + \\//Auto-generated file + \\var cjsSymbol = Symbol.for("CommonJS"); + \\import * as start from '{s}{s}'; + \\export * from '{s}{s}'; + \\var entryNamespace = start; + \\var cjs = start?.default; + \\if (cjs && typeof cjs === 'function' && cjsSymbol in cjs) {{ + \\ entryNamespace = cjs(); + \\}} + \\if (typeof entryNamespace?.then === 'function') {{ + \\ entryNamespace = entryNamespace.then((entryNamespace) => {{ + \\ if(typeof entryNamespace?.default?.fetch === 'function') {{ + \\ Bun.serve(entryNamespace.default); + \\ }} + \\ }}, reportError); + \\}} else if (typeof entryNamespace?.default?.fetch === 'function') {{ + \\ Bun.serve(entryNamespace.default); + \\}} + \\ + , + .{ + dir_to_use, + original_path.filename, + dir_to_use, + original_path.filename, + }, + ); + }; entry.source = logger.Source.initPathString(name, code); entry.source.path.text = name; |