久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

未在打包的應用程序中加載預加載腳本

preload script not loaded in packaged app(未在打包的應用程序中加載預加載腳本)
本文介紹了未在打包的應用程序中加載預加載腳本的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我制作了一個需要加載預加載 js 文件的小型 Electron 應用程序.

I've made a small Electron app which needs to load a preload js file.

當我用 electron . 啟動應用程序時,它會找到該文件,但是當應用程序被打包時,它不會.

When I start the app with electron ., it finds the file, but when the app is packaged, it doesn't.

在此處撥打電話:

mainWindow = new BrowserWindow({
   width: 800,
   height: 600,
   webPreferences: {
     nodeIntegration: false, 
     nativeWindowOpen: true,
     webSecurity: false,
     preload: path.join(__dirname, 'preload.js')  
  }
})

我的簡化 package.json:

My simplified package.json:

"name": "app",
"version": "1.0.0",
"main": "main.js",
"scripts": {
  "start": "electron .",
  "build": "electron-packager . --platform=win32 --arch=x64 --overwrite"
 }
"devDependencies": {
  "electron": "^1.8.4",
  "electron-packager": "^12.0.1",
}

我的項目結構:

- 節點模塊

- main.js

- preload.js

- preload.js

- package.json

- package.json

我檢查了 path.join 的結果,在這兩種情況下,路徑都是正確的,并且文件在那里.

I've checked the result of the path.join and in both cases, the path is correct, and the file is there.

推薦答案

對于使用 Electron Forge webpack typescript 樣板的人們:

For peoples using Electron Forge webpack typescript boilerplate :

  1. package.json 文件中添加 preload 鍵:
  1. Add the preload key in package.json file:

{
  "config": {
    "forge": {
      "plugins": [
        [
          "@electron-forge/plugin-webpack",
          {
            "mainConfig": "./webpack.main.config.js",
            "renderer": {
              "config": "./webpack.renderer.config.js",
              "entryPoints": [
                {
                  "html": "./src/index.html",
                  "js": "./src/renderer.tsx",
                  "name": "main_window",
                  "preload": {
                    "js": "./src/preload.ts"
                  }
                }
              ]
            }
          }
        ]
      ]
    }
  }
}

預加載腳本可以是打字稿文件.

Preload script can be a typescript file.

  1. 添加 MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY 常量作為 preload 的值:
  1. Add MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY constant as value for preload:

// Tell typescript about this magic constant
declare const MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY: any;

// [...]

  const mainWindow = new BrowserWindow({
    height: 1000,
    width: 1500,
    webPreferences: {
      preload:  MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY,
    }
  });

  1. preload.ts 中寫:

import {
  contextBridge,
  ipcRenderer
} from 'electron';

contextBridge.exposeInMainWorld(
  'electron',
  {
    doThing: () => ipcRenderer.send('do-a-thing')
  }
)

  1. 添加index.d.ts文件,寫入:
  1. Add index.d.ts file, write :

declare global {
  interface Window {
    electron: {
      doThing(): void;
    }
  }
}

  1. 啟動您的應用程序,在您的開發控制臺中您可以輸入 electron 并查看它的定義.

獎勵:為 contextBridge 公開的 API 正確輸入:

BONUS: getting the typing right for the contextBridge exposed API:

為什么要分開 fie ?不確定是否需要,但我不希望在渲染器進程中從包含主進程代碼(如 preload.ts)的文件中導入接口.

Why a separated fie ? Not sure if needed, but I prefer not having to import an interface from a file that contain main process code (like preload.ts) in renderer process.

// exposed-main-api.model.ts

export interface ExposedMainAPI {
  doThat(data: string): Promise<number>;
}

// index.d.ts

declare global {
  interface Window {
    electron: ExposedMainAPI
  }
}

// preload.ts
import {
  contextBridge,
  ipcRenderer
} from 'electron';

const exposedAPI: ExposedAPI = {
  // You are free to omit parameters typing and return type if you feel so.
  // TS know the function type thanks to exposedAPI typing. 
  doThat: (data) => ipcRenderer.invoke('do-that-and-return-promise', data)
};
// note: this assume you have a `ipcMain.handle('do-thing-and-return-promise', ...)` 
// somewhere that return a number.

contextBridge.exposeInMainWorld('electron', exposedAPI);

學分:

  • @deadcoder0904 在 評論 對于以前的答案,這是他鏈接的 github 問題:https://github.com/electron-userland/electron-forge/issues/1590
  • index.d.ts 輸入和 contextBridge 的小示例用法:https://github.com/electron/electron/issues/9920#issuecomment-743803249
  • @deadcoder0904 that gave us the solution in an comment to a previous answer, here is the github issue he link : https://github.com/electron-userland/electron-forge/issues/1590
  • index.d.ts typing and small example usage of contextBridge: https://github.com/electron/electron/issues/9920#issuecomment-743803249

另見:

  • 關于安全性的說明以及使用 IPC 的示例:https://github.com/electron/electron/issues/9920#issuecomment-575839738

這篇關于未在打包的應用程序中加載預加載腳本的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to fix BrowserWindow is not a constructor error when creating child window in Electron renderer process(在 Electron 渲染器進程中創建子窗口時如何修復 BrowserWindow 不是構造函數錯誤) - IT屋-程序員軟件開發技術
mainWindow.loadURL(quot;https://localhost:3000/quot;) show white screen on Electron app(mainWindow.loadURL(https://localhost:3000/) 在 Electron 應用程序上顯示白屏)
Electron webContents executeJavaScript : Cannot execute script on second on loadURL(Electron webContents executeJavaScript:無法在第二個 loadURL 上執行腳本)
how to use electron browser window inside components in angular-cli?(如何在angular-cli的組件內使用電子瀏覽器窗口?)
ElectronJS - sharing redux store between windows?(ElectronJS - 在 Windows 之間共享 redux 存儲?)
How to access camera/webcamera inside electron app?(如何在電子應用程序中訪問相機/網絡攝像頭?)
主站蜘蛛池模板: 久久精品中文 | 精品亚洲二区 | 国产精品久久久久久福利一牛影视 | 黄色毛片免费 | 91在线一区二区 | 在线观看视频福利 | 欧美日韩中文在线 | 午夜私人影院在线观看 | 国产91亚洲精品一区二区三区 | 中文字幕在线播放不卡 | 91短视频网址 | www.亚洲精品 | 亚洲国产精品一区二区三区 | 美女福利网站 | 久久久精彩视频 | 中文字幕一级毛片 | 日本公妇乱淫xxxⅹ 国产在线不卡 | 久久久爽爽爽美女图片 | 亚洲欧美日韩电影 | 日本久久久一区二区三区 | www.99热 | 高清av电影 | 日韩免费一区 | 中文字幕第49页 | 久久精品免费 | 亚洲一区二区在线电影 | 精品国产一区二区三区性色 | www.国产精| 久久久久久久久久久成人 | 亚洲永久入口 | 日韩有码一区 | 自拍偷拍亚洲视频 | 中文字幕亚洲视频 | 亚洲成人高清 | 99精品一区二区 | 日韩在线观看一区 | 国产欧美精品一区二区 | 亚洲男女视频在线观看 | 在线观看中文字幕av | 97碰碰碰| 国产精品99久久久精品免费观看 |