简体   繁体   English

如何在 Electron 渲染器进程中使用像 RxJs 这样的模块?

[英]How do I use modules like RxJs in the Electron renderer process?

I am trying to add RxJs (or any other non-node lib, for example) to the electron renderer process with contextIsolation enabled.我正在尝试将 RxJs(或任何其他非节点库,例如)添加到启用了 contextIsolation 的电子渲染器进程中。 I am also using Typescript.我也在使用打字稿。

If I require or import 'rxjs' in renderer.ts, The load fails with: Uncaught ReferenceError: exports is not defined.如果我在 renderer.ts 中要求或导入 'rxjs',加载失败: Uncaught ReferenceError: exports is not defined. I have looked at other solutions that this might be a typescript configuration problem, but various permutations of target and module settings do not seem to make a difference.我查看了其他解决方案,认为这可能是打字稿配置问题,但目标和模块设置的各种排列似乎没有区别。

Current setting.当前设置。

"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": [],       

And in main.js在 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
        }
    });

Adding this alone to the top of renderer.ts compiles, and is in fact required to compile if I try to use rxjs, but then won't load in the index.html将它单独添加到 renderer.ts 编译的顶部,如果我尝试使用 rxjs,实际上需要编译,但不会在 index.html 中加载

import { BehaviorSubject } from 'rxjs';

Everything else is basically electron boilderplate.其他一切基本上都是电子样板。

I have solved this partly, without fully understanding the problem.我已经部分解决了这个问题,但没有完全理解这个问题。 The module situation in javascript/node is very confusing. javascript/node 中的模块情况非常混乱。

Basically, to get the default tsc --init config to work, you need a module loader such as webpack.基本上,为了让默认的tsc --init配置工作,你需要一个模块加载器,比如 webpack。 Which is what I ultimately added, and I am not going to mess with it further.这就是我最终添加的内容,我不会进一步弄乱它。

Before doing that I tried to switch to native browser modules.在此之前,我尝试切换到本机浏览器模块。 To do this in tsconfig, I belive the correct setting is要在 tsconfig 中执行此操作,我相信正确的设置是

"target": "es5",
"module": "es2015",
"moduleResolution": "node",

you also have to add type="module" to your script tag.您还必须将type="module"添加到您的script标记中。

The problem with that is then the mainProcess code screws up.问题是 mainProcess 代码搞砸了。 In my mainProcess code I am using require for node modules and import for my own typescript classes because I want code completion and typescript compiler checks.在我的 mainProcess 代码中,我对节点模块使用require并为我自己的打字稿类使用import ,因为我想要代码完成和打字稿编译器检查。 I think I would have to switch to one format or another if I want to use native import or somehow configure typescript to treat main Process code different from render process code.我想如果我想使用本机导入或以某种方式配置打字稿来处理与渲染过程代码不同的主流程代码,我将不得不切换到一种或另一种格式。 Which seemed like too much work, so I just added webpack to bundle my renderer side.这看起来工作量太大,所以我只是添加了 webpack 来捆绑我的渲染器端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM