mirror of
https://github.com/alex8088/electron-vite.git
synced 2025-11-10 06:24:33 +08:00
perf(plugin): lazily initialize MagicString and remove the redundant pre-check
This commit is contained in:
parent
c7955aa6fd
commit
0fb8918090
@ -107,7 +107,6 @@ export default function assetPlugin(): Plugin {
|
|||||||
let s: MagicString | undefined
|
let s: MagicString | undefined
|
||||||
|
|
||||||
nodeAssetRE.lastIndex = 0
|
nodeAssetRE.lastIndex = 0
|
||||||
if (code.match(nodeAssetRE)) {
|
|
||||||
while ((match = nodeAssetRE.exec(code))) {
|
while ((match = nodeAssetRE.exec(code))) {
|
||||||
s ||= new MagicString(code)
|
s ||= new MagicString(code)
|
||||||
const [full, hash] = match
|
const [full, hash] = match
|
||||||
@ -118,10 +117,8 @@ export default function assetPlugin(): Plugin {
|
|||||||
contentOnly: true
|
contentOnly: true
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
nodePublicAssetRE.lastIndex = 0
|
nodePublicAssetRE.lastIndex = 0
|
||||||
if (code.match(nodePublicAssetRE)) {
|
|
||||||
while ((match = nodePublicAssetRE.exec(code))) {
|
while ((match = nodePublicAssetRE.exec(code))) {
|
||||||
s ||= new MagicString(code)
|
s ||= new MagicString(code)
|
||||||
const [full, hash] = match
|
const [full, hash] = match
|
||||||
@ -132,16 +129,15 @@ export default function assetPlugin(): Plugin {
|
|||||||
contentOnly: true
|
contentOnly: true
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (s) {
|
if (s) {
|
||||||
return {
|
return {
|
||||||
code: s.toString(),
|
code: s.toString(),
|
||||||
map: sourcemap ? s.generateMap({ hires: 'boundary' }) : null
|
map: sourcemap ? s.generateMap({ hires: 'boundary' }) : null
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -255,18 +255,21 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
|
|||||||
const chunk = output[name]
|
const chunk = output[name]
|
||||||
if (chunk.type === 'chunk') {
|
if (chunk.type === 'chunk') {
|
||||||
let _code = chunk.code
|
let _code = chunk.code
|
||||||
if (bytecodeRE && _code.match(bytecodeRE)) {
|
if (bytecodeRE) {
|
||||||
let match: RegExpExecArray | null
|
let match: RegExpExecArray | null
|
||||||
const s = new MagicString(_code)
|
let s: MagicString | undefined
|
||||||
while ((match = bytecodeRE.exec(_code))) {
|
while ((match = bytecodeRE.exec(_code))) {
|
||||||
|
s ||= new MagicString(_code)
|
||||||
const [prefix, chunkName] = match
|
const [prefix, chunkName] = match
|
||||||
const len = prefix.length + chunkName.length
|
const len = prefix.length + chunkName.length
|
||||||
s.overwrite(match.index, match.index + len, prefix + chunkName + 'c', {
|
s.overwrite(match.index, match.index + len, prefix + chunkName + 'c', {
|
||||||
contentOnly: true
|
contentOnly: true
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
if (s) {
|
||||||
_code = s.toString()
|
_code = s.toString()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (bytecodeChunks.includes(name)) {
|
if (bytecodeChunks.includes(name)) {
|
||||||
const bytecodeBuffer = await compileToBytecode(_code)
|
const bytecodeBuffer = await compileToBytecode(_code)
|
||||||
this.emitFile({
|
this.emitFile({
|
||||||
|
|||||||
@ -38,11 +38,12 @@ export default function modulePathPlugin(config: InlineConfig): Plugin {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
renderChunk(code, chunk, { sourcemap }): { code: string; map: SourceMapInput } | null {
|
renderChunk(code, chunk, { sourcemap }): { code: string; map: SourceMapInput } | null {
|
||||||
if (code.match(modulePathRE)) {
|
|
||||||
let match: RegExpExecArray | null
|
let match: RegExpExecArray | null
|
||||||
const s = new MagicString(code)
|
let s: MagicString | undefined
|
||||||
|
|
||||||
|
modulePathRE.lastIndex = 0
|
||||||
while ((match = modulePathRE.exec(code))) {
|
while ((match = modulePathRE.exec(code))) {
|
||||||
|
s ||= new MagicString(code)
|
||||||
const [full, hash] = match
|
const [full, hash] = match
|
||||||
const filename = this.getFileName(hash)
|
const filename = this.getFileName(hash)
|
||||||
const outputFilepath = toRelativePath(filename, chunk.fileName)
|
const outputFilepath = toRelativePath(filename, chunk.fileName)
|
||||||
@ -52,6 +53,7 @@ export default function modulePathPlugin(config: InlineConfig): Plugin {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (s) {
|
||||||
return {
|
return {
|
||||||
code: s.toString(),
|
code: s.toString(),
|
||||||
map: sourcemap ? s.generateMap({ hires: 'boundary' }) : null
|
map: sourcemap ? s.generateMap({ hires: 'boundary' }) : null
|
||||||
|
|||||||
@ -37,11 +37,12 @@ export default function workerPlugin(): Plugin {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
renderChunk(code, chunk, { sourcemap }): { code: string; map: SourceMapInput } | null {
|
renderChunk(code, chunk, { sourcemap }): { code: string; map: SourceMapInput } | null {
|
||||||
if (code.match(nodeWorkerAssetUrlRE)) {
|
|
||||||
let match: RegExpExecArray | null
|
let match: RegExpExecArray | null
|
||||||
const s = new MagicString(code)
|
let s: MagicString | undefined
|
||||||
|
|
||||||
|
nodeWorkerAssetUrlRE.lastIndex = 0
|
||||||
while ((match = nodeWorkerAssetUrlRE.exec(code))) {
|
while ((match = nodeWorkerAssetUrlRE.exec(code))) {
|
||||||
|
s ||= new MagicString(code)
|
||||||
const [full, hash] = match
|
const [full, hash] = match
|
||||||
const filename = this.getFileName(hash)
|
const filename = this.getFileName(hash)
|
||||||
const outputFilepath = toRelativePath(filename, chunk.fileName)
|
const outputFilepath = toRelativePath(filename, chunk.fileName)
|
||||||
@ -51,6 +52,7 @@ export default function workerPlugin(): Plugin {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (s) {
|
||||||
return {
|
return {
|
||||||
code: s.toString(),
|
code: s.toString(),
|
||||||
map: sourcemap ? s.generateMap({ hires: 'boundary' }) : null
|
map: sourcemap ? s.generateMap({ hires: 'boundary' }) : null
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user