From 6d0b65c5c4c36fb94ef66f39455bfb442032a4a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leo=20Wang=28=E8=8D=89=E9=9E=8B=E6=B2=A1=E5=8F=B7=29?= <308487730@qq.com> Date: Thu, 18 Apr 2024 21:02:54 +0800 Subject: [PATCH] refactor: better export `process.env.XXX` --- electron/electron-env.d.ts | 18 +++++++++++--- electron/main/index.ts | 48 +++++++++++++++++--------------------- 2 files changed, 37 insertions(+), 29 deletions(-) diff --git a/electron/electron-env.d.ts b/electron/electron-env.d.ts index 683a8658..aa2de819 100644 --- a/electron/electron-env.d.ts +++ b/electron/electron-env.d.ts @@ -3,9 +3,21 @@ declare namespace NodeJS { interface ProcessEnv { VSCODE_DEBUG?: 'true' - DIST_ELECTRON: string - DIST: string + /** + * The built directory structure + * + * ```tree + * ├─┬ dist-electron + * │ ├─┬ main + * │ │ └── index.js > Electron-Main + * │ └─┬ preload + * │ └── index.mjs > Preload-Scripts + * ├─┬ dist + * │ └── index.html > Electron-Renderer + * ``` + */ + APP_ROOT: string /** /dist/ or /public/ */ VITE_PUBLIC: string } -} \ No newline at end of file +} diff --git a/electron/main/index.ts b/electron/main/index.ts index 1a126c1e..df835e1b 100644 --- a/electron/main/index.ts +++ b/electron/main/index.ts @@ -1,11 +1,11 @@ import { app, BrowserWindow, shell, ipcMain } from 'electron' -import { release } from 'node:os' -import { dirname, join } from 'node:path' import { fileURLToPath } from 'node:url' +import path from 'node:path' +import os from 'node:os' import { update } from './update' globalThis.__filename = fileURLToPath(import.meta.url) -globalThis.__dirname = dirname(__filename) +globalThis.__dirname = path.dirname(__filename) // The built directory structure // @@ -13,18 +13,22 @@ globalThis.__dirname = dirname(__filename) // │ ├─┬ main // │ │ └── index.js > Electron-Main // │ └─┬ preload -// │ └── index.mjs > Preload-Scripts +// │ └── index.mjs > Preload-Scripts // ├─┬ dist // │ └── index.html > Electron-Renderer // -process.env.DIST_ELECTRON = join(__dirname, '../') -process.env.DIST = join(process.env.DIST_ELECTRON, '../dist') -process.env.VITE_PUBLIC = process.env.VITE_DEV_SERVER_URL - ? join(process.env.DIST_ELECTRON, '../public') - : process.env.DIST +process.env.APP_ROOT = path.join(__dirname, '../..') + +export const MAIN_DIST = path.join(process.env.APP_ROOT, 'dist-electron') +export const RENDERER_DIST = path.join(process.env.APP_ROOT, 'dist') +export const VITE_DEV_SERVER_URL = process.env.VITE_DEV_SERVER_URL + +process.env.VITE_PUBLIC = VITE_DEV_SERVER_URL + ? path.join(process.env.APP_ROOT, 'public') + : RENDERER_DIST // Disable GPU Acceleration for Windows 7 -if (release().startsWith('6.1')) app.disableHardwareAcceleration() +if (os.release().startsWith('6.1')) app.disableHardwareAcceleration() // Set application name for Windows 10+ notifications if (process.platform === 'win32') app.setAppUserModelId(app.getName()) @@ -34,21 +38,14 @@ if (!app.requestSingleInstanceLock()) { process.exit(0) } -// Remove electron security warnings -// This warning only shows in development mode -// Read more on https://www.electronjs.org/docs/latest/tutorial/security -// process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true' - let win: BrowserWindow | null = null -// Here, you can also use other preload -const preload = join(__dirname, '../preload/index.mjs') -const url = process.env.VITE_DEV_SERVER_URL -const indexHtml = join(process.env.DIST, 'index.html') +const preload = path.join(__dirname, '../preload/index.mjs') +const indexHtml = path.join(RENDERER_DIST, 'index.html') async function createWindow() { win = new BrowserWindow({ title: 'Main window', - icon: join(process.env.VITE_PUBLIC, 'favicon.ico'), + icon: path.join(process.env.VITE_PUBLIC, 'favicon.ico'), webPreferences: { preload, // Warning: Enable nodeIntegration and disable contextIsolation is not secure in production @@ -60,8 +57,8 @@ async function createWindow() { }, }) - if (url) { // electron-vite-vue#298 - win.loadURL(url) + if (VITE_DEV_SERVER_URL) { // #298 + win.loadURL(VITE_DEV_SERVER_URL) // Open devTool if the app is not packaged win.webContents.openDevTools() } else { @@ -79,7 +76,7 @@ async function createWindow() { return { action: 'deny' } }) - // Apply electron-updater + // Auto update update(win) } @@ -117,10 +114,9 @@ ipcMain.handle('open-win', (_, arg) => { }, }) - if (process.env.VITE_DEV_SERVER_URL) { - childWindow.loadURL(`${url}#${arg}`) + if (VITE_DEV_SERVER_URL) { + childWindow.loadURL(`${VITE_DEV_SERVER_URL}#${arg}`) } else { childWindow.loadFile(indexHtml, { hash: arg }) } }) -