Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 由于路径问题,electron builder无法用于生产_Reactjs_Webpack_Electron_Electron Builder - Fatal编程技术网

Reactjs 由于路径问题,electron builder无法用于生产

Reactjs 由于路径问题,electron builder无法用于生产,reactjs,webpack,electron,electron-builder,Reactjs,Webpack,Electron,Electron Builder,每个人 我创建了一个React应用程序来构建一个electron应用程序。我正在尝试使用electron builder创建安装程序。我正在Ubuntu上工作,试图生成一个.deb 我没有使用CreateReact应用程序,我使用的是我已有的样板文件,但我不知道这是否是问题所在 只要我在build文件夹中编辑网页包生成的index.html,我就可以让它工作。我必须将main.css和build.js的路径从/main.css和/build.js更改为./main.css和./build.js

每个人

我创建了一个React应用程序来构建一个electron应用程序。我正在尝试使用electron builder创建安装程序。我正在Ubuntu上工作,试图生成一个.deb

我没有使用CreateReact应用程序,我使用的是我已有的样板文件,但我不知道这是否是问题所在

只要我在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\u electron\u 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


有没有办法让这个过程自动化?

请尝试在index.html中添加这一行

<base href="./">


您的main.css和build.js目录应该相对于build文件夹中的index.html设置

对我有用的是:

  • 添加到网页包配置:

    输出:{ 公共路径:'./', ... }

  • 这告诉webpack,它创建的文件之间的每个链接都位于同一目录下

  • 根据carlokid的建议,添加到index.html: