diff options
author | 2022-01-29 23:49:41 -0800 | |
---|---|---|
committer | 2022-01-29 23:49:41 -0800 | |
commit | adeb880d7f3f6be634357a84bbd389f1c2b044c8 (patch) | |
tree | 67636927dab1fcc3d1746d27bc671c1b253a61c1 | |
parent | 857e9bee0085b13320e3ded423ed8caff30c4e40 (diff) | |
download | bun-adeb880d7f3f6be634357a84bbd389f1c2b044c8.tar.gz bun-adeb880d7f3f6be634357a84bbd389f1c2b044c8.tar.zst bun-adeb880d7f3f6be634357a84bbd389f1c2b044c8.zip |
Enable Fast Refresh when not bundling
-rw-r--r-- | src/http.zig | 43 |
1 files changed, 34 insertions, 9 deletions
diff --git a/src/http.zig b/src/http.zig index 1ec847aad..dacde86fb 100644 --- a/src/http.zig +++ b/src/http.zig @@ -1696,11 +1696,7 @@ pub const RequestContext = struct { if (ctx.bundler.options.hot_module_reloading) { reloader = Api.Reloader.live; if (ctx.bundler.options.jsx.supports_fast_refresh) { - if (ctx.bundler.options.node_modules_bundle) |bundle| { - if (bundle.hasFastRefresh()) { - reloader = Api.Reloader.fast_refresh; - } - } + reloader = Api.Reloader.fast_refresh; } } const welcome_message = Api.WebsocketMessageWelcome{ @@ -2440,7 +2436,29 @@ pub const RequestContext = struct { } if (strings.eqlComptime(path, "wrap")) { - const buffer = Runtime.sourceContent(); + const buffer = Runtime.sourceContent(ctx.bundler.options.jsx.use_embedded_refresh_runtime); + ctx.appendHeader("Content-Type", MimeType.javascript.value); + ctx.appendHeader("Cache-Control", "public, max-age=3600"); + ctx.appendHeader("Age", "0"); + if (FeatureFlags.strong_etags_for_built_files) { + const did_send = ctx.writeETag(buffer) catch false; + if (did_send) return; + } + + if (buffer.len == 0) { + return try ctx.sendNoContent(); + } + const send_body = ctx.method == .GET; + defer ctx.done(); + try ctx.writeStatus(200); + try ctx.prepareToSendBody(buffer.len, false); + if (!send_body) return; + _ = try ctx.writeSocket(buffer, SOCKET_FLAGS); + return; + } + + if (strings.eqlComptime(path, "reactfsh-v0.11.0")) { + const buffer = @embedFile("react-refresh.js"); ctx.appendHeader("Content-Type", MimeType.javascript.value); ctx.appendHeader("Cache-Control", "public, max-age=3600"); ctx.appendHeader("Age", "0"); @@ -3044,7 +3062,6 @@ pub const Server = struct { // We want to bind to the network socket as quickly as possible so that opening the URL works // We use a secondary loop so that we avoid the extra branch in a hot code path - server.detectFastRefresh(); Analytics.Features.fast_refresh = server.bundler.options.jsx.supports_fast_refresh; server.detectTSConfig(); try server.initWatcher(); @@ -3328,11 +3345,16 @@ pub const Server = struct { if (node_modules_bundle.getPackageIDByName(package_name) != null) return; } - _ = this.bundler.resolver.resolve(this.bundler.fs.top_level_dir, runtime, .internal) catch { - // 2. Try react refresh from import source perspective + _ = this.bundler.resolver.resolve(this.bundler.fs.top_level_dir, this.bundler.options.jsx.import_source, .internal) catch { + // if they don't have React, they can't use fast refresh this.bundler.options.jsx.supports_fast_refresh = false; return; }; + + this.bundler.options.jsx.supports_fast_refresh = true; + this.bundler.options.jsx.refresh_runtime = "bun:reactfsh-v0.11.0"; + this.bundler.options.jsx.use_embedded_refresh_runtime = true; + this.bundler.resolver.opts = this.bundler.options; } pub fn detectTSConfig(this: *Server) void { @@ -3382,6 +3404,8 @@ pub const Server = struct { return; } + server.detectFastRefresh(); + server.bundler.options.macro_remap = debug.macros orelse .{}; if (debug.fallback_only or server.bundler.env.map.get("BUN_DISABLE_BUN_JS") != null) { @@ -3424,6 +3448,7 @@ pub const Server = struct { }, ); } + } else if (server.bundler.options.routes.single_page_app_routing) { } else { try server.run( ConnectionFeatures{ .filesystem_router = false }, |