Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reset global regex before match #11132

Merged
merged 1 commit into from Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/vite/src/client/overlay.ts
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
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++
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain why this isnt needed any more?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used to safe-guard zero-width matches, e.g. the regex would match "". But since importsRE is guaranteed to never do that (it always captures some text), this can be removed.

I think this part was copied from https://regex101.com generated output before.

js += `\nimport ${m[1]}`
}
return js
Expand Down
2 changes: 2 additions & 0 deletions packages/vite/src/node/plugins/asset.ts
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
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
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