#!/usr/bin/env node import fs from 'node:fs/promises'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { spawn } from 'node:child_process'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const root = path.join(__dirname, '..'); const runtimeRoot = path.join(root, '.runtime', 'portal'); const esbuildBin = path.join(root, 'node_modules', '.pnpm', 'node_modules', '.bin', 'esbuild'); const runtimeNodeTarget = process.env.PORTAL_RUNTIME_NODE_TARGET || 'node24'; const outputArg = process.argv.find((arg) => arg.startsWith('--outfile=')); const outfile = outputArg ? outputArg.slice('--outfile='.length) : path.join(runtimeRoot, 'wechat-mp.bundle.mjs'); const externalPackages = [ '@img/sharp-darwin-arm64', '@img/sharp-libvips-darwin-arm64', '@node-rs/argon2', '@resvg/resvg-js', 'undici', 'mysql2', 'mysql2/promise', 'sharp', 'fsevents', 'playwright', 'playwright-core', 'chromium-bidi', 'chromium-bidi/*', ]; function run(command, args) { return new Promise((resolve, reject) => { const child = spawn(command, args, { cwd: root, stdio: 'inherit', env: process.env }); child.on('exit', (code) => { if (code === 0) resolve(); else reject(new Error(`${command} ${args.join(' ')} exited with code ${code ?? 1}`)); }); child.on('error', reject); }); } async function main() { await fs.mkdir(path.dirname(outfile), { recursive: true }); const args = [ 'wechat-mp.mjs', '--bundle', '--platform=node', '--format=esm', `--target=${runtimeNodeTarget}`, `--outfile=${outfile}`, '--banner:js=import { createRequire as __createRequire } from "node:module"; const require = __createRequire(import.meta.url);', ]; for (const pkg of externalPackages) { args.push(`--external:${pkg}`); } await run(esbuildBin, args); console.log(`WeChat MP runtime bundle written: ${outfile}`); } main().catch((err) => { console.error(err instanceof Error ? err.message : err); process.exit(1); });