Iterate on a module locally, and dynamically assemble it with ESBuild.
Especially useful with npm link but can also work on arbitrary entrypoints.
| import { watch } from 'node:fs'; | |
| import { fileURLToPath } from 'node:url'; | |
| import { build, } from 'esbuild'; | |
| let webvisionBundle = await buildBundle(); | |
| const webvisionEntrypoint = fileURLToPath(import.meta.resolve('@athree/webvision')); | |
| const watchTarget = webvisionEntrypoint.replace('out/main/index.js', 'out'); | |
| watch(watchTarget, { | |
| recursive: true, | |
| }, async () => { | |
| webvisionBundle = await buildBundle(); | |
| }); | |
| export function getWebvisionModule() { | |
| return webvisionBundle.outputFiles[0].text; | |
| } | |
| export function getWebvisionUrl() { | |
| const webvisionModule = getWebvisionModule(); | |
| return `data:text/javascript;base64,${Buffer.from(webvisionModule).toString('base64')}`; | |
| } | |
| async function buildBundle() { | |
| return await build({ | |
| entryPoints: ['@athree/webvision'], | |
| format: 'esm', | |
| write: false, | |
| bundle: true, | |
| minify: true, | |
| keepNames: true, | |
| logLevel: 'info' | |
| }); | |
| } |