From 6b0f85a2a79f8fdcb0e7d3e201bc7c320d2eb05d Mon Sep 17 00:00:00 2001 From: alex8088 <244096523@qq.com> Date: Sat, 10 Oct 2026 02:03:25 +0800 Subject: [PATCH] chore: simplify bytecode compiler initialization --- src/build.ts | 11 ++++---- src/bytecode/build.ts | 25 ------------------- .../compiler.ts => bytecodeCompiler.ts} | 2 +- src/constants.ts | 1 + src/plugins/bytecode.ts | 23 ++++++++++------- 5 files changed, 22 insertions(+), 40 deletions(-) delete mode 100644 src/bytecode/build.ts rename src/{bytecode/compiler.ts => bytecodeCompiler.ts} (99%) create mode 100644 src/constants.ts diff --git a/src/build.ts b/src/build.ts index 7754edd..25106fd 100644 --- a/src/build.ts +++ b/src/build.ts @@ -1,6 +1,7 @@ import { build as viteBuild, mergeConfig } from 'vite' import { type InlineConfig, type MainViteConfig, type PreloadViteConfig, resolveConfig } from './config' -import { BytecodeBuild, bytecodeBuildPluginName } from './bytecode/build' +import { BytecodeCompiler } from './bytecodeCompiler' +import { BYTECODE_BUILD_PLUGIN_NAME } from './constants' import { asyncFlatten } from './utils' async function useBytecode(config?: MainViteConfig | PreloadViteConfig): Promise { @@ -15,10 +16,10 @@ async function useBytecode(config?: MainViteConfig | PreloadViteConfig): Promise export async function build(inlineConfig: InlineConfig = {}): Promise { process.env.NODE_ENV_ELECTRON_VITE = 'production' - const bytecodeBuild = new BytecodeBuild() + const bytecodeCompiler = new BytecodeCompiler() const config = await resolveConfig( mergeConfig(inlineConfig, { - plugins: [{ name: bytecodeBuildPluginName, api: { build: bytecodeBuild } }] + plugins: [{ name: BYTECODE_BUILD_PLUGIN_NAME, api: { compiler: bytecodeCompiler } }] }), 'build', 'production' @@ -49,9 +50,9 @@ export async function build(inlineConfig: InlineConfig = {}): Promise { if (!hasBytecode) return buildTargets() try { - await bytecodeBuild.start() + await bytecodeCompiler.start() await buildTargets() } finally { - await bytecodeBuild.stop() + await bytecodeCompiler.stop() } } diff --git a/src/bytecode/build.ts b/src/bytecode/build.ts deleted file mode 100644 index 3d21668..0000000 --- a/src/bytecode/build.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { type Plugin } from 'vite' -import { BytecodeCompiler, type BytecodeCompileResult, type BytecodeTarget } from './compiler' - -export const bytecodeBuildPluginName = 'vite:electron-bytecode-build' - -export class BytecodeBuild { - private readonly compiler = new BytecodeCompiler() - - start(): Promise { - return this.compiler.start() - } - - compile(target: BytecodeTarget, code: string): Promise { - return this.compiler.compile(target, code) - } - - stop(): Promise { - return this.compiler.stop() - } -} - -export function getBytecodeBuild(plugins: readonly Plugin[]): BytecodeBuild | undefined { - const plugin = plugins.find(plugin => plugin.name === bytecodeBuildPluginName) - return (plugin?.api as { build?: BytecodeBuild } | undefined)?.build -} diff --git a/src/bytecode/compiler.ts b/src/bytecodeCompiler.ts similarity index 99% rename from src/bytecode/compiler.ts rename to src/bytecodeCompiler.ts index a29efcf..f0390ee 100644 --- a/src/bytecode/compiler.ts +++ b/src/bytecodeCompiler.ts @@ -1,7 +1,7 @@ import { type ChildProcess, spawn } from 'node:child_process' import path from 'node:path' import { createRequire } from 'node:module' -import { getElectronPath } from '../electron' +import { getElectronPath } from './electron' const require = createRequire(import.meta.url) const RPC_TIMEOUT = 30_000 diff --git a/src/constants.ts b/src/constants.ts new file mode 100644 index 0000000..05ad834 --- /dev/null +++ b/src/constants.ts @@ -0,0 +1 @@ +export const BYTECODE_BUILD_PLUGIN_NAME = 'vite:electron-bytecode-build' diff --git a/src/plugins/bytecode.ts b/src/plugins/bytecode.ts index 090cf92..8ab0f28 100644 --- a/src/plugins/bytecode.ts +++ b/src/plugins/bytecode.ts @@ -5,18 +5,23 @@ import { type Plugin, type LibraryOptions, type Rolldown, normalizePath } from ' import * as babel from '@babel/core' import MagicString from 'magic-string' import { toRelativePath } from '../utils' -import { type BytecodeBuild, getBytecodeBuild } from '../bytecode/build' -import { type BytecodeTarget } from '../bytecode/compiler' +import type { BytecodeCompiler, BytecodeTarget } from '../bytecodeCompiler' +import { BYTECODE_BUILD_PLUGIN_NAME } from '../constants' // Inspired by https://github.com/bytenode/bytenode +function getBytecodeCompiler(plugins: readonly Plugin[]): BytecodeCompiler | undefined { + const plugin = plugins.find(plugin => plugin.name === BYTECODE_BUILD_PLUGIN_NAME) + return (plugin?.api as { compiler?: BytecodeCompiler } | undefined)?.compiler +} + // Versioned envelope: magic/version (8), UTF-16 source length (4), module ID (16), V8 cache. // Keep our metadata separate from the private V8 cache header. -async function compileToBytecode(build: BytecodeBuild, target: BytecodeTarget, code: string): Promise { +async function compileToBytecode(compiler: BytecodeCompiler, target: BytecodeTarget, code: string): Promise { const id = randomBytes(16) // Reserve enough source space for an untruncated, per-artifact placeholder identity. const source = code + '\n/*' + id.toString('hex') + '*/' - const result = await build.compile(target, source) + const result = await compiler.compile(target, source) const header = Buffer.alloc(28) header.write('EVBC0001', 0, 'ascii') header.writeUInt32LE(result.sourceLength, 8) @@ -118,14 +123,14 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null { let supported = false let isPreload = false - let bytecodeBuild: BytecodeBuild | undefined + let bytecodeCompiler: BytecodeCompiler | undefined return { name: 'vite:bytecode', apply: 'build', enforce: 'post', configResolved(config): void { - bytecodeBuild = getBytecodeBuild(config.plugins) + bytecodeCompiler = getBytecodeCompiler(config.plugins) if (supported) { return } @@ -184,8 +189,8 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null { const bundles = Object.keys(output) - if (!bytecodeBuild) { - throw new Error('Bytecode compilation requires an electron-vite build session') + if (!bytecodeCompiler) { + throw new Error('Bytecode compilation requires an electron-vite build') } for (const name of bundles) { const chunk = output[name] @@ -207,7 +212,7 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null { } } if (bytecodeChunks.includes(name)) { - const bytecodeBuffer = await compileToBytecode(bytecodeBuild, isPreload ? 'preload' : 'main', _code) + const bytecodeBuffer = await compileToBytecode(bytecodeCompiler, isPreload ? 'preload' : 'main', _code) this.emitFile({ type: 'asset', fileName: name + 'c',