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 {
let sourcemap: boolean | 'inline' | 'hidden' = false
let publicDir = ''
let outDir = ''
const publicAssetPathCache = new Map<string, string>()
@ -40,7 +39,6 @@ export default function assetPlugin(): Plugin {
assetCache.clear()
},
configResolved(config): void {
sourcemap = config.build.sourcemap
publicDir = normalizePath(config.publicDir)
outDir = normalizePath(path.resolve(config.root, config.build.outDir))
},
@ -104,7 +102,7 @@ export default function assetPlugin(): Plugin {
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 s: MagicString | undefined

View File

@ -186,7 +186,6 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
const bytecodeModuleLoader = 'bytecode-loader.cjs'
let logger: Logger
let sourcemap: boolean | 'inline' | 'hidden' = false
let supported = false
return {
@ -198,7 +197,6 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
return
}
logger = config.logger
sourcemap = config.build.sourcemap
const useInRenderer = config.plugins.some(p => p.name === 'vite:electron-renderer-preset-config')
if (useInRenderer) {
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.'))
}
},
renderChunk(code, chunk): { code: string; map?: SourceMapInput } | null {
renderChunk(code, chunk, { sourcemap }): { code: string; map?: SourceMapInput } | null {
if (supported && isBytecodeChunk(chunk.name) && shouldTransformBytecodeChunk) {
return _transform(code, !!sourcemap)
}

View File

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

View File

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

View File

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