production compilation OK

This commit is contained in:
Jérémy Munsch 2024-01-19 09:31:01 +01:00
parent 995e38ed66
commit 57f534b643
No known key found for this signature in database
GPG key ID: 18361AF3BFEC0FEC
4 changed files with 30 additions and 45 deletions

View file

@ -1,12 +1,7 @@
/* eslint-disable */
import { BrowserWindow, app, type BrowserWindowConstructorOptions } from 'electron';
import type serve from "electron-serve";
import { EventEmitter } from 'events';
import path from 'path';
import url from 'url';
const isDev = process.env.NODE_ENV === "development";
const root = path.join(__dirname, "..");
const publicPath = isDev ? path.join(root, "public") : path.join(root, ".output/public");
export default class BrowserWinHandler {
@ -83,21 +78,22 @@ export default class BrowserWinHandler {
})
}
async loadMainPage() {
async loadMainPage(loadUrl: serve.loadURL) {
if (!this.browserWindow) return Promise.reject(new Error('The page could not be loaded before win \'created\' event'))
if (process.env.VITE_DEV_SERVER_URL) {
this.browserWindow.loadURL(process.env.VITE_DEV_SERVER_URL);
this.browserWindow.webContents.openDevTools();
} else {
// A TESTER https://github.com/sindresorhus/electron-serve
// this.browserWindow.loadFile(path.join(publicPath, "index.html"), {
// hash: '/'
// });
// this.browserWindow.loadURL("app://./index.html#/");
this.browserWindow.loadURL(url.format({
pathname: 'index.html', /* Attention here: origin is path.join(__dirname, 'index.html') */
protocol: 'file',
slashes: true
}));
loadUrl(this.browserWindow);
// this.browserWindow.loadURL(url.format({
// pathname: 'index.html', /* Attention here: origin is path.join(__dirname, 'index.html') */
// protocol: 'file'
// }));
}
}

View file

@ -1,13 +1,11 @@
import fs from "node:fs";
import path from "node:path";
import { app, ipcMain, protocol } from "electron";
import { app, ipcMain } from "electron";
import log from "electron-log/main";
import serve from "electron-serve";
import BrowserWinHandler from "./BrowserWinHandler";
import UnzipData from "./UnzipData";
const isDev = process.env.NODE_ENV === "development";
const root = path.join(__dirname, "..");
const publicPath = isDev ? path.join(root, "public") : path.join(root, ".output/public");
// The built directory structure
//
@ -40,28 +38,6 @@ export const appInfo = {
log.initialize();
// * SCHEME REGISTER
// const PRODUCTION_APP_PROTOCOL = "app";
// // Credits: https://stackoverflow.com/questions/77340986/what-to-use-now-that-registerfileprotocol-is-deprecated-in-electron
// function registerProtocol(scheme: string) {
// protocol.handle(scheme, () => {
// const absolutePath = path.join(publicPath, "index.html");
// return net.fetch(`file://${absolutePath}`);
// });
// }
// if (!isDev) {
// // Scheme must be registered before the app is ready
// protocol.registerSchemesAsPrivileged([
// { scheme: PRODUCTION_APP_PROTOCOL, privileges: { secure: true, standard: true } }
// ]);
// app.once("ready", () => {
// registerProtocol(PRODUCTION_APP_PROTOCOL);
// });
// }
// ** Close Part
const handleProcessExit = () => {
app.exit(0);
@ -84,13 +60,12 @@ app.once("browser-window-created", (_, browserWindow) => {
});
});
app.whenReady().then(() => {
protocol.interceptFileProtocol("file", (request, callback) => {
const url = request.url.substr(7); /* all urls start with 'file://' */
// eslint-disable-next-line n/no-callback-literal
callback({ path: path.normalize(path.join(publicPath, url)) });
});
const isDev = process.env.NODE_ENV === "development";
const root = path.join(__dirname, "..");
const publicPath = isDev ? path.join(root, "public") : path.join(root, ".output/public");
const loadURL = serve({ directory: publicPath });
app.whenReady().then(() => {
// * Run an Electron window ?
const winHandler = new BrowserWinHandler({
// width: 1280,
@ -110,7 +85,7 @@ app.whenReady().then(() => {
try {
const appData = UnzipData.loadJsonDataFile();
winHandler.loadMainPage();
winHandler.loadMainPage(loadURL);
ipcMain.handle("appInfo", () => appInfo);
ipcMain.handle("channel", () => appData);
winHandler.openDevTool();

13
package-lock.json generated
View file

@ -32,6 +32,7 @@
"electron": "^28.1.1",
"electron-builder": "^24.9.1",
"electron-log": "^5.0.3",
"electron-serve": "^1.2.0",
"eslint": "latest",
"eslint-config-prettier": "latest",
"eslint-plugin-nuxt": "latest",
@ -8413,6 +8414,18 @@
"node": ">= 10.0.0"
}
},
"node_modules/electron-serve": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/electron-serve/-/electron-serve-1.2.0.tgz",
"integrity": "sha512-zJG3wisMrDn2G/gnjrhyB074COvly1FnS0U7Edm8bfXLB8MYX7UtwR9/y2LkFreYjzQHm9nEbAfgCmF+9M9LHQ==",
"dev": true,
"engines": {
"node": ">=12"
},
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/electron-to-chromium": {
"version": "1.4.628",
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.628.tgz",

View file

@ -53,6 +53,7 @@
"electron": "^28.1.1",
"electron-builder": "^24.9.1",
"electron-log": "^5.0.3",
"electron-serve": "^1.2.0",
"eslint": "latest",
"eslint-config-prettier": "latest",
"eslint-plugin-nuxt": "latest",