有条件地将电子导入 react-native-web 应用程序

Importing electron conditionally into react-native-web app

我目前有一个 react-native-web 应用程序同时针对 Android 和 Electron。

我已经在应用程序中添加了 Electron 快捷方式,并且我在 React 组件中使用 ipcRenderer 来监听加速器和 运行 React 特定代码。

问题是,除非我将 ipcRenderer 导入 .web.js 文件,否则我在 运行ning Android:

时收到以下错误
Unable to resolve module `fs` from `/Users/.../node_modules/electron/index.js`: Module `fs` does not exist in the Haste module map

导致问题的代码片段:

if (isElectron()) {
    const { ipcRenderer } = require('electron'); <-- this line

我试过像这样 Platform.OS 有条件地包装导入

if (Platform.OS === 'web') {
    const isElectron = require('is-electron');

    if (isElectron()) {
        const { ipcRenderer } = require('electron');

但是我仍然收到同样的错误。

谢谢:)

找到了解决我的问题的方法:)

我做了2个文件

ipcRenderer.js 包含空对象的导出

ipcRenderer.web.js 其中包含 isElectron() 检查并从 electron

导出空值或 ipcRenderer