Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions packages/vite/src/node/__tests__/filterRegex.spec.ts
Comment thread
bluwy marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { describe, expect, test } from 'vitest'
import { assetImportMetaUrlRE } from '../plugins/assetImportMetaUrl'
import { workerImportMetaUrlRE } from '../plugins/workerImportMetaUrl'

describe('filter regexes do not cause catastrophic backtracking', () => {
const largeCode =
`new URL('https://example.com');\n`.repeat(200) +
`var a = 1;\n`.repeat(200_000)

test('assetImportMetaUrlRE completes without backtracking on large files', () => {
assetImportMetaUrlRE.lastIndex = 0
expect(assetImportMetaUrlRE.test(largeCode)).toBe(false)
})

test('workerImportMetaUrlRE completes without backtracking on large files', () => {
workerImportMetaUrlRE.lastIndex = 0
expect(workerImportMetaUrlRE.test(largeCode)).toBe(false)
})

test('assetImportMetaUrlRE still matches valid patterns', () => {
assetImportMetaUrlRE.lastIndex = 0
expect(
assetImportMetaUrlRE.test(`new URL('./asset.png', import.meta.url)`),
).toBe(true)
})

test('workerImportMetaUrlRE still matches valid patterns', () => {
workerImportMetaUrlRE.lastIndex = 0
expect(
workerImportMetaUrlRE.test(
`new Worker(new URL('./worker.js', import.meta.url))`,
),
).toBe(true)
})
})
8 changes: 4 additions & 4 deletions packages/vite/src/node/optimizer/rolldownDepPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import type { Environment } from '../environment'
import { createBackCompatIdResolver } from '../idResolver'
import { isWindows } from '../../shared/utils'
import { hasViteIgnoreRE } from '../plugins/importAnalysis'
import { assetImportMetaUrlRE } from '../plugins/assetImportMetaUrl'

const externalWithConversionNamespace =
'vite:dep-pre-bundle:external-conversion'
Expand Down Expand Up @@ -313,16 +314,15 @@ export function rolldownDepPlugin(
},
transform: {
filter: {
code: /new\s+URL.+import\.meta\.url/s,
code: assetImportMetaUrlRE,
},
async handler(code, id) {
let s: MagicString | undefined
const assetImportMetaUrlRE =
/\bnew\s+URL\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*(?:,\s*)?\)/dg
const re = new RegExp(assetImportMetaUrlRE)
const cleanString = stripLiteral(code)

let match: RegExpExecArray | null
while ((match = assetImportMetaUrlRE.exec(cleanString))) {
while ((match = re.exec(cleanString))) {
const [[startIndex, endIndex], [urlStart, urlEnd]] = match.indices!
if (hasViteIgnoreRE.test(code.slice(startIndex, urlStart))) continue

Expand Down
10 changes: 6 additions & 4 deletions packages/vite/src/node/plugins/assetImportMetaUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import { hasViteIgnoreRE } from './importAnalysis'
* import.meta.glob('./dir/**.png', { eager: true, import: 'default' })[`./dir/${name}.png`]
* ```
*/
export const assetImportMetaUrlRE: RegExp =
/\bnew\s+URL\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*(?:,\s*)?\)/dg

export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
const { publicDir } = config
let assetResolver: ResolveIdFn
Expand All @@ -56,16 +59,15 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
id: {
exclude: [exactRegex(preloadHelperId), exactRegex(CLIENT_ENTRY)],
},
code: /new\s+URL.+import\.meta\.url/s,
code: assetImportMetaUrlRE,
},
async handler(code, id) {
let s: MagicString | undefined
const assetImportMetaUrlRE =
/\bnew\s+URL\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*(?:,\s*)?\)/dg
const re = new RegExp(assetImportMetaUrlRE)
const cleanString = stripLiteral(code)

let match: RegExpExecArray | null
while ((match = assetImportMetaUrlRE.exec(cleanString))) {
while ((match = re.exec(cleanString))) {
const [[startIndex, endIndex], [urlStart, urlEnd]] = match.indices!
if (hasViteIgnoreRE.test(code.slice(startIndex, urlStart))) continue

Expand Down
9 changes: 4 additions & 5 deletions packages/vite/src/node/plugins/workerImportMetaUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ async function getWorkerType(
return 'classic'
}

const workerImportMetaUrlRE =
/new\s+(?:Worker|SharedWorker)\s*\(\s*new\s+URL.+?import\.meta\.url/s
export const workerImportMetaUrlRE: RegExp =
/\bnew\s+(?:Worker|SharedWorker)\s*\(\s*(new\s+URL\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*(?:,\s*)?\))/dg

export function workerImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
const isBundled = config.isBundled
Expand All @@ -209,11 +209,10 @@ export function workerImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
async handler(code, id) {
let s: MagicString | undefined
const cleanString = stripLiteral(code)
const workerImportMetaUrlRE =
/\bnew\s+(?:Worker|SharedWorker)\s*\(\s*(new\s+URL\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*(?:,\s*)?\))/dg
const re = new RegExp(workerImportMetaUrlRE)

let match: RegExpExecArray | null
while ((match = workerImportMetaUrlRE.exec(cleanString))) {
while ((match = re.exec(cleanString))) {
const [[, endIndex], [expStart, expEnd], [urlStart, urlEnd]] =
match.indices!

Expand Down
Loading