refactor: better export process.env.XXX

This commit is contained in:
Leo Wang(草鞋没号) 2024-04-18 17:35:48 +08:00
parent c7e5fd70cd
commit 107ffe8d40
2 changed files with 35 additions and 26 deletions

View File

@ -3,8 +3,20 @@
declare namespace NodeJS { declare namespace NodeJS {
interface ProcessEnv { interface ProcessEnv {
VSCODE_DEBUG?: 'true' 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/ */ /** /dist/ or /public/ */
VITE_PUBLIC: string VITE_PUBLIC: string
} }

View File

@ -1,10 +1,10 @@
import { app, BrowserWindow, shell, ipcMain } from 'electron' import { app, BrowserWindow, shell, ipcMain } from 'electron'
import { release } from 'node:os'
import { join, dirname } from 'node:path'
import { fileURLToPath } from 'node:url' import { fileURLToPath } from 'node:url'
import path from 'node:path'
import os from 'node:os'
globalThis.__filename = fileURLToPath(import.meta.url) globalThis.__filename = fileURLToPath(import.meta.url)
globalThis.__dirname = dirname(__filename) globalThis.__dirname = path.dirname(__filename)
// The built directory structure // The built directory structure
// //
@ -12,18 +12,22 @@ globalThis.__dirname = dirname(__filename)
// │ ├─┬ main // │ ├─┬ main
// │ │ └── index.js > Electron-Main // │ │ └── index.js > Electron-Main
// │ └─┬ preload // │ └─┬ preload
// │ └── index.mjs > Preload-Scripts // │ └── index.mjs > Preload-Scripts
// ├─┬ dist // ├─┬ dist
// │ └── index.html > Electron-Renderer // │ └── index.html > Electron-Renderer
// //
process.env.DIST_ELECTRON = join(__dirname, '..') process.env.APP_ROOT = path.join(__dirname, '../..')
process.env.DIST = join(process.env.DIST_ELECTRON, '../dist')
process.env.VITE_PUBLIC = process.env.VITE_DEV_SERVER_URL export const MAIN_DIST = path.join(process.env.APP_ROOT, 'dist-electron')
? join(process.env.DIST_ELECTRON, '../public') export const RENDERER_DIST = path.join(process.env.APP_ROOT, 'dist')
: process.env.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 // 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 // Set application name for Windows 10+ notifications
if (process.platform === 'win32') app.setAppUserModelId(app.getName()) if (process.platform === 'win32') app.setAppUserModelId(app.getName())
@ -33,21 +37,14 @@ if (!app.requestSingleInstanceLock()) {
process.exit(0) 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 let win: BrowserWindow | null = null
// Here, you can also use other preload const preload = path.join(__dirname, '../preload/index.mjs')
const preload = join(__dirname, '../preload/index.mjs') const indexHtml = path.join(RENDERER_DIST, 'index.html')
const url = process.env.VITE_DEV_SERVER_URL
const indexHtml = join(process.env.DIST, 'index.html')
async function createWindow() { async function createWindow() {
win = new BrowserWindow({ win = new BrowserWindow({
title: 'Main window', title: 'Main window',
icon: join(process.env.VITE_PUBLIC, 'favicon.ico'), icon: path.join(process.env.VITE_PUBLIC, 'favicon.ico'),
webPreferences: { webPreferences: {
preload, preload,
// Warning: Enable nodeIntegration and disable contextIsolation is not secure in production // Warning: Enable nodeIntegration and disable contextIsolation is not secure in production
@ -59,8 +56,8 @@ async function createWindow() {
}, },
}) })
if (process.env.VITE_DEV_SERVER_URL) { // electron-vite-vue#298 if (VITE_DEV_SERVER_URL) { // #298
win.loadURL(url) win.loadURL(VITE_DEV_SERVER_URL)
// Open devTool if the app is not packaged // Open devTool if the app is not packaged
win.webContents.openDevTools() win.webContents.openDevTools()
} else { } else {
@ -114,8 +111,8 @@ ipcMain.handle('open-win', (_, arg) => {
}, },
}) })
if (process.env.VITE_DEV_SERVER_URL) { if (VITE_DEV_SERVER_URL) {
childWindow.loadURL(`${url}#${arg}`) childWindow.loadURL(`${VITE_DEV_SERVER_URL}#${arg}`)
} else { } else {
childWindow.loadFile(indexHtml, { hash: arg }) childWindow.loadFile(indexHtml, { hash: arg })
} }