diff --git a/packages/vite/src/node/optimizer/scan.ts b/packages/vite/src/node/optimizer/scan.ts index bcf6fd92c7e662..1cdef6c339c103 100644 --- a/packages/vite/src/node/optimizer/scan.ts +++ b/packages/vite/src/node/optimizer/scan.ts @@ -409,12 +409,10 @@ function esbuildScanPlugin( // Avoid matching the content of the comment raw = raw.replace(commentRE, '') const isHtml = p.endsWith('.html') - scriptRE.lastIndex = 0 let js = '' let scriptId = 0 - let match: RegExpExecArray | null - while ((match = scriptRE.exec(raw))) { - const [, openTag, content] = match + const matches = raw.matchAll(scriptRE) + for (const [, openTag, content] of matches) { const typeMatch = openTag.match(typeRE) const type = typeMatch && (typeMatch[1] || typeMatch[2] || typeMatch[3])