diff --git a/README.md b/README.md index 30f0521..d3d929c 100644 --- a/README.md +++ b/README.md @@ -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) }) ``` diff --git a/quick-start/electron-env.d.ts b/quick-start/electron-env.d.ts index f3b4f4a..3389c1b 100644 --- a/quick-start/electron-env.d.ts +++ b/quick-start/electron-env.d.ts @@ -1,26 +1,5 @@ /// -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 diff --git a/quick-start/electron/main.ts b/quick-start/electron/main.ts index 7d73e86..9db1007 100644 --- a/quick-start/electron/main.ts +++ b/quick-start/electron/main.ts @@ -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') } } diff --git a/src/index.ts b/src/index.ts index 8c640ce..20d9403 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,3 @@ -import { type AddressInfo } from 'net' import { defineNuxtModule } from '@nuxt/kit' import type { ResolvedConfig, @@ -101,15 +100,9 @@ export default defineNuxtModule({ 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 ??= {}