import * as NodeFSP from "node:fs/promises"; import * as NodePath from "node:path"; import * as NodeURL from "node:url"; import { build } from "vite-plus"; const mobileRoot = NodePath.resolve(NodePath.dirname(NodeURL.fileURLToPath(import.meta.url)), ".."); /** Metro embeds the shared browser transport as a small script in the native WebView. */ export async function generateDeviceStreamScript() { const result = await build({ configFile: false, logLevel: "silent", build: { write: false, target: "es2022", minify: true, lib: { entry: NodePath.join(mobileRoot, "src/features/devices/device-stream.browser.ts"), name: "T3DeviceStream", formats: ["iife"], }, }, }); const bundles = Array.isArray(result) ? result : [result]; const chunk = bundles .flatMap((bundle) => ("output" in bundle ? bundle.output : [])) .find((output) => output.type === "chunk"); if (!chunk) throw new Error("Device stream build did not emit a script."); const root = NodePath.join(mobileRoot, ".generated/device-stream"); await NodeFSP.mkdir(root, { recursive: true }); for (const [name, contents] of [ ["index.js", `module.exports = ${JSON.stringify(chunk.code)};\n`], ["package.json", '{"main":"index.js"}\n'], ] as const) { const destination = NodePath.join(root, name); const previous = await NodeFSP.readFile(destination, "utf8").catch(() => null); if (previous !== contents) await NodeFSP.writeFile(destination, contents); } }