5

everyone

I created a React app in order to build an electron app. I'm trying to use electron-builder to create the installers. I'm working on Ubuntu trying to generate a .deb.

I didn't use create-react-app, I used a boilerplate I already had, but I don't know if this is the problem.

I can make it work as long as I edit the index.html the webpack generates in the build folder. I have to change the path to main.css and build.js from /main.css and /build.js to ./main.css and ./build.js.

I read that I have to use the "homepage" property in package.json, but it's not working, seems to be ignored.

My folder structure is like:

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.

By the way. I'm not building it. I'm running it pointing to the index.html in the dist, as I would do in production mode. So i'm running npm run build:prd and then npm run electron

ANy ideas how I could make this process automatic?

Victor Ferreira
  • 6,151
  • 13
  • 64
  • 120

2 Answers2

4

Please try adding this line in your index.html

<base href="./">

And your main.css and build.js directory should be set relative to your index.html in build folder

carlokid
  • 207
  • 1
  • 4
1

What worked for me was:

  1. Add to webpack config:

    output: { publicPath: './', ... }

This tells webpack to have every link between files it creates based at the same directory.

  1. As suggested by carlokid, add to index.html:

    < base href="./" />