Skip to content

Commit 7afecf0

Browse files
committed
move mod specifier resolution js/context => script manager
1 parent 1b462da commit 7afecf0

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

src/browser/ScriptManager.zig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,16 @@ pub fn addFromElement(self: *ScriptManager, element: *parser.Element, comptime c
248248
});
249249
}
250250

251+
// Resolve a module specifier to an valid URL.
252+
pub fn resolveSpecifier(_: *ScriptManager, arena: Allocator, specifier: []const u8, base: []const u8) ![:0]const u8 {
253+
return URL.stitch(
254+
arena,
255+
specifier,
256+
base,
257+
.{ .alloc = .if_needed, .null_terminated = true },
258+
);
259+
}
260+
251261
pub fn getModule(self: *ScriptManager, url: [:0]const u8, referrer: []const u8) !void {
252262
const gop = try self.sync_modules.getOrPut(self.allocator, url);
253263
if (gop.found_existing) {

src/browser/html/elements.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,6 +1353,7 @@ test "Browser: HTML.HtmlScriptElement" {
13531353
try testing.htmlRunner("html/script/inline_defer.html");
13541354
try testing.htmlRunner("html/script/import.html");
13551355
try testing.htmlRunner("html/script/dynamic_import.html");
1356+
try testing.htmlRunner("html/script/importmap.html");
13561357
}
13571358

13581359
test "Browser: HTML.HtmlSlotElement" {

src/browser/js/Context.zig

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,10 @@ pub fn module(self: *Context, comptime want_result: bool, src: []const u8, url:
251251
for (0..requests.length()) |i| {
252252
const req = requests.get(v8_context, @intCast(i)).castTo(v8.ModuleRequest);
253253
const specifier = try self.jsStringToZig(req.getSpecifier(), .{});
254-
const normalized_specifier = try @import("../../url.zig").stitch(
254+
const normalized_specifier = try self.script_manager.?.resolveSpecifier(
255255
self.call_arena,
256256
specifier,
257257
owned_url,
258-
.{ .alloc = .if_needed, .null_terminated = true },
259258
);
260259
const gop = try self.module_cache.getOrPut(self.arena, normalized_specifier);
261260
if (!gop.found_existing) {
@@ -1127,11 +1126,10 @@ pub fn dynamicModuleCallback(
11271126
return @constCast(self.rejectPromise("Out of memory").handle);
11281127
};
11291128

1130-
const normalized_specifier = @import("../../url.zig").stitch(
1129+
const normalized_specifier = self.script_manager.?.resolveSpecifier(
11311130
self.arena, // might need to survive until the module is loaded
11321131
specifier,
11331132
resource,
1134-
.{ .alloc = .if_needed, .null_terminated = true },
11351133
) catch |err| {
11361134
log.err(.app, "OOM", .{ .err = err, .src = "dynamicModuleCallback3" });
11371135
return @constCast(self.rejectPromise("Out of memory").handle);
@@ -1171,11 +1169,10 @@ fn _resolveModuleCallback(self: *Context, referrer: v8.Module, specifier: []cons
11711169
return error.UnknownModuleReferrer;
11721170
};
11731171

1174-
const normalized_specifier = try @import("../../url.zig").stitch(
1172+
const normalized_specifier = try self.script_manager.?.resolveSpecifier(
11751173
self.call_arena,
11761174
specifier,
11771175
referrer_path,
1178-
.{ .alloc = .if_needed, .null_terminated = true },
11791176
);
11801177

11811178
const gop = try self.module_cache.getOrPut(self.arena, normalized_specifier);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE html>
2+
3+
<script src="../../testing.js"></script>
4+
5+
<script id=importmap type=importmap>
6+
{
7+
"imports": {
8+
"core": "./import.js",
9+
}
10+
}
11+
</script>
12+
13+
<script id=use_importmap type=module>
14+
import * as im from 'core';
15+
testing.expectEqual('hello', im.greeting);
16+
</script>
17+
18+
<script id=cached_importmpa type=module>
19+
// hopefully cached, who knows, no real way to assert this
20+
// but at least it works.
21+
import * as im from 'core';
22+
testing.expectEqual('hello', im.greeting);
23+
</script>
24+

0 commit comments

Comments
 (0)