fix-alipay-event-source.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. const fs = require('fs');
  2. const path = require('path');
  3. // 修复支付宝小程序的EventSource错误
  4. function fixAlipayEventSource() {
  5. const taroJsPath = path.join(__dirname, '../dist/taro.js');
  6. if (!fs.existsSync(taroJsPath)) {
  7. console.log('Error: dist/taro.js not found');
  8. return false;
  9. }
  10. console.log('Fixing EventSource in dist/taro.js...');
  11. let content = fs.readFileSync(taroJsPath, 'utf8');
  12. // 查找并替换EventSource类
  13. const oldEventSource = /class EventSource extends Map \{[\s\S]*?\}/g;
  14. const newEventSource = `class EventSource {
  15. data = {};
  16. get(id) {
  17. return this.data[id];
  18. }
  19. set(id, value) {
  20. this.data[id] = value;
  21. }
  22. delete(id) {
  23. delete this.data[id];
  24. }
  25. has(id) {
  26. return id in this.data;
  27. }
  28. removeNode(child) {
  29. const { sid, uid } = child;
  30. this.delete(sid);
  31. if (uid !== sid && uid) this.delete(uid);
  32. }
  33. removeNodeTree(child) {
  34. this.removeNode(child);
  35. const { childNodes } = child;
  36. childNodes.forEach((node) => this.removeNodeTree(node));
  37. }
  38. }`;
  39. if (oldEventSource.test(content)) {
  40. content = content.replace(oldEventSource, newEventSource);
  41. fs.writeFileSync(taroJsPath, content, 'utf8');
  42. console.log('Success: EventSource fixed');
  43. return true;
  44. } else {
  45. console.log('Info: EventSource pattern not found, skipping');
  46. return false;
  47. }
  48. }
  49. // 导出函数
  50. module.exports = fixAlipayEventSource;
  51. // 如果直接运行此脚本
  52. if (require.main === module) {
  53. fixAlipayEventSource();
  54. }