所有人
我创建了一个React应用程序来构建一个电子应用程序。我正在尝试使用电子构建器来创建安装程序。我正在开发Ubuntu,试图生成一个.deb。
我没有使用创建-反应-应用程序,我使用的样板,我已经有,但我不知道这是不是问题所在。
只要我编辑webpack在构建文件夹中生成的index.html,我就可以让它工作。我必须将main.css和build.js的路径从/main.css和/build.js更改为./main.css和./build.js。
我读到我必须使用package.json中的“主页”属性,但它不起作用,似乎被忽略了。
我的文件夹结构如下:
package.json
src/
config/
main_electron_file.js
build/ #generated by webpack when I run the 'build:prd' command
public/ #emptypackage.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
ANy的想法是如何使这个过程自动化?
发布于 2018-11-22 15:32:47
请尝试在您的index.html中添加这一行
<base href="./">并且您的main.css和build.js目录应该相对于构建文件夹中的index.html设置。
发布于 2019-06-16 12:07:47
对我有用的是:
这告诉webpack在同一个目录下创建的文件之间要有每个链接。
https://stackoverflow.com/questions/53422717
复制相似问题