Skip to content

Commit

Permalink
fix: load fixed url in dev
Browse files Browse the repository at this point in the history
  • Loading branch information
kingyue737 committed Aug 22, 2024
1 parent d7cf10d commit 586891e
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 52 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default defineNuxtConfig({
import { app, BrowserWindow } from 'electron'

app.whenReady().then(() => {
new BrowserWindow().loadURL(process.env.VITE_DEV_SERVER_URL)
new BrowserWindow().loadURL(JSON.parse(process.env.__NUXT_DEV__!).proxy.url)
})
```

Expand Down
23 changes: 1 addition & 22 deletions quick-start/electron-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,6 @@
/// <reference types="vite-plugin-electron/electron-env" />

declare namespace NodeJS {
interface ProcessEnv {
/**
* The built directory structure
*
* ```tree
* ├─┬─┬ dist
* │ │ └── index.html
* │ │
* │ ├─┬ dist-electron
* │ │ ├── main.js
* │ │ └── preload.js
* │
* ```
*/
APP_ROOT: string
/** /dist/ or /public/ */
VITE_PUBLIC: string
}
}

// Used in Renderer process, expose in `preload.ts`
interface Window {
ipcRenderer: import('electron').IpcRenderer
ipcRenderer: import("electron").IpcRenderer;
}
25 changes: 4 additions & 21 deletions quick-start/electron/main.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,21 @@
import { app, BrowserWindow, ipcMain } from 'electron'
import path from 'node:path'

// The built directory structure
//
// ├─┬ dist-electron
// │ ├─┬ main
// │ │ └── index.js
// │ ├─┬ preload
// │ │ └── index.js
// │ ├─┬ renderer
// │ │ └── index.html
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, '.output/public')

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

let win: BrowserWindow | null

function createWindow() {
win = new BrowserWindow({
webPreferences: {
preload: path.join(MAIN_DIST, 'preload.js'),
preload: path.join(__dirname, 'preload.js'),
},
})

if (process.env.VITE_DEV_SERVER_URL) {
win.loadURL(process.env.VITE_DEV_SERVER_URL)
if (process.env.NODE_ENV === 'development') {
win.loadURL(JSON.parse(process.env.__NUXT_DEV__!).proxy.url)
win.webContents.openDevTools()
} else {
win.loadFile(path.join(process.env.VITE_PUBLIC!, 'index.html'))
win.loadFile('.output/public/200.html')
}
}

Expand Down
9 changes: 1 addition & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { type AddressInfo } from 'net'
import { defineNuxtModule } from '@nuxt/kit'
import type {
ResolvedConfig,
Expand Down Expand Up @@ -101,15 +100,9 @@ export default defineNuxtModule<ElectronOptions>({
viteServerResolve(server)
},
// For development
listen(server, listener) {
listen() {
// For `viteConfig.promise` can able resolved
(async function _listen() {
const addressInfo = server.address() as AddressInfo
Object.assign(process.env, {
// This is required, and it is used in Electron-Main.
VITE_DEV_SERVER_URL: `http://localhost:${addressInfo.port}`,
})

// https://github.com/electron-vite/vite-plugin-electron/blob/v0.11.2/src/index.ts#L37-L59
for (const config of options.build) {
config.vite ??= {}
Expand Down

0 comments on commit 586891e

Please sign in to comment.