From 0ff8edebd9f15c1940e6aaf7a29de894ee153ece Mon Sep 17 00:00:00 2001 From: iddev5 Date: Wed, 6 Jul 2022 17:50:12 +0530 Subject: [PATCH] mach: Add js-runtime as a dependency for wasm applications This is a workaround for a limitation right now. The html-generator has no way to dynamically add JS sources (and that can't be done without using a preprocessor library) so we hardcode js-runtime in it. In the future, I think the correct behavior would be to move tools/ inside js-runtime along with a wasm application building SDK and get rid of any direct JS access we have today (which is just src/platform/wasm.zig and src/platform/mach.js). --- build.zig | 17 +++++++++++------ www/template.html | 4 +++- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/build.zig b/build.zig index 941a244b..ad05fb2c 100644 --- a/build.zig +++ b/build.zig @@ -5,6 +5,7 @@ const gpu_dawn = @import("gpu-dawn/build.zig"); pub const glfw = @import("glfw/build.zig"); pub const ecs = @import("ecs/build.zig"); const freetype = @import("freetype/build.zig"); +const js_runtime = @import("js-runtime/build.zig"); const Pkg = std.build.Pkg; pub fn build(b: *std.build.Builder) void { @@ -162,6 +163,7 @@ pub const App = struct { if (options.target.toTarget().cpu.arch == .wasm32) { const lib = b.addSharedLibrary(options.name, thisDir() ++ "/src/platform/wasm.zig", .unversioned); lib.addPackage(gpu.pkg); + lib.addPackage(js_runtime.pkg); break :blk lib; } else { @@ -194,12 +196,14 @@ pub const App = struct { // Set install directory to '{prefix}/www' app.getInstallStep().?.dest_dir = web_install_dir; - const install_mach_js = app.b.addInstallFileWithDir( - .{ .path = thisDir() ++ "/src/platform/mach.js" }, - web_install_dir, - "mach.js", - ); - app.getInstallStep().?.step.dependOn(&install_mach_js.step); + inline for (.{ "/src/platform/mach.js", "/js-runtime/src/js-runtime.js" }) |js| { + const install_js = app.b.addInstallFileWithDir( + .{ .path = thisDir() ++ js }, + web_install_dir, + std.fs.path.basename(js), + ); + app.getInstallStep().?.step.dependOn(&install_js.step); + } const html_generator = app.b.addExecutable("html-generator", thisDir() ++ "/tools/html-generator.zig"); html_generator.main_pkg_path = thisDir(); @@ -209,6 +213,7 @@ pub const App = struct { u8, &.{ app.name, ".html" }, ) catch unreachable, app.name }); + run_html_generator.cwd = app.b.getInstallPath(web_install_dir, ""); app.getInstallStep().?.step.dependOn(&run_html_generator.step); } diff --git a/www/template.html b/www/template.html index d163c273..68366fda 100644 --- a/www/template.html +++ b/www/template.html @@ -6,9 +6,10 @@