import {build} from 'esbuild'; import {mkdir, rm, readFile} from 'node:fs/promises'; import {join, resolve} from 'node:path'; const root = process.cwd(); const publicRoot = join(root, 'public'); const workerRoot = join(publicRoot, 'tts', 'piper-wasm-v1'); await rm(workerRoot, {recursive: true, force: true}); await mkdir(workerRoot, {recursive: true}); await build({ entryPoints: [resolve(root, 'piper-runtime.js')], outfile: join(publicRoot, 'piper-runtime.js'), bundle: true, format: 'iife', platform: 'browser', target: ['es2022'], external: ['fs', 'path'], sourcemap: false, legalComments: 'none', }); await build({ entryPoints: [resolve(root, 'piper-worker.js')], outfile: join(workerRoot, 'piper-worker.js'), bundle: true, format: 'esm', platform: 'browser', target: ['es2022'], external: ['fs', 'path'], sourcemap: false, legalComments: 'none', }); const workerBundle = await readFile(join(workerRoot, 'piper-worker.js'), 'utf8'); for (const route of [ 'https://huggingface.co/diffusionstudio/piper-voices/resolve/main/', '/static/piper-wasm-v1', '/static/piper-wasm-v1/voices/', '/static/piper-wasm-v1/onnx/', ]) { if (!workerBundle.includes(route)) throw new Error(`The Piper worker is missing static route ${route}`); } console.log(`Piper worker bundle prepared at ${workerRoot}`);