refactor: better export process.env.XXX
This commit is contained in:
1 parent
6cd6bb5353
commit
6d0b65c5c4
2 files changed
+37
-29
No files matched your search
Vendored
+15
-3
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
+22
-26
@@ -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 })
|
||||
}
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user