🌳
pt0/deployF/devpconlyF/doBuildWorkerF.mts
1import esbuild, { type Plugin, type OnResolveArgs } from 'esbuild'
6import { existsSync } from 'fs'
7import path from 'path'
8import { createRequire } from 'module'
9const require = createRequire(import.meta.url)
11const automergeAliasPlugin: Plugin = {
12 name: 'automerge-alias',
13 setup(build) {
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}
21 }
22 return null
23 })
24 }
27export const doBuildWorker = async ({doWatch}: {doWatch?: boolean}={}) => {
28 const {appPath} = getAppCfg()
30 const ptOutPath = pathDownJoin(appPath!, 'public', getReqPubEnvItem('builtWorkerFilename') as string)
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
36 const absOutPath = pathDownJoin(ptDir, ptOutPath)
37 const buildOptions = {
38 entryPoints: [
39 pathDownJoin(ptDir, resolvedInPath)
40 ],
41 bundle: true,
42 outfile: absOutPath,
43 format: 'esm' as const,
44 minify: true,
45 plugins: [automergeAliasPlugin],
46 }
48 await esbuild.build(buildOptions)
49 console.log('builtworker', resolvedInPath, '->', ptOutPath)
51 if (doWatch) {
52 const ctx = await esbuild.context(buildOptions)
53 await ctx.watch()
54 console.log('Watching worker for changes...')
55 }