perf(plugin): no need to cache sourcemap option

This commit is contained in:
alex8088 2025-10-19 12:11:29 +08:00
parent 70e027d38a
commit c7955aa6fd
5 changed files with 6 additions and 23 deletions

View File

@ -26,7 +26,6 @@ export default async function loadWasm(file, importObject = {}) {
` `
export default function assetPlugin(): Plugin { export default function assetPlugin(): Plugin {
let sourcemap: boolean | 'inline' | 'hidden' = false
let publicDir = '' let publicDir = ''
let outDir = '' let outDir = ''
const publicAssetPathCache = new Map<string, string>() const publicAssetPathCache = new Map<string, string>()
@ -40,7 +39,6 @@ export default function assetPlugin(): Plugin {
assetCache.clear() assetCache.clear()
}, },
configResolved(config): void { configResolved(config): void {
sourcemap = config.build.sourcemap
publicDir = normalizePath(config.publicDir) publicDir = normalizePath(config.publicDir)
outDir = normalizePath(path.resolve(config.root, config.build.outDir)) outDir = normalizePath(path.resolve(config.root, config.build.outDir))
}, },
@ -104,7 +102,7 @@ export default function assetPlugin(): Plugin {
export default importObject => loadWasm(${referenceId}, importObject)` export default importObject => loadWasm(${referenceId}, importObject)`
} }
}, },
renderChunk(code, chunk): { code: string; map: SourceMapInput } | null { renderChunk(code, chunk, { sourcemap }): { code: string; map: SourceMapInput } | null {
let match: RegExpExecArray | null let match: RegExpExecArray | null
let s: MagicString | undefined let s: MagicString | undefined

View File

@ -186,7 +186,6 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
const bytecodeModuleLoader = 'bytecode-loader.cjs' const bytecodeModuleLoader = 'bytecode-loader.cjs'
let logger: Logger let logger: Logger
let sourcemap: boolean | 'inline' | 'hidden' = false
let supported = false let supported = false
return { return {
@ -198,7 +197,6 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
return return
} }
logger = config.logger logger = config.logger
sourcemap = config.build.sourcemap
const useInRenderer = config.plugins.some(p => p.name === 'vite:electron-renderer-preset-config') const useInRenderer = config.plugins.some(p => p.name === 'vite:electron-renderer-preset-config')
if (useInRenderer) { if (useInRenderer) {
config.logger.warn(colors.yellow('bytecodePlugin does not support renderer.')) config.logger.warn(colors.yellow('bytecodePlugin does not support renderer.'))
@ -223,7 +221,7 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
config.logger.warn(colors.yellow('Strings cannot be protected when minification is enabled.')) config.logger.warn(colors.yellow('Strings cannot be protected when minification is enabled.'))
} }
}, },
renderChunk(code, chunk): { code: string; map?: SourceMapInput } | null { renderChunk(code, chunk, { sourcemap }): { code: string; map?: SourceMapInput } | null {
if (supported && isBytecodeChunk(chunk.name) && shouldTransformBytecodeChunk) { if (supported && isBytecodeChunk(chunk.name) && shouldTransformBytecodeChunk) {
return _transform(code, !!sourcemap) return _transform(code, !!sourcemap)
} }

View File

@ -46,19 +46,14 @@ function findStaticImports(code: string): StaticImport[] {
} }
export default function esmShimPlugin(): Plugin { export default function esmShimPlugin(): Plugin {
let sourcemap: boolean | 'inline' | 'hidden' = false
const CJSShim = getElectronMajorVersion() >= 30 ? CJSShim_node_20_11 : CJSShim_normal const CJSShim = getElectronMajorVersion() >= 30 ? CJSShim_node_20_11 : CJSShim_normal
return { return {
name: 'vite:esm-shim', name: 'vite:esm-shim',
apply: 'build', apply: 'build',
enforce: 'post', enforce: 'post',
configResolved(config): void { renderChunk(code, _chunk, { format, sourcemap }): { code: string; map?: SourceMapInput } | null {
sourcemap = config.build.sourcemap if (format === 'es') {
},
renderChunk(code, _chunk, options): { code: string; map?: SourceMapInput } | null {
if (options.format === 'es') {
if (code.includes(CJSShim) || !CJSyntaxRe.test(code)) { if (code.includes(CJSShim) || !CJSyntaxRe.test(code)) {
return null return null
} }

View File

@ -10,14 +10,10 @@ const modulePathRE = /__VITE_MODULE_PATH__([\w$]+)__/g
* Resolve `?modulePath` import and return the module bundle path. * Resolve `?modulePath` import and return the module bundle path.
*/ */
export default function modulePathPlugin(config: InlineConfig): Plugin { export default function modulePathPlugin(config: InlineConfig): Plugin {
let sourcemap: boolean | 'inline' | 'hidden' = false
return { return {
name: 'vite:module-path', name: 'vite:module-path',
apply: 'build', apply: 'build',
enforce: 'pre', enforce: 'pre',
configResolved(config): void {
sourcemap = config.build.sourcemap
},
async load(id): Promise<string | void> { async load(id): Promise<string | void> {
if (id.endsWith('?modulePath')) { if (id.endsWith('?modulePath')) {
// id resolved by Vite resolve plugin // id resolved by Vite resolve plugin
@ -41,7 +37,7 @@ export default function modulePathPlugin(config: InlineConfig): Plugin {
export default join(__dirname, ${refId})` export default join(__dirname, ${refId})`
} }
}, },
renderChunk(code, chunk): { code: string; map: SourceMapInput } | null { renderChunk(code, chunk, { sourcemap }): { code: string; map: SourceMapInput } | null {
if (code.match(modulePathRE)) { if (code.match(modulePathRE)) {
let match: RegExpExecArray | null let match: RegExpExecArray | null
const s = new MagicString(code) const s = new MagicString(code)

View File

@ -11,14 +11,10 @@ const nodeWorkerImporterRE = /(?:\?)nodeWorker&importer=([^&]+)(?:&|$)/
* Resolve `?nodeWorker` import and automatically generate `Worker` wrapper. * Resolve `?nodeWorker` import and automatically generate `Worker` wrapper.
*/ */
export default function workerPlugin(): Plugin { export default function workerPlugin(): Plugin {
let sourcemap: boolean | 'inline' | 'hidden' = false
return { return {
name: 'vite:node-worker', name: 'vite:node-worker',
apply: 'build', apply: 'build',
enforce: 'pre', enforce: 'pre',
configResolved(config): void {
sourcemap = config.build.sourcemap
},
resolveId(id, importer): string | void { resolveId(id, importer): string | void {
if (id.endsWith('?nodeWorker')) { if (id.endsWith('?nodeWorker')) {
return id + `&importer=${importer}` return id + `&importer=${importer}`
@ -40,7 +36,7 @@ export default function workerPlugin(): Plugin {
} }
} }
}, },
renderChunk(code, chunk): { code: string; map: SourceMapInput } | null { renderChunk(code, chunk, { sourcemap }): { code: string; map: SourceMapInput } | null {
if (code.match(nodeWorkerAssetUrlRE)) { if (code.match(nodeWorkerAssetUrlRE)) {
let match: RegExpExecArray | null let match: RegExpExecArray | null
const s = new MagicString(code) const s = new MagicString(code)