diff --git a/src/plugins/asset.ts b/src/plugins/asset.ts index 3b0e7d2..7292ee9 100644 --- a/src/plugins/asset.ts +++ b/src/plugins/asset.ts @@ -107,31 +107,27 @@ 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 - const filename = this.getFileName(hash) - const outputFilepath = toRelativePath(filename, chunk.fileName) - const replacement = JSON.stringify(outputFilepath) - s.overwrite(match.index, match.index + full.length, replacement, { - contentOnly: true - }) - } + while ((match = nodeAssetRE.exec(code))) { + s ||= new MagicString(code) + const [full, hash] = match + const filename = this.getFileName(hash) + const outputFilepath = toRelativePath(filename, chunk.fileName) + const replacement = JSON.stringify(outputFilepath) + s.overwrite(match.index, match.index + full.length, replacement, { + contentOnly: true + }) } nodePublicAssetRE.lastIndex = 0 - if (code.match(nodePublicAssetRE)) { - while ((match = nodePublicAssetRE.exec(code))) { - s ||= new MagicString(code) - const [full, hash] = match - const filename = publicAssetPathCache.get(hash)! - const outputFilepath = toRelativePath(filename, normalizePath(path.join(outDir, chunk.fileName))) - const replacement = JSON.stringify(outputFilepath) - s.overwrite(match.index, match.index + full.length, replacement, { - contentOnly: true - }) - } + while ((match = nodePublicAssetRE.exec(code))) { + s ||= new MagicString(code) + const [full, hash] = match + const filename = publicAssetPathCache.get(hash)! + const outputFilepath = toRelativePath(filename, normalizePath(path.join(outDir, chunk.fileName))) + const replacement = JSON.stringify(outputFilepath) + s.overwrite(match.index, match.index + full.length, replacement, { + contentOnly: true + }) } if (s) { @@ -139,9 +135,9 @@ export default function assetPlugin(): Plugin { code: s.toString(), map: sourcemap ? s.generateMap({ hires: 'boundary' }) : null } - } else { - return null } + + return null } } } diff --git a/src/plugins/bytecode.ts b/src/plugins/bytecode.ts index 276110e..eeed31c 100644 --- a/src/plugins/bytecode.ts +++ b/src/plugins/bytecode.ts @@ -255,17 +255,20 @@ 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 }) } - _code = s.toString() + if (s) { + _code = s.toString() + } } if (bytecodeChunks.includes(name)) { const bytecodeBuffer = await compileToBytecode(_code) diff --git a/src/plugins/modulePath.ts b/src/plugins/modulePath.ts index c2f2796..06c0374 100644 --- a/src/plugins/modulePath.ts +++ b/src/plugins/modulePath.ts @@ -38,20 +38,22 @@ 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 match: RegExpExecArray | null + let s: MagicString | undefined - while ((match = modulePathRE.exec(code))) { - const [full, hash] = match - const filename = this.getFileName(hash) - const outputFilepath = toRelativePath(filename, chunk.fileName) - const replacement = JSON.stringify(outputFilepath) - s.overwrite(match.index, match.index + full.length, replacement, { - contentOnly: true - }) - } + 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) + const replacement = JSON.stringify(outputFilepath) + s.overwrite(match.index, match.index + full.length, replacement, { + contentOnly: true + }) + } + if (s) { return { code: s.toString(), map: sourcemap ? s.generateMap({ hires: 'boundary' }) : null diff --git a/src/plugins/worker.ts b/src/plugins/worker.ts index e17b888..b13b369 100644 --- a/src/plugins/worker.ts +++ b/src/plugins/worker.ts @@ -37,20 +37,22 @@ 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 match: RegExpExecArray | null + let s: MagicString | undefined - while ((match = nodeWorkerAssetUrlRE.exec(code))) { - const [full, hash] = match - const filename = this.getFileName(hash) - const outputFilepath = toRelativePath(filename, chunk.fileName) - const replacement = JSON.stringify(outputFilepath) - s.overwrite(match.index, match.index + full.length, replacement, { - contentOnly: true - }) - } + 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) + const replacement = JSON.stringify(outputFilepath) + s.overwrite(match.index, match.index + full.length, replacement, { + contentOnly: true + }) + } + if (s) { return { code: s.toString(), map: sourcemap ? s.generateMap({ hires: 'boundary' }) : null