From 01a6622d71f92612e6a617d076c534990306bc1d Mon Sep 17 00:00:00 2001 From: "Tommy D. Rossi" Date: Tue, 3 Feb 2026 13:51:27 +0100 Subject: [PATCH] use virtual entrypoints for wrapper bundles --- playwriter/scripts/build-client-bundles.ts | 45 +++++++++++++--------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/playwriter/scripts/build-client-bundles.ts b/playwriter/scripts/build-client-bundles.ts index d4b25c7..986703e 100644 --- a/playwriter/scripts/build-client-bundles.ts +++ b/playwriter/scripts/build-client-bundles.ts @@ -91,21 +91,35 @@ globalThis.__readability = { Readability, isProbablyReaderable } async function buildBundle(config: BundleConfig): Promise { const startTime = Date.now() - let entryPath: string - let cleanupEntry = false - - if (config.type === 'source') { - entryPath = path.join(srcDir, config.entry) - } else { - // Create temporary entry file for wrapper bundles - const entrySuffix = `${process.pid}-${Date.now()}` - entryPath = path.join(distDir, `_${config.name}-entry-${entrySuffix}.js`) - fs.writeFileSync(entryPath, config.code) - cleanupEntry = true - } + const entrypoint = + config.type === 'source' ? path.join(srcDir, config.entry) : `virtual:${config.name}` + const plugins = + config.type === 'source' + ? [] + : [ + { + name: `virtual-${config.name}`, + setup(build) { + const filter = new RegExp(`^virtual:${config.name}$`) + build.onResolve({ filter }, (args) => { + return { + path: args.path, + namespace: 'virtual', + } + }) + build.onLoad({ filter, namespace: 'virtual' }, () => { + return { + contents: config.code, + loader: 'js', + } + }) + }, + }, + ] const result = await Bun.build({ - entrypoints: [entryPath], + entrypoints: [entrypoint], + plugins, target: 'browser', format: 'iife', define: { @@ -113,11 +127,6 @@ async function buildBundle(config: BundleConfig): Promise { }, }) - // Cleanup temporary entry file - if (cleanupEntry && fs.existsSync(entryPath)) { - fs.unlinkSync(entryPath) - } - if (!result.success) { console.error(`Bundle errors for ${config.name}:`, result.logs) throw new Error(`Failed to bundle ${config.name}`)