From 444349c3f3750ac4de37dfa587eac0271dd0eb91 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Wed, 4 Mar 2026 16:07:38 +0100 Subject: [PATCH] Cleanup plug runtime loading during compilation Note: drops custom worker URL support (which I doubt anybody used) --- bin/plug-compile.ts | 2 -- client/plugos/plug_compile.ts | 30 ++++++++++++++++-------------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/bin/plug-compile.ts b/bin/plug-compile.ts index b5b9708c..f9a27fea 100755 --- a/bin/plug-compile.ts +++ b/bin/plug-compile.ts @@ -16,14 +16,12 @@ program .option("--info", "Print out size info per function", false) .option("-w, --watch", "Watch for changes and rebuild", false) .option("--dist ", "Folder to put the resulting .plug.json file into", ".") - .option("--runtimeUrl ", "URL to worker_runtime.ts to use") .action(async (manifestPaths: string[], options: any) => { await plugCompileCommand( { dist: options.dist, debug: options.debug, info: options.info, - runtimeUrl: options.runtimeUrl, }, ...manifestPaths, ); diff --git a/client/plugos/plug_compile.ts b/client/plugos/plug_compile.ts index 874062eb..f44dc276 100644 --- a/client/plugos/plug_compile.ts +++ b/client/plugos/plug_compile.ts @@ -9,25 +9,30 @@ import type { Manifest } from "./types.ts"; import { version } from "../../version.ts"; import { fileURLToPath } from "node:url"; -import { readFileSync, existsSync } from "node:fs"; +import { existsSync } from "node:fs"; import { dirname } from "node:path"; -// Read the pre-built worker_runtime bundle +// Resolve the pre-built worker_runtime bundle path // When running from source: ../../dist/worker_runtime_bundle.js (from client/plugos/) // When bundled: ./worker_runtime_bundle.js (from dist/) const currentDir = dirname(fileURLToPath(import.meta.url)); const bundledPath = path.join(currentDir, "worker_runtime_bundle.js"); const sourcePath = path.join(currentDir, "../../dist/worker_runtime_bundle.js"); +const workerRuntimeBundlePath = existsSync(bundledPath) + ? bundledPath + : sourcePath; -const workerRuntimeBundlePath = existsSync(bundledPath) ? bundledPath : sourcePath; -const workerRuntimeBundle = readFileSync(workerRuntimeBundlePath, "utf-8"); - -// Create a data URL so esbuild can inline it -const workerRuntimeUrl = `data:text/javascript;charset=utf-8,${encodeURIComponent(workerRuntimeBundle)}`; +const workerRuntimePlugin: esbuild.Plugin = { + name: "worker-runtime", + setup(build) { + build.onResolve({ filter: /^worker-runtime$/ }, () => ({ + path: workerRuntimeBundlePath, + })); + }, +}; export type CompileOptions = { debug?: boolean; - runtimeUrl?: string; // Print info on bundle size info?: boolean; }; @@ -58,9 +63,7 @@ export async function compileManifest( } const jsFile = ` -import { setupMessageListener } from "${ - options.runtimeUrl || workerRuntimeUrl - }"; +import { setupMessageListener } from "worker-runtime"; // Imports ${ @@ -119,6 +122,7 @@ setupMessageListener(functionMapping, manifest, self.postMessage); outfile: outFile, metafile: options.info, treeShaking: true, + plugins: [workerRuntimePlugin], }); if (options.info) { @@ -180,11 +184,10 @@ export function patchBundledJS(code: string): string { } export async function plugCompileCommand( - { dist, debug, info, runtimeUrl }: { + { dist, debug, info }: { dist: string; debug: boolean; info: boolean; - runtimeUrl?: string; }, ...manifestPaths: string[] ) { @@ -194,7 +197,6 @@ export async function plugCompileCommand( { debug: debug, info: info, - runtimeUrl, }, ); esbuild.stop();