import {build} from 'esbuild'; import fs from 'node:fs'; import path from 'node:path'; const root=process.cwd(),vendor=path.join(root,'site/assets/vendor/background-ai');fs.mkdirSync(vendor,{recursive:true}); const source=path.join(root,'node_modules/@imgly/background-removal/dist/index.mjs'); const patched=fs.readFileSync(source,'utf8').replace('var maxNumThreads = () => navigator.hardwareConcurrency ?? 4;','var maxNumThreads = () => 1;'); if(patched===fs.readFileSync(source,'utf8'))throw Error('Thread setting patch did not match the pinned upstream source.'); const entry=path.join(root,'engine-patched.mjs');fs.writeFileSync(entry,patched); await build({entryPoints:[entry],bundle:true,format:'esm',platform:'browser',target:['es2022'],minify:true,outfile:path.join(vendor,'engine.mjs'),alias:{'onnxruntime-web/webgpu':'onnxruntime-web'},legalComments:'linked'}); for(const [source,target]of [['node_modules/@imgly/background-removal/LICENSE.md','LICENSE.md'],['node_modules/@imgly/background-removal/README.md','UPSTREAM-README.md'],['node_modules/@imgly/background-removal/ThirdPartyLicenses.json','ThirdPartyLicenses.json'],['node_modules/onnxruntime-web/LICENSE','ONNXRUNTIME-LICENSE']])if(fs.existsSync(source))fs.copyFileSync(source,path.join(vendor,target)); console.log('Browser inference engine built with one CPU thread; no cross-origin isolation headers needed.');