client-side entry point works and also generates a correct url on the server

Former-commit-id: 272e52f55e44e998b9238e4173de37bfc6a05a94
This commit is contained in:
Jarred Sumner
2021-08-11 00:02:08 -07:00
parent 10b4b872a2
commit ca90126cc4
5 changed files with 346 additions and 34 deletions

View File

@@ -9,9 +9,10 @@ const CombinedScanner = @import("../../../query_string_map.zig").CombinedScanner
usingnamespace @import("../bindings/bindings.zig");
usingnamespace @import("../webcore/response.zig");
const Router = @This();
const Bundler = @import("../../../bundler.zig");
const VirtualMachine = JavaScript.VirtualMachine;
const ScriptSrcStream = std.io.FixedBufferStream([]u8);
const Fs = @import("../../../fs.zig");
route: *const FilesystemRouter.Match,
query_string_map: ?QueryStringMap = null,
@@ -73,7 +74,9 @@ fn matchPathNameString(
ctx: js.JSContextRef,
pathname: string,
exception: js.ExceptionRef,
) js.JSObjectRef {}
) js.JSObjectRef {
}
fn matchPathName(
ctx: js.JSContextRef,
@@ -174,6 +177,7 @@ pub const Instance = NewClass(
.@"tsdoc" = "URL path as appears in a web browser's address bar",
},
},
.filePath = .{
.get = getFilePath,
.ro = true,
@@ -336,6 +340,7 @@ pub fn createQueryObject(ctx: js.JSContextRef, map: *QueryStringMap, exception:
return value.asRef();
}
threadlocal var entry_point_tempbuf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
pub fn getScriptSrc(
this: *Router,
ctx: js.JSContextRef,
@@ -344,11 +349,25 @@ pub fn getScriptSrc(
exception: js.ExceptionRef,
) js.JSValueRef {
const src = this.script_src orelse brk: {
// We don't store the framework config including the client parts in the server
// instead, we just store a boolean saying whether we should generate this whenever the script is requested
// this is kind of bad. we should consider instead a way to inline the contents of the script.
var writer = this.script_src_buf_writer.writer();
JavaScript.Wundle.getPublicPath(this.route.file_path, ScriptSrcStream.Writer, writer);
if (this.route.client_framework_enabled) {
JavaScript.Wundle.getPublicPath(
Bundler.ClientEntryPoint.generateEntryPointPath(
&entry_point_tempbuf,
Fs.PathName.init(this.route.file_path),
),
ScriptSrcStream.Writer,
writer,
);
} else {
JavaScript.Wundle.getPublicPath(this.route.file_path, ScriptSrcStream.Writer, writer);
}
break :brk this.script_src_buf[0..this.script_src_buf_writer.pos];
};
this.script_src = src;

View File

@@ -110,6 +110,34 @@ pub const Wundle = struct {
return ZigString.init(VirtualMachine.vm.main).toValue(VirtualMachine.vm.global).asRef();
}
pub fn getRoutesDir(
this: void,
ctx: js.JSContextRef,
thisObject: js.JSValueRef,
prop: js.JSStringRef,
exception: js.ExceptionRef,
) js.JSValueRef {
if (!VirtualMachine.vm.bundler.options.routes.routes_enabled or VirtualMachine.vm.bundler.options.routes.dir.len > 0) {
return js.JSValueMakeUndefined(ctx);
}
return ZigString.init(VirtualMachine.vm.bundler.options.routes.dir).toValue(VirtualMachine.vm.global).asRef();
}
pub fn routeByName(
this: void,
ctx: js.JSContextRef,
thisObject: js.JSValueRef,
prop: js.JSStringRef,
exception: js.ExceptionRef,
) js.JSValueRef {
if (!VirtualMachine.vm.bundler.options.routes.routes_enabled) {
return js.JSValueMakeUndefined(ctx);
}
return ZigString.init(VirtualMachine.vm.bundler.options.routes.dir).toValue(VirtualMachine.vm.global).asRef();
}
pub fn getImportedStyles(
this: void,
ctx: js.JSContextRef,
@@ -181,6 +209,10 @@ pub const Wundle = struct {
.get = getOrigin,
.ts = d.ts{ .name = "origin", .@"return" = "string" },
},
.routesDir = .{
.get = getRoutesDir,
.ts = d.ts{ .name = "routesDir", .@"return" = "string" },
},
},
);
};
@@ -349,6 +381,7 @@ pub const VirtualMachine = struct {
0,
fd,
hash,
null,
) orelse {
return error.ParseError;
};