所有人
我创建了一个React应用程序来构建电子应用程序。我正在尝试使用electronic-builder创建安装程序。我正在Ubuntu上尝试生成.deb。
我没有使用create-react-app,而是使用了已有的样板,但我不知道这是否是问题所在。
只要编辑Webpack在build文件夹中生成的index.html,我就可以使它工作。我必须将main.css和build.js的路径从/main.css和/build.js更改为./main.css和./build.js。
我读到我必须在package.json中使用“ homepage”属性,但是它不起作用,似乎被忽略了。
我的文件夹结构如下:
package.json
src/
config/
main_electron_file.js
build/ #generated by webpack when I run the 'build:prd' command
public/ #empty
package.json
{
"name": "frontend",
"version": "1.0.0",
"description": "",
"main": "./main_electron_file.js",
"scripts": {
"dev": "./node_modules/.bin/webpack-dev-server --config ./config/webpack.config.dev.js --mode development --open --hot",
"build:prd": "./node_modules/webpack/bin/webpack.js --mode production --config ./config/webpack.config.prd.js --env.NODE_ENV=production --progress",
"electron": "electron .",
"start": "npm run dev && npm run electron",
"start:dev": "ELECTRON_START_URL=http://localhost:3000 electron .",
"dist": "build"
},
"keywords": [],
"author": {
"name": "name",
"email": "email@mail.com"
},
"license": "ISC",
"dependencies": {
...
},
"devDependencies": {
...
},
"homepage": "./",
"build": {
"appId": "com.myself.myapp",
"linux": {
"target": [
"deb"
]
}
}
}
main_electron_file.js
// Modules to control application life and create native browser window
const { app, BrowserWindow } = require('electron');
const path = require('path');
const url = require('url');
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow
function createWindow() {
const startUrl = process.env.ELECTRON_START_URL || url.format({
pathname: path.join(__dirname, 'build/index.html'),
protocol: 'file:',
slashes: true
});
// Create the browser window.
mainWindow = new BrowserWindow({ width: 800, height: 600, resizeble: false });
mainWindow.setResizable(false);
// and load the index.html of the app.
// win.loadURL('http://localhost:3000');
mainWindow.loadURL(startUrl);
// Open the DevTools.
mainWindow.webContents.openDevTools()
// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null
})
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow()
}
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
顺便说一句。我没有建立它。我正在运行它,指向dist中的index.html,就像在生产模式下那样。所以我先运行npm run build:prd
,然后运行npm run electron
有什么想法可以使这个过程自动化吗?
答案 0 :(得分:0)
请尝试在您的index.html中添加这一行
<base href="./">
并且应该相对于build文件夹中的index.html设置main.css和build.js目录
答案 1 :(得分:0)
对我有用的是:
添加到Webpack配置:
输出:{ publicPath:'./', ... }
这告诉webpack在相同目录下创建文件之间的每个链接。
按照carlokid的建议,添加到index.html: