Skip to content

Commit 6d0b65c

Browse files
committed
refactor: better export process.env.XXX
1 parent 6cd6bb5 commit 6d0b65c

File tree

2 files changed

+37
-29
lines changed

2 files changed

+37
-29
lines changed

Diff for: electron/electron-env.d.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,21 @@
33
declare namespace NodeJS {
44
interface ProcessEnv {
55
VSCODE_DEBUG?: 'true'
6-
DIST_ELECTRON: string
7-
DIST: string
6+
/**
7+
* The built directory structure
8+
*
9+
* ```tree
10+
* ├─┬ dist-electron
11+
* │ ├─┬ main
12+
* │ │ └── index.js > Electron-Main
13+
* │ └─┬ preload
14+
* │ └── index.mjs > Preload-Scripts
15+
* ├─┬ dist
16+
* │ └── index.html > Electron-Renderer
17+
* ```
18+
*/
19+
APP_ROOT: string
820
/** /dist/ or /public/ */
921
VITE_PUBLIC: string
1022
}
11-
}
23+
}

Diff for: electron/main/index.ts

+22-26
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
11
import { app, BrowserWindow, shell, ipcMain } from 'electron'
2-
import { release } from 'node:os'
3-
import { dirname, join } from 'node:path'
42
import { fileURLToPath } from 'node:url'
3+
import path from 'node:path'
4+
import os from 'node:os'
55
import { update } from './update'
66

77
globalThis.__filename = fileURLToPath(import.meta.url)
8-
globalThis.__dirname = dirname(__filename)
8+
globalThis.__dirname = path.dirname(__filename)
99

1010
// The built directory structure
1111
//
1212
// ├─┬ dist-electron
1313
// │ ├─┬ main
1414
// │ │ └── index.js > Electron-Main
1515
// │ └─┬ preload
16-
// │ └── index.mjs > Preload-Scripts
16+
// │ └── index.mjs > Preload-Scripts
1717
// ├─┬ dist
1818
// │ └── index.html > Electron-Renderer
1919
//
20-
process.env.DIST_ELECTRON = join(__dirname, '../')
21-
process.env.DIST = join(process.env.DIST_ELECTRON, '../dist')
22-
process.env.VITE_PUBLIC = process.env.VITE_DEV_SERVER_URL
23-
? join(process.env.DIST_ELECTRON, '../public')
24-
: process.env.DIST
20+
process.env.APP_ROOT = path.join(__dirname, '../..')
21+
22+
export const MAIN_DIST = path.join(process.env.APP_ROOT, 'dist-electron')
23+
export const RENDERER_DIST = path.join(process.env.APP_ROOT, 'dist')
24+
export const VITE_DEV_SERVER_URL = process.env.VITE_DEV_SERVER_URL
25+
26+
process.env.VITE_PUBLIC = VITE_DEV_SERVER_URL
27+
? path.join(process.env.APP_ROOT, 'public')
28+
: RENDERER_DIST
2529

2630
// Disable GPU Acceleration for Windows 7
27-
if (release().startsWith('6.1')) app.disableHardwareAcceleration()
31+
if (os.release().startsWith('6.1')) app.disableHardwareAcceleration()
2832

2933
// Set application name for Windows 10+ notifications
3034
if (process.platform === 'win32') app.setAppUserModelId(app.getName())
@@ -34,21 +38,14 @@ if (!app.requestSingleInstanceLock()) {
3438
process.exit(0)
3539
}
3640

37-
// Remove electron security warnings
38-
// This warning only shows in development mode
39-
// Read more on https://www.electronjs.org/docs/latest/tutorial/security
40-
// process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true'
41-
4241
let win: BrowserWindow | null = null
43-
// Here, you can also use other preload
44-
const preload = join(__dirname, '../preload/index.mjs')
45-
const url = process.env.VITE_DEV_SERVER_URL
46-
const indexHtml = join(process.env.DIST, 'index.html')
42+
const preload = path.join(__dirname, '../preload/index.mjs')
43+
const indexHtml = path.join(RENDERER_DIST, 'index.html')
4744

4845
async function createWindow() {
4946
win = new BrowserWindow({
5047
title: 'Main window',
51-
icon: join(process.env.VITE_PUBLIC, 'favicon.ico'),
48+
icon: path.join(process.env.VITE_PUBLIC, 'favicon.ico'),
5249
webPreferences: {
5350
preload,
5451
// Warning: Enable nodeIntegration and disable contextIsolation is not secure in production
@@ -60,8 +57,8 @@ async function createWindow() {
6057
},
6158
})
6259

63-
if (url) { // electron-vite-vue#298
64-
win.loadURL(url)
60+
if (VITE_DEV_SERVER_URL) { // #298
61+
win.loadURL(VITE_DEV_SERVER_URL)
6562
// Open devTool if the app is not packaged
6663
win.webContents.openDevTools()
6764
} else {
@@ -79,7 +76,7 @@ async function createWindow() {
7976
return { action: 'deny' }
8077
})
8178

82-
// Apply electron-updater
79+
// Auto update
8380
update(win)
8481
}
8582

@@ -117,10 +114,9 @@ ipcMain.handle('open-win', (_, arg) => {
117114
},
118115
})
119116

120-
if (process.env.VITE_DEV_SERVER_URL) {
121-
childWindow.loadURL(`${url}#${arg}`)
117+
if (VITE_DEV_SERVER_URL) {
118+
childWindow.loadURL(`${VITE_DEV_SERVER_URL}#${arg}`)
122119
} else {
123120
childWindow.loadFile(indexHtml, { hash: arg })
124121
}
125122
})
126-

0 commit comments

Comments
 (0)