diff --git a/packages/vite/src/client/overlay.ts b/packages/vite/src/client/overlay.ts index 57171b7ec13c0b..91ad9cc6fb95df 100644 --- a/packages/vite/src/client/overlay.ts +++ b/packages/vite/src/client/overlay.ts @@ -178,6 +178,7 @@ export class ErrorOverlay extends HTMLElement { } else { let curIndex = 0 let match: RegExpExecArray | null + fileRE.lastIndex = 0 while ((match = fileRE.exec(text))) { const { 0: file, index } = match if (index != null) { diff --git a/packages/vite/src/node/optimizer/scan.ts b/packages/vite/src/node/optimizer/scan.ts index 7764705d76694c..3bacfa02abe043 100644 --- a/packages/vite/src/node/optimizer/scan.ts +++ b/packages/vite/src/node/optimizer/scan.ts @@ -531,11 +531,8 @@ function extractImportPaths(code: string) { let js = '' let m + importsRE.lastIndex = 0 while ((m = importsRE.exec(code)) != null) { - // This is necessary to avoid infinite loops with zero-width matches - if (m.index === importsRE.lastIndex) { - importsRE.lastIndex++ - } js += `\nimport ${m[1]}` } return js diff --git a/packages/vite/src/node/plugins/asset.ts b/packages/vite/src/node/plugins/asset.ts index 8a74736715bd9c..ee96fef25cc343 100644 --- a/packages/vite/src/node/plugins/asset.ts +++ b/packages/vite/src/node/plugins/asset.ts @@ -75,6 +75,7 @@ export function renderAssetUrlInJS( // In both cases, the wrapping should already be fine + assetUrlRE.lastIndex = 0 while ((match = assetUrlRE.exec(code))) { s ||= new MagicString(code) const [full, hash, postfix = ''] = match @@ -101,6 +102,7 @@ export function renderAssetUrlInJS( // Replace __VITE_PUBLIC_ASSET__5aa0ddc0__ with absolute paths const publicAssetUrlMap = publicAssetUrlCache.get(config)! + publicAssetUrlRE.lastIndex = 0 while ((match = publicAssetUrlRE.exec(code))) { s ||= new MagicString(code) const [full, hash] = match diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index c13a334ba7b7da..18e7fd75daf4e6 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -396,6 +396,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { const cleanCode = stripLiteral(scriptNode.value) let match: RegExpExecArray | null + inlineImportRE.lastIndex = 0 while ((match = inlineImportRE.exec(cleanCode))) { const { 1: url, index } = match const startUrl = cleanCode.indexOf(url, index) @@ -777,6 +778,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { // no use assets plugin because it will emit file let match: RegExpExecArray | null let s: MagicString | undefined + inlineCSSRE.lastIndex = 0 while ((match = inlineCSSRE.exec(result))) { s ||= new MagicString(result) const { 0: full, 1: scopedName } = match diff --git a/packages/vite/src/node/plugins/worker.ts b/packages/vite/src/node/plugins/worker.ts index 02d0dd3d74ece4..31b79cac8ca8d5 100644 --- a/packages/vite/src/node/plugins/worker.ts +++ b/packages/vite/src/node/plugins/worker.ts @@ -336,6 +336,7 @@ export function webWorkerPlugin(config: ResolvedConfig): Plugin { let match: RegExpExecArray | null s = new MagicString(code) + workerAssetUrlRE.lastIndex = 0 // Replace "__VITE_WORKER_ASSET__5aa0ddc0__" using relative paths const workerMap = workerCache.get(config.mainConfig || config)!