| 24 | }); |
| 25 | |
| 26 | export const workerBundlerArtifact = (): Plugin => { |
| 27 | let command: "build" | "serve" = "build"; |
| 28 | |
| 29 | return { |
| 30 | name: "executor-worker-bundler-artifact", |
| 31 | configResolved(config) { |
| 32 | command = config.command; |
| 33 | }, |
| 34 | resolveId(id) { |
| 35 | return id === virtualId ? resolvedVirtualId : null; |
| 36 | }, |
| 37 | async load(id) { |
| 38 | if (id !== resolvedVirtualId) return null; |
| 39 | const artifact = await bundledWorkerBundler(); |
| 40 | const paths = artifactPaths(artifact); |
| 41 | const output = [ |
| 42 | `export const sourcePath = ${JSON.stringify(paths.sourcePath)};`, |
| 43 | `export const wasmPath = ${JSON.stringify(paths.wasmPath)};`, |
| 44 | ]; |
| 45 | |
| 46 | if (command === "serve") { |
| 47 | output.push( |
| 48 | `export const source = ${JSON.stringify(artifact.source)};`, |
| 49 | `export const wasmBase64 = ${JSON.stringify(Buffer.from(artifact.wasm).toString("base64"))};`, |
| 50 | "export default { sourcePath, wasmPath, source, wasmBase64 };", |
| 51 | ); |
| 52 | } else { |
| 53 | output.push( |
| 54 | "export const source = undefined;", |
| 55 | "export const wasmBase64 = undefined;", |
| 56 | "export default { sourcePath, wasmPath, source, wasmBase64 };", |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | return output.join("\n"); |
| 61 | }, |
| 62 | async closeBundle() { |
| 63 | if (command !== "build") return; |
| 64 | const artifact = await bundledWorkerBundler(); |
| 65 | const paths = artifactPaths(artifact); |
| 66 | const outputDir = join(process.cwd(), clientAssetsDir); |
| 67 | await mkdir(outputDir, { recursive: true }); |
| 68 | await Promise.all([ |
| 69 | writeFile(join(process.cwd(), "dist/client", paths.sourcePath), artifact.source), |
| 70 | writeFile(join(process.cwd(), "dist/client", paths.wasmPath), artifact.wasm), |
| 71 | ]); |
| 72 | }, |
| 73 | }; |
| 74 | }; |