Skip to content

Commit

Permalink
refactor: better process.env assign
Browse files Browse the repository at this point in the history
  • Loading branch information
caoxiemeihao committed Apr 15, 2024
1 parent e636b5d commit 1037f1b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion electron/electron-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ declare namespace NodeJS {
* │
* ```
*/
DIST: string
APP_ROOT: string
/** /dist/ or /public/ */
VITE_PUBLIC: string
}
Expand Down
17 changes: 9 additions & 8 deletions electron/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { app, BrowserWindow } from 'electron'
import path from 'node:path'

// 🚧 Use ['ENV_NAME'] avoid vite:define plugin - [email protected]
const VITE_DEV_SERVER_URL = process.env['VITE_DEV_SERVER_URL']

// The built directory structure
//
// ├─┬─┬ dist
Expand All @@ -13,10 +10,14 @@ const VITE_DEV_SERVER_URL = process.env['VITE_DEV_SERVER_URL']
// │ │ ├── main.js
// │ │ └── preload.js
// │
process.env.DIST = path.join(__dirname, '../dist')
process.env.VITE_PUBLIC = VITE_DEV_SERVER_URL
? path.join(process.env.DIST, '../public')
: process.env.DIST
process.env.APP_ROOT = path.join(__dirname, '..')

// 🚧 Use ['ENV_NAME'] avoid vite:define plugin - [email protected]
export const VITE_DEV_SERVER_URL = process.env['VITE_DEV_SERVER_URL']
export const MAIN_DIST = path.join(process.env.APP_ROOT, 'dist-electron')
export const RENDERER_DIST = path.join(process.env.APP_ROOT, 'dist')

process.env.VITE_PUBLIC = VITE_DEV_SERVER_URL ? path.join(RENDERER_DIST, '../public') : RENDERER_DIST

let win: BrowserWindow | null

Expand All @@ -37,7 +38,7 @@ function createWindow() {
win.loadURL(VITE_DEV_SERVER_URL)
} else {
// win.loadFile('dist/index.html')
win.loadFile(path.join(process.env.DIST, 'index.html'))
win.loadFile(path.join(RENDERER_DIST, 'index.html'))
}
}

Expand Down

0 comments on commit 1037f1b

Please sign in to comment.