how to use electron-builder or any other pakage to make exe distributable bundle with laravel

Viewed 113

i have a laravel project that runs inside electron i have given below the folder stucture my larvel project is inside of www

enter image description here

when i do npm start my electron project stats with my larvel project inside it now i want to make distubutable exe file of this project

i was tring to use electron-builder but i didnt find any document for using larvel with electron can someone help to fix this

here i am adding main.js file code so u can have refernce

main.js

const electron = require("electron");
const path = require("path");

const BrowserWindow = electron.BrowserWindow;
const app = electron.app;

app.on("ready", () => {
  createWindow();
});

var phpServer = require("node-php-server");
const port = 8000,
  host = "127.0.0.1";
const serverUrl = `http://${host}:${port}`;

let mainWindow;

function createWindow() {

  console.log(`${__dirname}`),
    phpServer.createServer({
      port: port,
      hostname: host,
      base: `${__dirname}/www/public`,
      keepalive: false,
      open: false,
      bin: `${__dirname}/php/php.exe`,
      router: __dirname + "/www/server.php",
    });

  const { width, height } = electron.screen.getPrimaryDisplay().workAreaSize;
  mainWindow = new BrowserWindow({
    width: width,
    height: height,
    show: false,
    autoHideMenuBar: false,
  });

  mainWindow.loadURL(serverUrl);

  mainWindow.webContents.once("dom-ready", function () {
    mainWindow.show();
    mainWindow.maximize();

  });

  mainWindow.on("closed", function () {
    phpServer.close();
    mainWindow = null;
  });
}


app.on("window-all-closed", function () {
  if (process.platform !== "darwin") {
 
    phpServer.close();
    app.quit();
  }
});

app.on("activate", function () {
  if (mainWindow === null) {
    createWindow();
  }
});

also here i am adding pakage.json file code also for referce

package.json

{
  "name": "laravelprocject",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "scripts": {
    "start": "electron .",
    "php": "/usr/bin/php",
    "laravel:serve": "php artisan serve"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "electron": "^20.1.0",
    "electron-packager": "^16.0.0"
  },
  "dependencies": {
    "node-php-server": "^0.1.2"
  }
}
1 Answers

I don't think this is really the purpose of Electron. Electron by itself is already pretty big in terms of memory footprint and disk space used. If you would want to include a PHP server on top of that, it will not make this footprint smaller.

However, it is not impossible to accomplish such a thing. I think it would be helpful for you to read this discussion on Github. There you can read that you can use the gulp-connect-php package to include PHP within your electron executable, note that this is 5 years old already.

Since you already have running PHP inside Electron, the only thing that you have to do is build an executable. I used this project as a boilerplate and added electron-builder:

{
  ...
  "main": "main.js",
  "scripts": {
    "start": "electron main.js"
  },
  "devDependencies": {
    "electron": "^1.3",
    "gulp-connect-php": "^0.0.8",
    "electron-builder": "^19.32.2"
  }
  ...
}

Then install packages and run the following command, which will create an executable for you:

electron-builder build --win
Related