Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: load fixed url in dev #87

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
21 changes: 0 additions & 21 deletions quick-start/electron-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,5 @@
/// <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
Expand Down
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