const fs = require('fs'); const path = require('path'); class AlipayEventSourceFixPlugin { apply(compiler) { compiler.hooks.afterEmit.tapAsync('AlipayEventSourceFixPlugin', (compilation, callback) => { this.fixEventSource(); callback(); }); } fixEventSource() { const taroJsPath = path.join(process.cwd(), 'dist', 'taro.js'); if (!fs.existsSync(taroJsPath)) { console.log('AlipayEventSourceFixPlugin: dist/taro.js not found'); return; } console.log('AlipayEventSourceFixPlugin: Fixing EventSource in dist/taro.js...'); let content = fs.readFileSync(taroJsPath, 'utf8'); let replaced = false; // 新策略: Taro 4.x 压缩后的 EventSource (class X extends Map with removeNode) if (content.includes('extends Map{removeNode(')) { console.log('AlipayEventSourceFixPlugin: Found minified EventSource (extends Map with removeNode)'); const minifiedPattern = /class \w+ extends Map\{removeNode\([\s\S]*?removeNodeTree\([\s\S]*?\}\}const r=new \w+/g; if (minifiedPattern.test(content)) { 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'; content = content.replace(minifiedPattern, replacement); fs.writeFileSync(taroJsPath, content, 'utf8'); console.log('AlipayEventSourceFixPlugin: Success: Minified EventSource replaced'); replaced = true; } } // 原有策略: 处理未压缩的 EventSource if (!replaced && content.includes('EventSource')) { console.log('AlipayEventSourceFixPlugin: Found EventSource in dist/taro.js'); const newEventSource = 'class EventSource {\n data = {};\n\n get(id) {\n return this.data[id];\n }\n\n set(id, value) {\n this.data[id] = value;\n }\n\n delete(id) {\n delete this.data[id];\n }\n\n has(id) {\n return id in this.data;\n }\n\n removeNode(child) {\n const { sid, uid } = child;\n this.delete(sid);\n if (uid !== sid && uid) this.delete(uid);\n }\n\n removeNodeTree(child) {\n this.removeNode(child);\n const { childNodes } = child;\n childNodes.forEach((node) => this.removeNodeTree(node));\n }\n}'; // 方式1:查找并替换event-source.js模块 const eventSourceModulePattern = /\/\*\*\*\.\/node_modules\/@tarojs\/runtime\/dist\/dom\/event-source\.js[\s\S]*?class EventSource[\s\S]*?\/\*\*\*\/\s*\}\);/g; if (eventSourceModulePattern.test(content)) { console.log('AlipayEventSourceFixPlugin: Found event-source.js module'); const matches = content.match(eventSourceModulePattern); if (matches && matches.length > 0) { const oldModule = matches[0]; const moduleStart = oldModule.substring(0, oldModule.indexOf('class EventSource')); const moduleEnd = oldModule.substring(oldModule.lastIndexOf('*/') + 2); const newModule = moduleStart + newEventSource + moduleEnd; content = content.replace(oldModule, newModule); fs.writeFileSync(taroJsPath, content, 'utf8'); console.log('AlipayEventSourceFixPlugin: Success: EventSource module replaced'); replaced = true; } } // 方式2:直接查找EventSource类定义 if (!replaced) { const eventSourceStart = content.indexOf('class EventSource'); if (eventSourceStart !== -1) { console.log('AlipayEventSourceFixPlugin: Found EventSource class'); let braceCount = 0; let eventSourceEnd = eventSourceStart; let foundOpeningBrace = false; for (let i = eventSourceStart; i < content.length; i++) { if (content[i] === '{') { braceCount++; foundOpeningBrace = true; } else if (content[i] === '}') { braceCount--; if (foundOpeningBrace && braceCount === 0) { eventSourceEnd = i + 1; break; } } } if (eventSourceEnd > eventSourceStart) { const oldEventSource = content.substring(eventSourceStart, eventSourceEnd); content = content.replace(oldEventSource, newEventSource); fs.writeFileSync(taroJsPath, content, 'utf8'); console.log('AlipayEventSourceFixPlugin: Success: EventSource class replaced'); replaced = true; } } } if (!replaced) { console.log('AlipayEventSourceFixPlugin: Info: EventSource not found or could not be replaced'); } } if (!replaced) { console.log('AlipayEventSourceFixPlugin: Info: No EventSource pattern found in dist/taro.js'); } } } module.exports = AlipayEventSourceFixPlugin;