vite-plugin-electron
Advanced tools
Comparing version 0.28.0 to 0.28.1
@@ -0,1 +1,2 @@ | ||
/// <reference types="node" /> | ||
import { type Plugin } from 'vite'; | ||
@@ -22,3 +23,3 @@ import { resolveViteConfig, withExternalBuiltins } from './utils'; | ||
*/ | ||
startup: (argv?: string[]) => Promise<void>; | ||
startup: (argv?: string[], options?: import('node:child_process').SpawnOptions) => Promise<void>; | ||
/** Reload Electron-Renderer */ | ||
@@ -35,3 +36,3 @@ reload: () => void; | ||
*/ | ||
export declare function startup(argv?: string[]): Promise<void>; | ||
export declare function startup(argv?: string[], options?: import('node:child_process').SpawnOptions): Promise<void>; | ||
export declare namespace startup { | ||
@@ -38,0 +39,0 @@ var hookedProcessExit: boolean; |
@@ -28,2 +28,3 @@ "use strict"; | ||
const path = require("node:path"); | ||
const cp = require("node:child_process"); | ||
const node_module = require("node:module"); | ||
@@ -54,2 +55,6 @@ function resolveViteConfig(options) { | ||
mainFields: ["module", "jsnext:main", "jsnext"] | ||
}, | ||
define: { | ||
// @see - https://github.com/vitejs/vite/blob/v5.0.11/packages/vite/src/node/plugins/define.ts#L20 | ||
"process.env": "process.env" | ||
} | ||
@@ -110,14 +115,2 @@ }; | ||
} | ||
function calcEntryCount(optionsArray) { | ||
return optionsArray.reduce((count, options) => { | ||
var _a, _b, _c; | ||
const input = (_c = (_b = (_a = options.vite) == null ? void 0 : _a.build) == null ? void 0 : _b.rollupOptions) == null ? void 0 : _c.input; | ||
if (input) { | ||
count += typeof input === "string" ? 1 : Object.keys(input).length; | ||
} else if (options.entry) { | ||
count += typeof options.entry === "string" ? 1 : Object.keys(options.entry).length; | ||
} | ||
return count; | ||
}, 0); | ||
} | ||
function resolvePackageJson(root = process.cwd()) { | ||
@@ -132,2 +125,29 @@ const packageJsonPath = path.join(root, "package.json"); | ||
} | ||
function treeKillSync(pid) { | ||
if (process.platform === "win32") { | ||
cp.execSync(`taskkill /pid ${pid} /T /F`); | ||
} else { | ||
killTree(pidTree()); | ||
} | ||
} | ||
function pidTree(tree = { pid: process.pid, ppid: process.ppid }) { | ||
var _a; | ||
const command = process.platform === "darwin" ? `pgrep -P ${tree.pid}` : `ps -o pid --no-headers --ppid ${tree.ppid}`; | ||
try { | ||
const childs = (_a = cp.execSync(command, { encoding: "utf8" }).match(/\d+/g)) == null ? void 0 : _a.map((id) => +id); | ||
if (childs) { | ||
tree.children = childs.map((cid) => pidTree({ pid: cid, ppid: tree.pid })); | ||
} | ||
} catch { | ||
} | ||
return tree; | ||
} | ||
function killTree(tree) { | ||
if (tree.children) { | ||
for (const child of tree.children) { | ||
killTree(child); | ||
} | ||
} | ||
process.kill(tree.pid); | ||
} | ||
function build(options) { | ||
@@ -150,3 +170,3 @@ return vite.build(withExternalBuiltins(resolveViteConfig(options))); | ||
}); | ||
const entryCount = calcEntryCount(optionsArray); | ||
const entryCount = optionsArray.length; | ||
let closeBundleCount = 0; | ||
@@ -206,3 +226,3 @@ for (const options2 of optionsArray) { | ||
} | ||
async function startup(argv = [".", "--no-sandbox"]) { | ||
async function startup(argv = [".", "--no-sandbox"], options) { | ||
const { spawn } = await import("node:child_process"); | ||
@@ -212,3 +232,3 @@ const electron2 = await import("electron"); | ||
await startup.exit(); | ||
process.electronApp = spawn(electronPath, argv, { stdio: "inherit" }); | ||
process.electronApp = spawn(electronPath, argv, { stdio: "inherit", ...options }); | ||
process.electronApp.once("exit", process.exit); | ||
@@ -219,3 +239,3 @@ if (!startup.hookedProcessExit) { | ||
startup.exit(); | ||
process.electronApp.kill(); | ||
treeKillSync(process.electronApp.pid); | ||
}); | ||
@@ -222,0 +242,0 @@ } |
@@ -29,2 +29,3 @@ "use strict"; | ||
const index = require("./index.js"); | ||
require("node:child_process"); | ||
require("node:module"); | ||
@@ -50,21 +51,39 @@ async function electronSimple(options) { | ||
// `rollupOptions.input` has higher priority than `build.lib`. | ||
// https://github.com/vitejs/vite/blob/v5.0.9/packages/vite/src/node/build.ts#L482 | ||
// @see - https://github.com/vitejs/vite/blob/v5.0.9/packages/vite/src/node/build.ts#L482 | ||
input, | ||
// For use the Electron API - `import { contextBridge, ipcRenderer } from 'electron'`, | ||
output: { | ||
// In most cases, use `cjs` format. | ||
// @see - https://github.com/electron/electron/blob/v30.0.0-nightly.20240104/docs/tutorial/esm.md#preload-scripts | ||
// @see - https://github.com/electron-vite/vite-plugin-electron-preload/blob/v0.2.0/src/index.ts#L24-L47 | ||
// In most cases, use `cjs` format | ||
format: "cjs", | ||
// Whether Node.js is enabled in the Main process or not, the Preload scripts supports loading `electron` module, | ||
// so we need to build it in `cjs` format. | ||
// e.g. | ||
// import { ipcRenderer } from 'electron' | ||
// // โโโโ Build with `cjs` format โโโโ | ||
// const { ipcRenderer } = require('electron') | ||
// `require()` can usable matrix | ||
// @see - https://github.com/electron/electron/blob/v30.0.0-nightly.20240104/docs/tutorial/esm.md#preload-scripts | ||
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโณโโโโโโโโโโโณโโโโโโโโโโโโ | ||
// โ webPreferences: { } โ import โ require โ | ||
// โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโจ | ||
// โ nodeIntegration: false(undefined) โ โ โ โ โ | ||
// โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโจ | ||
// โ nodeIntegration: true โ โ โ โ โ | ||
// โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโจ | ||
// โ sandbox: true(undefined) โ โ โ โ โ | ||
// โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโจ | ||
// โ sandbox: false โ โ โ โ โ | ||
// โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโจ | ||
// โ nodeIntegration: false โ โ โ โ โ | ||
// โ sandbox: true โ โ โ | ||
// โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโจ | ||
// โ nodeIntegration: false โ โ โ โ โ | ||
// โ sandbox: false โ โ โ | ||
// โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโจ | ||
// โ nodeIntegration: true โ โ โ โ โ | ||
// โ sandbox: true โ โ โ | ||
// โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโจ | ||
// โ nodeIntegration: true โ โ โ โ โ | ||
// โ sandbox: false โ โ โ | ||
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโธโโโโโโโโโโโธโโโโโโโโโโโโ | ||
// - import(โ): SyntaxError: Cannot use import statement outside a module | ||
// - require(โ): ReferenceError: require is not defined in ES module scope, you can use import instead | ||
// Note, however, that `preload.ts` should not be split. ๐ง | ||
inlineDynamicImports: true, | ||
// When Rollup builds code in `cjs` format, it will automatically split the code into multiple chunks, and use `require()` to load them, | ||
// and use `require()` to load other modules when `nodeIntegration: false` in the Main process Errors will occur. | ||
// So we need to configure Rollup not to split the code when building to ensure that it works correctly with `nodeIntegration: false`. | ||
inlineDynamicImports: true, | ||
// @see - https://github.com/vitejs/vite/blob/v5.0.9/packages/vite/src/node/build.ts#L608 | ||
@@ -71,0 +90,0 @@ entryFileNames: `[name].${esmodule ? "mjs" : "js"}`, |
import { type InlineConfig, type ViteDevServer } from 'vite'; | ||
import type { ElectronOptions } from '.'; | ||
export interface PidTree { | ||
pid: number; | ||
ppid: number; | ||
children?: PidTree[]; | ||
} | ||
/** Resolve the default Vite's `InlineConfig` for build Electron-Main */ | ||
@@ -11,3 +16,2 @@ export declare function resolveViteConfig(options: ElectronOptions): InlineConfig; | ||
export declare function resolveServerUrl(server: ViteDevServer): string | void; | ||
export declare function calcEntryCount(optionsArray: ElectronOptions[]): number; | ||
export declare function resolvePackageJson(root?: string): { | ||
@@ -17,1 +21,6 @@ type?: 'module' | 'commonjs'; | ||
} | null; | ||
/** | ||
* Inspired `tree-kill`, implemented based on sync-api. #168 | ||
* @see https://github.com/pkrumins/node-tree-kill/blob/v1.2.2/index.js | ||
*/ | ||
export declare function treeKillSync(pid: number): void; |
{ | ||
"name": "vite-plugin-electron", | ||
"version": "0.28.0", | ||
"version": "0.28.1", | ||
"description": "Electron ๐ Vite", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -140,13 +140,13 @@ <p align="center"> | ||
* Triggered when Vite is built every time -- `vite serve` command only. | ||
* | ||
* If this `onstart` is passed, Electron App will not start automatically. | ||
* However, you can start Electroo App via `startup` function. | ||
* | ||
* If this `onstart` is passed, Electron App will not start automatically. | ||
* However, you can start Electroo App via `startup` function. | ||
*/ | ||
onstart?: (args: { | ||
/** | ||
* Electron App startup function. | ||
* It will mount the Electron App child-process to `process.electronApp`. | ||
* Electron App startup function. | ||
* It will mount the Electron App child-process to `process.electronApp`. | ||
* @param argv default value `['.', '--no-sandbox']` | ||
*/ | ||
startup: (argv?: string[]) => Promise<void> | ||
startup: (argv?: string[], options?: import('node:child_process').SpawnOptions) => Promise<void> | ||
/** Reload Electron-Renderer */ | ||
@@ -177,2 +177,30 @@ reload: () => void | ||
## Built format | ||
This is just the default behavior, and you can modify them at any time through custom config in the `vite.config.js` | ||
```log | ||
{ "type": "module" } | ||
โโโโโโโโโโโโโโโโโโโโโโณโโโโโโโโโโโณโโโโโโโโโโโโ | ||
โ built โ format โ suffix โ | ||
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโจ | ||
โ main process โ esm โ .js โ | ||
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโจ | ||
โ preload scripts โ cjs โ .mjs โ diff | ||
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโจ | ||
โ renderer process โ - โ .js โ | ||
โโโโโโโโโโโโโโโโโโโโโโธโโโโโโโโโโโธโโโโโโโโโโโโ | ||
{ "type": "commonjs" } - default | ||
โโโโโโโโโโโโโโโโโโโโโโณโโโโโโโโโโโณโโโโโโโโโโโโ | ||
โ built โ format โ suffix โ | ||
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโจ | ||
โ main process โ cjs โ .js โ | ||
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโจ | ||
โ preload scripts โ cjs โ .js โ diff | ||
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโจ | ||
โ renderer process โ - โ .js โ | ||
โโโโโโโโโโโโโโโโโโโโโโธโโโโโโโโโโโธโโโโโโโโโโโโ | ||
``` | ||
## Examples | ||
@@ -179,0 +207,0 @@ |
Sorry, the diff of this file is not supported yet
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
53488
936
368