const fs = require('fs'); const path = require('path'); // 修复支付宝小程序的EventSource错误 function fixAlipayEventSource() { const taroJsPath = path.join(__dirname, '../dist/taro.js'); if (!fs.existsSync(taroJsPath)) { console.log('Error: dist/taro.js not found'); return false; } console.log('Fixing EventSource in dist/taro.js...'); let content = fs.readFileSync(taroJsPath, 'utf8'); // 查找并替换EventSource类 const oldEventSource = /class EventSource extends Map \{[\s\S]*?\}/g; const newEventSource = `class EventSource { data = {}; get(id) { return this.data[id]; } set(id, value) { this.data[id] = value; } delete(id) { delete this.data[id]; } has(id) { return id in this.data; } removeNode(child) { const { sid, uid } = child; this.delete(sid); if (uid !== sid && uid) this.delete(uid); } removeNodeTree(child) { this.removeNode(child); const { childNodes } = child; childNodes.forEach((node) => this.removeNodeTree(node)); } }`; if (oldEventSource.test(content)) { content = content.replace(oldEventSource, newEventSource); fs.writeFileSync(taroJsPath, content, 'utf8'); console.log('Success: EventSource fixed'); return true; } else { console.log('Info: EventSource pattern not found, skipping'); return false; } } // 导出函数 module.exports = fixAlipayEventSource; // 如果直接运行此脚本 if (require.main === module) { fixAlipayEventSource(); }