import * as esbuild from 'esbuild'; import { resolve } from 'path'; import { fileURLToPath } from 'url'; import { dirname } from 'path'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); try { await esbuild.build({ entryPoints: [resolve(__dirname, 'src/index.js')], bundle: true, platform: 'node', target: 'node16', // Capacitor NodeJS plugin likely uses a somewhat recent Node version outfile: resolve(__dirname, '../dist/nodejs-v3/index.js'), external: [ // Mark native modules as external if necessary. // fluent-ffmpeg spawns a process, so it's fine as long as the binary is there (or it fails gracefully). // It doesn't need to be bundled, but the package wrapper code should be bundled. // We explicitly bundle everything by default. // If we encounter specific issues with native addons, we add them here. 'fsevents', // macOS only 'electron', // Not available in this environment ], loader: { '.node': 'file', // Handle .node files if any }, // Minimization isn't strictly necessary but reduces file size to copy minify: false, sourcemap: 'inline', format: 'cjs', // The plugin might expect CommonJS or ESM. It loads 'index.js'. // Given the previous 'nodejs-entry.js' effectively did `import(...)`, ESM is supported. // However, for a single bundle, CJS is often safer/simpler if top-level await isn't used. // Let's stick to ESM if possible, or CJS if not. // The original project was type: module. Let's use ESM. format: 'esm', banner: { js: ` import { createRequire } from 'module'; const require = createRequire(import.meta.url); const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); `, }, }); console.log('⚡ Bundle build complete: ../dist/nodejs-v3/index.js'); } catch (e) { console.error('Build failed:', e); process.exit(1); }