久久久久久久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?(如何在電子應用程序中訪問相機/網絡攝像頭?)
主站蜘蛛池模板: 秋霞影院一区二区 | 精品亚洲永久免费精品 | 美女视频三区 | 国产精品欧美一区二区三区不卡 | 成人a在线观看 | 久久久国产网站 | 亚洲国产精品一区二区三区 | 国产精品日韩一区二区 | 久精品久久 | 国产黄色一级片 | 99久久中文字幕三级久久日本 | 在线国产视频观看 | 在线色网 | 免费精品在线视频 | 狠狠爱网址 | 亚洲理论在线观看电影 | 中文字幕第一页在线 | 一级黄色影片在线观看 | 国产美女精品视频免费观看 | 在线一级片 | 欧美在线观看网站 | 欧洲精品视频一区 | 国产精品美女久久久久久久网站 | 91精品国产一区二区三区 | 精品无码久久久久久国产 | 亚洲精品乱码久久久久久黑人 | 综合二区| 免费久久久久久 | 五月婷亚洲 | 在线亚洲免费 | 国内精品久久久久久 | 午夜精品久久久久久久久久久久久 | 国产精品一区一区三区 | 欧美激情综合色综合啪啪五月 | 久久精品亚洲成在人线av网址 | 久久丁香| 99久久日韩精品免费热麻豆美女 | 日韩在线一区二区 | 91亚洲精品在线 | 成人精品一区二区 | 亚洲精品乱码久久久久久按摩观 |