1import esbuild, { type Plugin, type OnResolveArgs } from 'esbuild' 6import { existsSync } from 'fs' 8import { createRequire } from 'module' 9const require = createRequire(import.meta.url) 11const automergeAliasPlugin: Plugin = { 12 name: 'automerge-alias', 14 const automergeCjsPath = require.resolve('@automerge/automerge') 15 const automergeDistDir = path.join(path.dirname(automergeCjsPath), '..') 16 const slimPath = path.join(automergeDistDir, 'mjs/entrypoints/slim.js') 18 build.onResolve({filter: /^@automerge\/automerge/}, (args: OnResolveArgs) => { 19 if (args.path === '@automerge/automerge' || args.path === '@automerge/automerge/slim') { 20 return {path: slimPath} 27export const doBuildWorker = async ({doWatch}: {doWatch?: boolean}={}) => { 31 const baseName = (getReqPubEnvItem('builtWorkerFilename') as string).replace(/\.js$/, '') 32 const ptInPath = pathDownJoin(appPath!, 'entrypoints', `${baseName}AI.mjs`) 33 const ptInPathAlt = pathDownJoin(appPath!, 'entrypoints', `${baseName}.mjs`) 34 const resolvedInPath = existsSync(pathDownJoin(ptDir, ptInPath)) ? ptInPath : ptInPathAlt 37 const buildOptions = { 43 format: 'esm' as const, 45 plugins: [automergeAliasPlugin], 48 await esbuild.build(buildOptions) 49 console.log('builtworker', resolvedInPath, '->', ptOutPath) 52 const ctx = await esbuild.context(buildOptions) 54 console.log('Watching worker for changes...')