const fs = require('fs'); const path = require('path'); 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'); const replacement = 'class EventSource{data={};get(e){return this.data[e]};set(e,t){this.data[e]=t};delete(e){delete this.data[e]};has(e){return e in this.data};removeNode(e){const{sid:t,uid:n}=e;this.delete(t);n!==t&&n&&this.delete(n)};removeNodeTree(e){this.removeNode(e);const{childNodes:t}=e;t.forEach(e=>this.removeNodeTree(e))}}const r=new EventSource'; // Taro 4.x: 类名被压缩 (如 class i extends Map) → 通过 removeNode 特征匹配 const minifiedPattern = /class \w+ extends Map\{removeNode\([\s\S]*?removeNodeTree\([\s\S]*?\}\}const r=new \w+/g; if (minifiedPattern.test(content)) { content = content.replace(minifiedPattern, replacement); fs.writeFileSync(taroJsPath, content, 'utf8'); console.log('Success: Minified EventSource (Map) fixed'); return true; } // Taro 3.x: 未压缩的 class EventSource extends Map 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; } console.log('Info: EventSource pattern not found, skipping'); return false; } module.exports = fixAlipayEventSource; if (require.main === module) { fixAlipayEventSource(); }