Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot read properties of undefined (reading '__GARFISH_EXPORTS__') #666

Open
5 tasks done
festina-lente-z opened this issue Mar 30, 2024 · 9 comments
Open
5 tasks done

Comments

@festina-lente-z
Copy link

festina-lente-z commented Mar 30, 2024

复现 demo

https://github.com/festina-lente-z/react-vite-garfish-demo/tree/feat/garfish-exports-undefined

问题

react子应用关闭沙箱,vite启动时,引入 rxjs 报错,但是打开沙箱引入 GarfishEsModule() 插件正常显示。报错如下图所示:

image

我自己这边进行排查之后,发现 rxjs 和 garfish 的 window 冲突了。

我在源码中加入了 console.log(window),代码如下

const blobUrl = this.createBlobUrl(`import.meta.url='${url}';console.log(window);${this.app.isNoEntryScript(url) ? noEntryEnvVarStr : envVarStr}${scriptCode}
${sourcemap}`);

当运行到 rxjs 内部时,打印的 window 与其他依赖不一致:

image

请问有比较好的解决方案吗?

Reproduction

https://github.com/festina-lente-z/react-vite-garfish-demo/tree/feat/garfish-exports-undefined

Used Package Manager

pnpm

System Info

System:
    OS: macOS 14.2.1
    CPU: (8) arm64 Apple M2
    Memory: 67.70 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.19.1 - ~/.nvm/versions/node/v18.19.1/bin/node
    Yarn: 1.22.21 - /opt/homebrew/bin/yarn
    npm: 10.2.4 - ~/.nvm/versions/node/v18.19.1/bin/npm
    pnpm: 8.15.3 - /opt/homebrew/bin/pnpm
  Browsers:
    Chrome: 123.0.6312.87
    Safari: 17.2.1
  npmPackages:
    @garfish/bridge-react-v18: ^1.17.5 => 1.17.5
    @garfish/es-module: ^1.17.5 => 1.17.5
    garfish: ^1.17.5 => 1.17.5

Validations

@zhoushaw
Copy link
Member

image
我不知道为什么你的 example 里的子应用为什么用的都是主应用的 html ,你可以在子应用的 html 里通过动态 import 通过上面图片的方式再次导出就行,你可以试一下,不过因为你的 example 用的都是主应用的,我不是太好去验证,你可以按照这个思路试试看

@festina-lente-z
Copy link
Author

image 我不知道为什么你的 example 里的子应用为什么用的都是主应用的 html ,你可以在子应用的 html 里通过动态 import 通过上面图片的方式再次导出就行,你可以试一下,不过因为你的 example 用的都是主应用的,我不是太好去验证,你可以按照这个思路试试看

@zhoushaw 加上动态 import这段,前面 <script type="module" src="/main/index.tsx"></script> 需要去掉吗?我在本地试的把 <script type="module" src="/main/index.tsx"></script> 去掉才可以正常运行

@zhoushaw
Copy link
Member

zhoushaw commented Apr 8, 2024

@festina-lente-z 是的,下面有加载就不需要重新设置了

@festina-lente-z
Copy link
Author

@festina-lente-z 是的,下面有加载就不需要重新设置了

👌

@yubignhua
Copy link

我也遇到了相同问题,子应用引入 rxjs 报错: var GARFISH_EXPORTS = window.__GARFISH_ESM_ENV___0.GARFISH_EXPORTS; 报错: Cannot read properties of undefined (reading 'GARFISH_EXPORTS') , 尝试了你的方法还是报同样的错误

image

image 我不知道为什么你的 example 里的子应用为什么用的都是主应用的 html ,你可以在子应用的 html 里通过动态 import 通过上面图片的方式再次导出就行,你可以试一下,不过因为你的 example 用的都是主应用的,我不是太好去验证,你可以按照这个思路试试看

@yubignhua
Copy link

image
image

@festina-lente-z
Copy link
Author

@yubignhua 动态 import 的话,
20240515101512
这行需要去掉

@yubignhua
Copy link

改为动态引入:
image

报错:
image

版本:
"garfish": "^1.17.6",
"@garfish/bridge-vue-v3": "^1.17.6",
"rxjs": "^7.8.1",
"vue": "3.3.0",
"vite": "^5.2.8",

@festina-lente-z
Copy link
Author

改为动态引入: image

报错: image

版本: "garfish": "^1.17.6", "@garfish/bridge-vue-v3": "^1.17.6", "rxjs": "^7.8.1", "vue": "3.3.0", "vite": "^5.2.8",

@yubignhua main.ts不是子应用入口文件吧?main.ts你定义provider了吗?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants