электронный строитель не работает на производство из-за пути - PullRequest
0 голосов
/ 22 ноября 2018

каждый

Я создал приложение React для создания электронного приложения.Я пытаюсь использовать электронный строитель для создания установщиков.Я работаю над Ubuntu, пытаясь сгенерировать .deb.

Я не использовал create-Reaction-app, я использовал шаблон, который у меня уже был, но я не знаю, в этом ли проблема.

Я могу заставить его работать, пока я редактирую index.html, который веб-пакет создает в папке сборки.Мне нужно изменить путь к main.css и build.js с /main.css и /build.js на ./main.css и ./build.js.

Я прочитал, что должен использоватьсвойство "homepage" в package.json, но оно не работает, кажется, игнорируется.

Моя структура папок выглядит так:

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.

Кстати.Я не строю это.Я запускаю его, указывая на index.html в dist, как я делал бы в производственном режиме.Итак, я бегу npm run build:prd, а затем npm run electron

Любые идеи, как я могу сделать этот процесс автоматически?

Ответы [ 2 ]

0 голосов
/ 16 июня 2019

Что сработало для меня:

  1. Добавить в конфигурацию веб-пакета:

    Вывод: {publicPath: './', ...}

Это говорит веб-пакету, чтобы каждая ссылка между файлами, которые он создает, основывалась на одном и том же каталоге.

В соответствии с предложением carlokid добавьте в index.html:

0 голосов
/ 22 ноября 2018

Пожалуйста, попробуйте добавить эту строку в ваш index.html

<base href="./">

И ваш каталог main.css и build.js должен быть установлен относительно вашего index.html в папке сборки

...