Skip to content

Commit

Permalink
fix: reset global regex before match (vitejs#11132)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored and futurGH committed Feb 26, 2023
1 parent 883f9a5 commit ed1d3c1
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/vite/src/client/overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 1 addition & 4 deletions packages/vite/src/node/optimizer/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions packages/vite/src/node/plugins/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/node/plugins/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)!
Expand Down

0 comments on commit ed1d3c1

Please sign in to comment.