【发布时间】:2022-01-07 12:28:39
【问题描述】:
我有一个在开发中构建和运行的电子应用程序,但是使用 electron-builder 打包应用程序时,预加载脚本没有打包在正确的位置。
这是一个有据可查的问题,例如 here 和 here 有非常相似的问题,但在我的情况下,没有任何答复或解决方案有效。
来自我的 electron.js 文件:
function createWindow() {
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(app.getAppPath(), 'src/preload.js'),
contextIsolation: true,
},
});
// In production, set the initial browser path to the local bundle generated
// by the Create React App build process.
// In development, set it to localhost to allow live/hot-reloading.
const appURL = app.isPackaged
? url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true,
})
: 'http://localhost:3000';
mainWindow.loadURL(appURL);
mainWindow.webContents.openDevTools();
}
我的预加载脚本:
const { contextBridge, shell } = require('electron')
contextBridge.exposeInMainWorld(
'electron',
{
openBrowserWindow: (url) => shell.openExternal(url)
}
)
还有我的 Electron 应用 package.json:
"build": {
"extends": null,
"appId": "com.app",
"productName": "App",
"directories": {
"output": "dist"
},
"mac": {
"target": {
"target": "pkg",
"arch": [
"universal"
]
},
"darkModeSupport": "true",
"extendInfo": "app"
},
"pkg": {
"installLocation": "/Applications",
"overwriteAction": "upgrade"
},
"files": [
"**",
"../app/src/*",
"src/preload.js"
],
"extraResources": [
"../app/src/*",
"src/preload.js"
],
"extraFiles": [
"../app/src/*",
"src/preload.js"
]
}
上面我已经尝试确保以不同方式复制“src/preload.js”文件,但我仍然收到错误:
无法加载预加载脚本:...app/Contents/Resources/app.asar/src/preload.js
错误:找不到模块'...app/Contents/Resources/app.asar/src/preload.js'
预加载脚本实际上是复制过来的,但它不是 app.asar 文件的一部分。它被复制到包含 app.asar 文件的 Resources 文件夹之外的 src 文件夹中:
如何正确配置 electron-builder 以使该文件位于正确的位置并且可以在包运行时访问?