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 891be702cb6bb2..106a8358d2f327 100644 --- a/packages/vite/src/node/optimizer/scan.ts +++ b/packages/vite/src/node/optimizer/scan.ts @@ -534,11 +534,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 1e8b6303488fc3..2a16487a26bbc4 100644 --- a/packages/vite/src/node/plugins/asset.ts +++ b/packages/vite/src/node/plugins/asset.ts @@ -72,6 +72,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, referenceId, postfix = ''] = match @@ -96,6 +97,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 0561d305447f00..c9094ba6093d6e 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -398,6 +398,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) @@ -779,6 +780,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 9c3dc54654e2dd..ae13e60a052e62 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)!