perf(plugin): lazily initialize MagicString and remove the redundant pre-check

This commit is contained in:
alex8088 2025-10-19 14:42:35 +08:00
parent c7955aa6fd
commit 0fb8918090
4 changed files with 54 additions and 51 deletions

View File

@ -107,7 +107,6 @@ export default function assetPlugin(): Plugin {
let s: MagicString | undefined
nodeAssetRE.lastIndex = 0
if (code.match(nodeAssetRE)) {
while ((match = nodeAssetRE.exec(code))) {
s ||= new MagicString(code)
const [full, hash] = match
@ -118,10 +117,8 @@ export default function assetPlugin(): Plugin {
contentOnly: true
})
}
}
nodePublicAssetRE.lastIndex = 0
if (code.match(nodePublicAssetRE)) {
while ((match = nodePublicAssetRE.exec(code))) {
s ||= new MagicString(code)
const [full, hash] = match
@ -132,16 +129,15 @@ export default function assetPlugin(): Plugin {
contentOnly: true
})
}
}
if (s) {
return {
code: s.toString(),
map: sourcemap ? s.generateMap({ hires: 'boundary' }) : null
}
} else {
}
return null
}
}
}
}

View File

@ -255,18 +255,21 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
const chunk = output[name]
if (chunk.type === 'chunk') {
let _code = chunk.code
if (bytecodeRE && _code.match(bytecodeRE)) {
if (bytecodeRE) {
let match: RegExpExecArray | null
const s = new MagicString(_code)
let s: MagicString | undefined
while ((match = bytecodeRE.exec(_code))) {
s ||= new MagicString(_code)
const [prefix, chunkName] = match
const len = prefix.length + chunkName.length
s.overwrite(match.index, match.index + len, prefix + chunkName + 'c', {
contentOnly: true
})
}
if (s) {
_code = s.toString()
}
}
if (bytecodeChunks.includes(name)) {
const bytecodeBuffer = await compileToBytecode(_code)
this.emitFile({

View File

@ -38,11 +38,12 @@ export default function modulePathPlugin(config: InlineConfig): Plugin {
}
},
renderChunk(code, chunk, { sourcemap }): { code: string; map: SourceMapInput } | null {
if (code.match(modulePathRE)) {
let match: RegExpExecArray | null
const s = new MagicString(code)
let s: MagicString | undefined
modulePathRE.lastIndex = 0
while ((match = modulePathRE.exec(code))) {
s ||= new MagicString(code)
const [full, hash] = match
const filename = this.getFileName(hash)
const outputFilepath = toRelativePath(filename, chunk.fileName)
@ -52,6 +53,7 @@ export default function modulePathPlugin(config: InlineConfig): Plugin {
})
}
if (s) {
return {
code: s.toString(),
map: sourcemap ? s.generateMap({ hires: 'boundary' }) : null

View File

@ -37,11 +37,12 @@ export default function workerPlugin(): Plugin {
}
},
renderChunk(code, chunk, { sourcemap }): { code: string; map: SourceMapInput } | null {
if (code.match(nodeWorkerAssetUrlRE)) {
let match: RegExpExecArray | null
const s = new MagicString(code)
let s: MagicString | undefined
nodeWorkerAssetUrlRE.lastIndex = 0
while ((match = nodeWorkerAssetUrlRE.exec(code))) {
s ||= new MagicString(code)
const [full, hash] = match
const filename = this.getFileName(hash)
const outputFilepath = toRelativePath(filename, chunk.fileName)
@ -51,6 +52,7 @@ export default function workerPlugin(): Plugin {
})
}
if (s) {
return {
code: s.toString(),
map: sourcemap ? s.generateMap({ hires: 'boundary' }) : null