perf: skip Babel loading in development and builds without bytecode

This commit is contained in:
alex8088 2026-10-11 02:01:18 +08:00
parent 19050aaec9
commit 3ec79bbf65

View File

@ -1,14 +1,18 @@
import path from 'node:path' import path from 'node:path'
import { randomBytes } from 'node:crypto' import { randomBytes } from 'node:crypto'
import { createRequire } from 'node:module'
import colors from 'picocolors' import colors from 'picocolors'
import { type Plugin, type Rolldown, normalizePath } from 'vite' import { type Plugin, type Rolldown, normalizePath } from 'vite'
import * as babel from '@babel/core' import type * as Babel from '@babel/core'
import MagicString from 'magic-string' import MagicString from 'magic-string'
import { toRelativePath, resolveBuildOutputs } from '../utils' import { toRelativePath, resolveBuildOutputs } from '../utils'
import type { BytecodeCompiler, BytecodeTarget } from '../bytecodeCompiler' import type { BytecodeCompiler, BytecodeTarget } from '../bytecodeCompiler'
// Inspired by https://github.com/bytenode/bytenode // Inspired by https://github.com/bytenode/bytenode
const require = createRequire(import.meta.url)
let babel: typeof Babel | undefined
function getBytecodeCompiler(plugins: readonly Plugin[]): BytecodeCompiler | undefined { function getBytecodeCompiler(plugins: readonly Plugin[]): BytecodeCompiler | undefined {
const plugin = plugins.find(plugin => plugin.name === 'vite:electron-bytecode-build') const plugin = plugins.find(plugin => plugin.name === 'vite:electron-bytecode-build')
return (plugin?.api as { compiler?: BytecodeCompiler } | undefined)?.compiler return (plugin?.api as { compiler?: BytecodeCompiler } | undefined)?.compiler
@ -97,14 +101,14 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
return _chunkAlias.length === 0 || _chunkAlias.some(alias => alias === chunkName) return _chunkAlias.length === 0 || _chunkAlias.some(alias => alias === chunkName)
} }
const plugins: babel.PluginItem[] = [] const plugins: Babel.PluginItem[] = []
if (transformArrowFunctions) { if (transformArrowFunctions) {
plugins.push('@babel/plugin-transform-arrow-functions') plugins.push('@babel/plugin-transform-arrow-functions')
} }
if (protectedStrings.length > 0) { if (protectedStrings.length > 0) {
plugins.push([protectStringsPlugin as babel.PluginTarget, { protectedStrings: new Set(protectedStrings) }]) plugins.push([protectStringsPlugin as Babel.PluginTarget, { protectedStrings: new Set(protectedStrings) }])
} }
const shouldTransformBytecodeChunk = plugins.length !== 0 const shouldTransformBytecodeChunk = plugins.length !== 0
@ -113,6 +117,7 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
code: string, code: string,
sourceMaps: boolean = false sourceMaps: boolean = false
): { code: string; map?: Rolldown.SourceMapInput } | null => { ): { code: string; map?: Rolldown.SourceMapInput } | null => {
babel ??= require('@babel/core') as typeof Babel
const re = babel.transformSync(code, { plugins, sourceMaps }) const re = babel.transformSync(code, { plugins, sourceMaps })
return re ? { code: re.code || '', map: re.map as Rolldown.SourceMapInput } : null return re ? { code: re.code || '', map: re.map as Rolldown.SourceMapInput } : null
} }
@ -279,14 +284,14 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
} }
} }
interface ProtectStringsPluginState extends babel.PluginPass { interface ProtectStringsPluginState extends Babel.PluginPass {
opts: { protectedStrings: Set<string> } opts: { protectedStrings: Set<string> }
} }
function protectStringsPlugin(api: typeof babel & babel.ConfigAPI): babel.PluginObj<ProtectStringsPluginState> { function protectStringsPlugin(api: typeof Babel & Babel.ConfigAPI): Babel.PluginObj<ProtectStringsPluginState> {
const { types: t } = api const { types: t } = api
function createFromCharCodeFunction(value: string): babel.types.CallExpression { function createFromCharCodeFunction(value: string): Babel.types.CallExpression {
const charCodes = Array.from(value).map(s => s.charCodeAt(0)) const charCodes = Array.from(value).map(s => s.charCodeAt(0))
const charCodeLiterals = charCodes.map(code => t.numericLiteral(code)) const charCodeLiterals = charCodes.map(code => t.numericLiteral(code))