Skip to content

Commit

Permalink
refactor: better export process.env.XXX
Browse files Browse the repository at this point in the history
  • Loading branch information
caoxiemeihao committed Apr 18, 2024
1 parent 6cd6bb5 commit 6d0b65c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 29 deletions.
18 changes: 15 additions & 3 deletions electron/electron-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
48 changes: 22 additions & 26 deletions electron/main/index.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
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
//
// ├─┬ dist-electron
// │ ├─┬ 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())
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -79,7 +76,7 @@ async function createWindow() {
return { action: 'deny' }
})

// Apply electron-updater
// Auto update
update(win)
}

Expand Down Expand Up @@ -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 })
}
})

0 comments on commit 6d0b65c

Please sign in to comment.