简体   繁体   English

在汇总中为 Electron 保留 node.js 内置导入

[英]Preserving node.js built-in imports for Electron in rollup

I am making a Electron app with Svelte and Typescript.我正在用 Svelte 和 Typescript 制作一个 Electron 应用程序。 I started with this template for that exact purpose, but it disables node.js built-in imports (like fs) in the browser/electron frontend for security.我从这个模板开始就是为了这个确切的目的,但是为了安全起见,它在浏览器/电子前端禁用了 node.js 内置导入(如 fs)。

I do not need this improved security in my project, so I am trying to get node.js fs to work in the Electron browser.我的项目不需要这种改进的安全性,所以我试图让 node.js fs 在 Electron 浏览器中工作。 I already modified the Electron Backend script that creates the Browser to re-enable nodeIntegration, and this works: using require("fs") in the Electron browser console logs the fs library.我已经修改了创建浏览器以重新启用 nodeIntegration 的 Electron 后端脚本,这很有效:在 Electron 浏览器控制台中使用 require("fs") 记录 fs 库。

Using this in the actual typescript frontend code does not work, however.但是,在实际的打字稿前端代码中使用它是行不通的。 From looking at the bundled JS, it seems like rollup is assuming that the import of fs is just available as a global variable, and trying to guess its name.从捆绑的 JS 来看,rollup 似乎假设 fs 的导入仅可作为全局变量使用,并试图猜测其名称。

When building while importing fs and path, I get the following warnings:在导入 fs 和路径时构建时,我收到以下警告:

(!) Missing shims for Node.js built-ins
Creating a browser bundle that depends on "path". You might need to include https://github.com/snowpackjs/rollup-plugin-polyfill-node
(!) Missing global variable names
Use output.globals to specify browser global variable names corresponding to external modules
fs (guessing 'fs')
path (guessing 'path')

The first warning suggests a 404 GitHub link that seems to be a polyfill for some Node built-in libraries.第一个警告表明 404 GitHub 链接似乎是某些 Node 内置库的 polyfill。 This isn't what I want, I want the real node.js fs library.这不是我想要的,我想要真正的 node.js fs 库。 It also informs me that I'm creating a browser bundle - I have tried setting the browser option of @rollup/plugin-node-resolve (used by the template) to false, but this did not help.它还告诉我我正在创建一个浏览器包 - 我尝试将@rollup/plugin-node-resolve (由模板使用)的browser选项设置为 false,但这没有帮助。

The second warning seems to simply inform that it's trying to guess global variable names - which it should not, it should keep the imports.第二个警告似乎只是通知它正在尝试猜测全局变量名称 - 它不应该这样做,它应该保留导入。

How do I allow importing Node.js modules here?如何允许在此处导入 Node.js 模块? The linked template project still closely resembles my current one.链接的模板项目仍然与我当前的非常相似。 Help is greatly appreciated.非常感谢您的帮助。

Turns out that the deciding factor was the output.format of the rollup.config.js.事实证明,决定因素是 rollup.config.js 的output.format This was set to iife , which produced a result without require or import .这被设置为iife ,它在没有requireimport的情况下产生了结果。

Changing it to cjs solves this problem.将其更改为cjs可以解决此问题。

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

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