Skip to content

Commit

Permalink
asd
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner committed Dec 27, 2024
1 parent 1afff7c commit 5968b7b
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 73 deletions.
99 changes: 73 additions & 26 deletions src/bake/DevServer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ pub fn init(options: Options) bun.JSOOM!*DevServer {
for (dev.framework.file_system_router_types) |fsr| {
bun.writeAnyToHasher(&hash, fsr.allow_layouts);
bun.writeAnyToHasher(&hash, fsr.ignore_underscores);
hash.update(fsr.entry_server);
hash.update(fsr.entry_server orelse "");
hash.update(&.{0});
hash.update(fsr.entry_client orelse "");
hash.update(&.{0});
Expand Down Expand Up @@ -402,18 +402,20 @@ pub fn init(options: Options) bun.JSOOM!*DevServer {

dev.initServerRuntime();

var has_client_routes = false;
var has_server_routes = false;

// Initialize FrameworkRouter
dev.router = router: {
var types = try std.ArrayListUnmanaged(FrameworkRouter.Type).initCapacity(allocator, options.framework.file_system_router_types.len);
errdefer types.deinit(allocator);

for (options.framework.file_system_router_types, 0..) |fsr, i| {
_ = i; // autofix
const joined_root = bun.path.joinAbs(dev.root, .auto, fsr.root);
const entry = dev.server_bundler.resolver.readDirInfoIgnoreError(joined_root) orelse
continue;

const server_file = try dev.server_graph.insertStaleExtra(fsr.entry_server, false, true);

try types.append(allocator, .{
.abs_root = bun.strings.withoutTrailingSlash(entry.abs_path),
.prefix = fsr.prefix,
Expand All @@ -422,17 +424,28 @@ pub fn init(options: Options) bun.JSOOM!*DevServer {
.extensions = fsr.extensions,
.style = fsr.style,
.allow_layouts = fsr.allow_layouts,
.server_file = toOpaqueFileId(.server, server_file),
.client_file = if (fsr.entry_client) |client|
toOpaqueFileId(.client, try dev.client_graph.insertStale(client, false)).toOptional()
else
.none,
.server_file_string = .{},
});
.server_file = brk: {
if (fsr.entry_server) |server| {
const server_entry = try dev.server_graph.insertStaleExtra(server, false, true);
const server_file = toOpaqueFileId(.server, server_entry).toOptional();
has_server_routes = true;
break :brk server_file;
}

try dev.route_lookup.put(allocator, server_file, .{
.route_index = FrameworkRouter.Route.Index.init(@intCast(i)),
.should_recurse_when_visiting = true,
break :brk .none;
},
.client_file = brk: {
const is_route = fsr.entry_server == null;
if (fsr.entry_client) |client| {
const client_entry = try dev.client_graph.insertStaleExtra(client, false, is_route);
const client_file = toOpaqueFileId(.client, client_entry).toOptional();
has_client_routes = true;
break :brk client_file;
}

break :brk .none;
},
.server_file_string = .{},
});
}

Expand All @@ -442,7 +455,7 @@ pub fn init(options: Options) bun.JSOOM!*DevServer {
// TODO: move scanning to be one tick after server startup. this way the
// line saying the server is ready shows quicker, and route errors show up
// after that line.
try dev.scanInitialRoutes();
try dev.scanInitialRoutes(if (has_server_routes) .server else .client);

if (bun.FeatureFlags.bake_debugging_features and dev.has_pre_crash_handler)
try bun.crash_handler.appendPreCrashHandler(DevServer, dev, dumpStateDueToCrash);
Expand Down Expand Up @@ -476,10 +489,13 @@ fn initServerRuntime(dev: *DevServer) void {
}

/// Deferred one tick so that the server can be up faster
fn scanInitialRoutes(dev: *DevServer) !void {
fn scanInitialRoutes(dev: *DevServer, side: bake.Side) !void {
try dev.router.scanAll(
dev.allocator,
&dev.server_bundler.resolver,
switch (side) {
.server => &dev.server_bundler.resolver,
.client => &dev.client_bundler.resolver,
},
FrameworkRouter.InsertionContext.wrap(DevServer, dev),
);

Expand Down Expand Up @@ -760,12 +776,15 @@ fn onRequestWithBundle(
.{
// routerTypeMain
router_type.server_file_string.get() orelse str: {
const name = dev.server_graph.bundled_files.keys()[fromOpaqueFileId(.server, router_type.server_file).get()];
const str = bun.String.createUTF8(dev.relativePath(name));
defer str.deref();
const js = str.toJS(dev.vm.global);
router_type.server_file_string = JSC.Strong.create(js, dev.vm.global);
break :str js;
if (router_type.server_file.unwrap()) |id| {
const name = dev.server_graph.bundled_files.keys()[fromOpaqueFileId(.server, id).get()];
const str = bun.String.createUTF8(dev.relativePath(name));
defer str.deref();
const js = str.toJS(dev.vm.global);
router_type.server_file_string = JSC.Strong.create(js, dev.vm.global);
break :str js;
}
break :str bun.String.empty.toJS(dev.vm.global);
},
// routeModules
route_bundle.cached_module_list.get() orelse arr: {
Expand Down Expand Up @@ -1064,7 +1083,10 @@ fn traceAllRouteImports(dev: *DevServer, route_bundle: *RouteBundle, gts: *Graph
const router_type = dev.router.typePtr(route.type);

// Both framework entry points are considered
try dev.server_graph.traceImports(fromOpaqueFileId(.server, router_type.server_file), gts, .{ .find_css = true });
if (router_type.server_file.unwrap()) |id| {
try dev.server_graph.traceImports(fromOpaqueFileId(.server, id), gts, .{ .find_css = true });
}

if (router_type.client_file.unwrap()) |id| {
try dev.client_graph.traceImports(fromOpaqueFileId(.client, id), gts, goal);
}
Expand Down Expand Up @@ -1950,6 +1972,10 @@ pub fn IncrementalGraph(side: bake.Side) type {
fn fileKind(file: @This()) FileKind {
return file.kind;
}

pub fn isRoute(file: @This()) bool {
return file.is_route;
}
},
.client => struct {
/// Allocated by default_allocator. Access with `.code()`
Expand All @@ -1959,7 +1985,7 @@ pub fn IncrementalGraph(side: bake.Side) type {
code_len: u32,
flags: Flags,

const Flags = struct {
const Flags = packed struct {
/// If the file has an error, the failure can be looked up
/// in the `.failures` map.
failed: bool,
Expand All @@ -1971,6 +1997,10 @@ pub fn IncrementalGraph(side: bake.Side) type {
is_special_framework_file: bool,
/// CSS and Asset files get special handling
kind: FileKind,

/// If this file is a route root, the route can be looked up in
/// the route list. This also stops dependency propagation.
is_route: bool,
};

comptime {
Expand All @@ -1986,6 +2016,10 @@ pub fn IncrementalGraph(side: bake.Side) type {
};
}

pub fn isRoute(file: *const @This()) bool {
return file.flags.is_route;
}

fn code(file: @This()) []const u8 {
return file.code_ptr[0..file.code_len];
}
Expand Down Expand Up @@ -2123,6 +2157,7 @@ pub fn IncrementalGraph(side: bake.Side) type {
.is_hmr_root = ctx.server_to_client_bitset.isSet(index.get()),
.is_special_framework_file = false,
.kind = kind,
.is_route = false,
};
if (kind == .css) {
if (!gop.found_existing or gop.value_ptr.code_len == 0) {
Expand Down Expand Up @@ -2397,7 +2432,7 @@ pub fn IncrementalGraph(side: bake.Side) type {
switch (side) {
.server => {
const dev = g.owner();
if (file.is_route) {
if (file.isRoute()) {
const route_index = dev.route_lookup.get(file_index) orelse
Output.panic("Route not in lookup index: {d} {}", .{ file_index.get(), bun.fmt.quote(g.bundled_files.keys()[file_index.get()]) });
igLog("\\<- Route", .{});
Expand All @@ -2409,7 +2444,13 @@ pub fn IncrementalGraph(side: bake.Side) type {
}
},
.client => {
if (file.flags.is_hmr_root or (file.flags.kind == .css and trace_kind == .css_to_route)) {
if (file.isRoute()) {
const dev = g.owner();
const key = g.bundled_files.keys()[file_index.get()];
const index = dev.client_graph.getFileIndex(key) orelse
Output.panic("Client Incremental Graph is missing component for {}", .{bun.fmt.quote(key)});
try dev.client_graph.traceDependencies(index, gts, trace_kind);
} else if (file.flags.is_hmr_root or (file.flags.kind == .css and trace_kind == .css_to_route)) {
const dev = g.owner();
const key = g.bundled_files.keys()[file_index.get()];
const index = dev.server_graph.getFileIndex(key) orelse
Expand Down Expand Up @@ -2517,6 +2558,10 @@ pub fn IncrementalGraph(side: bake.Side) type {
} else {
if (side == .server) {
if (is_route) gop.value_ptr.*.is_route = is_route;
} else if (side == .client) {
if (is_route) {
gop.value_ptr.*.flags.is_route = true;
}
}
}

Expand All @@ -2531,6 +2576,7 @@ pub fn IncrementalGraph(side: bake.Side) type {
.is_hmr_root = false,
.is_special_framework_file = false,
.kind = .unknown,
.is_route = is_route,
});
},
.server => {
Expand Down Expand Up @@ -2614,6 +2660,7 @@ pub fn IncrementalGraph(side: bake.Side) type {
.is_hmr_root = false,
.is_special_framework_file = false,
.kind = .unknown,
.is_route = false,
});
},
.server => {
Expand Down
2 changes: 1 addition & 1 deletion src/bake/FrameworkRouter.zig
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub const Type = struct {
/// `FrameworkRouter` itself does not use this value.
client_file: OpaqueFileId.Optional,
/// `FrameworkRouter` itself does not use this value.
server_file: OpaqueFileId,
server_file: OpaqueFileId.Optional,
/// `FrameworkRouter` itself does not use this value.
server_file_string: JSC.Strong,

Expand Down
8 changes: 3 additions & 5 deletions src/bake/bake.zig
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ pub const Framework = struct {
pub const FileSystemRouterType = struct {
root: []const u8,
prefix: []const u8,
entry_server: []const u8,
entry_server: ?[]const u8,
entry_client: ?[]const u8,
ignore_underscores: bool,
ignore_dirs: []const []const u8,
Expand Down Expand Up @@ -281,7 +281,7 @@ pub const Framework = struct {
for (clone.file_system_router_types) |*fsr| {
fsr.root = try arena.dupe(u8, bun.path.joinAbs(server.fs.top_level_dir, .auto, fsr.root));
if (fsr.entry_client) |*entry_client| f.resolveHelper(client, entry_client, &had_errors, "client side entrypoint");
f.resolveHelper(client, &fsr.entry_server, &had_errors, "server side entrypoint");
if (fsr.entry_server) |*entry_server| f.resolveHelper(client, entry_server, &had_errors, "server side entrypoint");
}

if (had_errors) return error.ModuleNotFound;
Expand Down Expand Up @@ -443,9 +443,7 @@ pub const Framework = struct {
const root = try getOptionalString(fsr_opts, global, "root", refs, arena) orelse {
return global.throwInvalidArguments("'fileSystemRouterTypes[{d}]' is missing 'root'", .{i});
};
const server_entry_point = try getOptionalString(fsr_opts, global, "serverEntryPoint", refs, arena) orelse {
return global.throwInvalidArguments("'fileSystemRouterTypes[{d}]' is missing 'serverEntryPoint'", .{i});
};
const server_entry_point = try getOptionalString(fsr_opts, global, "serverEntryPoint", refs, arena);
const client_entry_point = try getOptionalString(fsr_opts, global, "clientEntryPoint", refs, arena);
const prefix = try getOptionalString(fsr_opts, global, "prefix", refs, arena) orelse "/";
const ignore_underscores = try fsr_opts.getBooleanStrict(global, "ignoreUnderscores") orelse false;
Expand Down
Loading

0 comments on commit 5968b7b

Please sign in to comment.