Files
memind/scripts/build-wechat-mp-runtime.mjs
T
john 6bc5d128d2 fix(wechat): deliver failure notices and hot-swappable wechat-mp bundle
Notify users when HTML publish guards reject stub pages instead of silently
failing, send real page links when non-stub files exist, and split wechat-mp
into wechat-mp.bundle.mjs for fast production updates without full portal rebuilds.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-04 12:52:59 +08:00

58 lines
1.8 KiB
JavaScript

#!/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 = [
'undici',
'mysql2',
'mysql2/promise',
'sharp',
'fsevents',
];
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);
});