javascript - 如何在 Electron 渲染器进程中使用 RxJs 等模块?

标签 javascript typescript rxjs electron

我正在尝试将 RxJs(或任何其他非节点库,例如)添加到启用 contextIsolation 的 Electron 渲染器进程中。我也在使用 typescript 。
如果我需要或在 renderer.ts 中导入“rxjs”,加载失败:Uncaught ReferenceError: exports is not defined.我查看了其他可能是 typescript 配置问题的解决方案,但是目标和模块设置的各种排列似乎没有什么区别。
当前设置。

"target": "es5",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
    "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
    // "lib": [],       
在 main.js 中
    var mainWindow = new BrowserWindow({
        width: 1600,
        height: 600,
        webPreferences: {
            preload: path.join(__dirname, 'dist/preload.js'),
            enableRemoteModule: false,
            nodeIntegration: false,
            contextIsolation: true,
            sandbox: true
        }
    });
将它单独添加到 renderer.ts 编译的顶部,如果我尝试使用 rxjs,实际上需要编译,但不会加载到 index.html
import { BehaviorSubject } from 'rxjs';
其他一切基本上都是 Electron 样板。

最佳答案

我已经部分解决了这个问题,但没有完全理解这个问题。 javascript/node 中的模块情况非常困惑。
基本上,要获得默认 tsc --init配置工作,你需要一个模块加载器,比如 webpack。这是我最终添加的内容,我不会再搞砸了。
在此之前,我尝试切换到 native 浏览器模块。要在 tsconfig 中执行此操作,我相信正确的设置是

"target": "es5",
"module": "es2015",
"moduleResolution": "node",
您还必须添加 type="module"给您的script标签。
问题在于 mainProcess 代码搞砸了。在我的 mainProcess 代码中,我使用了 require用于节点模块和 import对于我自己的 typescript 类,因为我想要代码完成和 typescript 编译器检查。我想如果我想使用 native 导入或以某种方式配置 typescript 来处理不同于渲染进程代码的主进程代码,我将不得不切换到一种或另一种格式。这看起来工作量太大,所以我只是添加了 webpack 来捆绑我的渲染器端。

关于javascript - 如何在 Electron 渲染器进程中使用 RxJs 等模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64996144/

相关文章:

javascript - 通过字符串路径设置嵌套对象属性

javascript - D3 同心/嵌套圆环图

javascript - 当我在我的 Angular 应用程序中进行验证时,我面临在类型'AbstractControl []错误上找不到类型为 'string' 的参数的索引签名

typescript - 关于特殊组件定义的 JSX 正确类型化的问题

javascript - jQuery if data 属性添加类

javascript - backgroundPositionX 不适用于 Firefox

javascript - eslint 错误不安全的成员访问 ['content-type'] 在任何值上

javascript - 为什么这个简单的 rxjs 示例没有按预期运行

javascript - RxJS combineLatest 与 Angular 中的 startsWith 给出错误

javascript - 检查 Observable 的条件并执行/返回函数(angular2、rxjs)