我正在尝试将 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.htmlimport { 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/