vite-plugin-electron
Advanced tools
Comparing version 0.28.8 to 0.29.0
/// <reference types="node" /> | ||
import { type Plugin } from 'vite'; | ||
import { resolveViteConfig, withExternalBuiltins, treeKillSync } from './utils'; | ||
export { resolveViteConfig, withExternalBuiltins, treeKillSync, }; | ||
import { resolveViteConfig, withExternalBuiltins } from './utils'; | ||
export { resolveViteConfig, withExternalBuiltins, }; | ||
export interface ElectronOptions { | ||
@@ -41,4 +41,5 @@ /** | ||
export declare namespace startup { | ||
var send: (message: string) => void; | ||
var hookedProcessExit: boolean; | ||
var exit: () => Promise<void>; | ||
} |
@@ -126,2 +126,46 @@ "use strict"; | ||
} | ||
function resolveInput(config) { | ||
var _a, _b; | ||
const options = config.build; | ||
const { root } = config; | ||
const libOptions = options.lib; | ||
const resolve = (p) => path.resolve(root, p); | ||
const input = libOptions ? ((_a = options.rollupOptions) == null ? void 0 : _a.input) || (typeof libOptions.entry === "string" ? resolve(libOptions.entry) : Array.isArray(libOptions.entry) ? libOptions.entry.map(resolve) : Object.fromEntries( | ||
Object.entries(libOptions.entry).map(([alias, file]) => [ | ||
alias, | ||
resolve(file) | ||
]) | ||
)) : (_b = options.rollupOptions) == null ? void 0 : _b.input; | ||
if (input) | ||
return input; | ||
const indexHtml = resolve("index.html"); | ||
return fs.existsSync(indexHtml) ? indexHtml : void 0; | ||
} | ||
async function mockIndexHtml(config) { | ||
const { root, build: build2 } = config; | ||
const output = path.resolve(root, build2.outDir); | ||
const content = ` | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<title>vite-plugin-electron</title> | ||
</head> | ||
<body> | ||
<div>An entry file for electron renderer process.</div> | ||
</body> | ||
</html> | ||
`.trim(); | ||
const index = "index.html"; | ||
const filepath = path.join(root, index); | ||
const distpath = path.join(output, index); | ||
await fs.promises.writeFile(filepath, content); | ||
return { | ||
async remove() { | ||
await fs.promises.unlink(filepath); | ||
await fs.promises.unlink(distpath); | ||
}, | ||
filepath, | ||
distpath | ||
}; | ||
} | ||
function treeKillSync(pid) { | ||
@@ -164,2 +208,3 @@ if (process.platform === "win32") { | ||
let configEnv; | ||
let mockdInput; | ||
return [ | ||
@@ -200,3 +245,3 @@ { | ||
// Why not use Vite's built-in `/@vite/client` to implement Hot reload? | ||
// Because Vite only inserts `/@vite/client` into the `*.html` entry file. | ||
// Because Vite only inserts `/@vite/client` into the `*.html` entry file, the preload scripts are usually a `*.js` file. | ||
// @see - https://github.com/vitejs/vite/blob/v5.2.11/packages/vite/src/node/server/middlewares/indexHtml.ts#L399 | ||
@@ -206,2 +251,3 @@ reload() { | ||
(server.hot || server.ws).send({ type: "full-reload" }); | ||
startup.send("electron-vite&type=hot-reload"); | ||
} else { | ||
@@ -231,4 +277,11 @@ startup(); | ||
}, | ||
async configResolved(config) { | ||
const input = resolveInput(config); | ||
if (input == null) { | ||
mockdInput = await mockIndexHtml(config); | ||
} | ||
}, | ||
async closeBundle() { | ||
var _a, _b, _c, _d; | ||
mockdInput == null ? void 0 : mockdInput.remove(); | ||
for (const options2 of optionsArray) { | ||
@@ -251,3 +304,6 @@ options2.vite ?? (options2.vite = {}); | ||
await startup.exit(); | ||
process.electronApp = spawn(electronPath, argv, { stdio: "inherit", ...options }); | ||
process.electronApp = spawn(electronPath, argv, { | ||
stdio: ["inherit", "inherit", "inherit", "ipc"], | ||
...options | ||
}); | ||
process.electronApp.once("exit", process.exit); | ||
@@ -259,7 +315,16 @@ if (!startup.hookedProcessExit) { | ||
} | ||
startup.send = (message) => { | ||
var _a, _b; | ||
if (process.electronApp) { | ||
(_b = (_a = process.electronApp).send) == null ? void 0 : _b.call(_a, message); | ||
} | ||
}; | ||
startup.hookedProcessExit = false; | ||
startup.exit = async () => { | ||
if (process.electronApp) { | ||
process.electronApp.removeAllListeners(); | ||
treeKillSync(process.electronApp.pid); | ||
await new Promise((resolve) => { | ||
process.electronApp.removeAllListeners(); | ||
process.electronApp.once("exit", resolve); | ||
treeKillSync(process.electronApp.pid); | ||
}); | ||
} | ||
@@ -271,3 +336,2 @@ }; | ||
exports.startup = startup; | ||
exports.treeKillSync = treeKillSync; | ||
exports.withExternalBuiltins = withExternalBuiltins; |
@@ -1,2 +0,2 @@ | ||
import { type InlineConfig, type ViteDevServer } from 'vite'; | ||
import { type InlineConfig, type ResolvedConfig, type ViteDevServer } from 'vite'; | ||
import type { ElectronOptions } from '.'; | ||
@@ -20,3 +20,15 @@ export interface PidTree { | ||
} | null; | ||
/** @see https://github.com/vitejs/vite/blob/v5.4.9/packages/vite/src/node/build.ts#L489-L504 */ | ||
export declare function resolveInput(config: ResolvedConfig): import("rollup").InputOption | undefined; | ||
/** | ||
* When run the `vite build` command, there must be an entry file. | ||
* If the user does not need Renderer, we need to create a temporary entry file to avoid Vite throw error. | ||
* @inspired https://github.com/vitejs/vite/blob/v5.4.9/packages/vite/src/node/config.ts#L1234-L1236 | ||
*/ | ||
export declare function mockIndexHtml(config: ResolvedConfig): Promise<{ | ||
remove(): Promise<void>; | ||
filepath: string; | ||
distpath: string; | ||
}>; | ||
/** | ||
* Inspired `tree-kill`, implemented based on sync-api. #168 | ||
@@ -23,0 +35,0 @@ * @see https://github.com/pkrumins/node-tree-kill/blob/v1.2.2/index.js |
{ | ||
"name": "vite-plugin-electron", | ||
"version": "0.28.8", | ||
"version": "0.29.0", | ||
"description": "Electron 🔗 Vite", | ||
@@ -29,3 +29,3 @@ "main": "./dist/index.js", | ||
}, | ||
"author": "草鞋没号 <308487730@qq.com>", | ||
"author": "Leo Wang(草鞋没号) <308487730@qq.com>", | ||
"license": "MIT", | ||
@@ -32,0 +32,0 @@ "packageManager": "pnpm@8.0.0", |
@@ -249,2 +249,20 @@ <p align="center"> | ||
**Hot Reload** | ||
Since `v0.29.0`, when preload scripts are rebuilt, they will send an `electron-vite&type=hot-reload` event to the main process. | ||
If your App doesn't need a renderer process, this will give you **hot-reload**. | ||
```js | ||
// electron/main.ts | ||
process.on('message', (msg) => { | ||
if (msg === 'electron-vite&type=hot-reload') { | ||
for (const win of BrowserWindow.getAllWindows()) { | ||
// Hot reload preload scripts | ||
win.webContents.reload() | ||
} | ||
} | ||
}) | ||
``` | ||
## How to work | ||
@@ -251,0 +269,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
59969
1085
381