简体   繁体   English

我可以在 electron.js 中预加载完整的 javascript 库并在客户端使用它吗?

[英]Can I preload a full javascript library in electron.js and use it on the client side?

I'm creating an Electron.js project.我正在创建一个 Electron.js 项目。 I've added some npm packages for my project.我为我的项目添加了一些 npm 包。 Most of these are for the index.html.其中大部分用于 index.html。 Eg I've intalled vis.network (graphics js lib) for showing graphs.例如,我安装了 vis.network(图形 js 库)来显示图形。 How can I preload these propely and use the libraries on the browser side?我怎样才能正确地预加载这些并在浏览器端使用这些库? Or is there any other useful method to use npm packages on the browser side in Electron?或者还有其他有用的方法可以在 Electron 的浏览器端使用 npm 包吗?

preload.js预加载.js

const { contextBridge } = require("electron")
const { vis } = require("vis-network")

const attachedLibraries = {
    libVis : vis
}

contextBridge.exposeInIsolatedWorld("attachedLibraries", attachedLibraries)

main.js主程序

const createWindow = () => {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true,
      preload : 'preload.js'
    }
  })

  win.loadFile('view/index.html')
}

npm package in node_modules node_modules 中的 npm package

node_modules 中的 npm 包层次结构

You can use the package electron-vite to do this, There are other options (like webpack as well), but this one is pretty well documented, particularly how they structure preload scripts .您可以使用 package electron-vite来执行此操作,还有其他选项(如 webpack 以及),但这一个有很好的记录,特别是它们如何构建 预加载脚本

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

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