Skip to content

Commit f675a36

Browse files
Copilotsxzz
andcommitted
Address code review feedback: improve CSS fallback logic and add cache size limit for picomatch
Co-authored-by: sxzz <[email protected]>
1 parent e1be868 commit f675a36

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

src/features/css.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,15 @@ export function CssCodeSplitPlugin(
111111

112112
// Build an index of normalized chunk names for faster lookup
113113
const chunkNameIndex = new Map<string, string[]>()
114+
const chunkFileNamesByBaseName = new Map<string, string>()
114115
for (const chunkFileName of chunkCSSMap.keys()) {
115116
const chunkBaseName = normalizeChunkFileName(chunkFileName)
116117
if (!chunkNameIndex.has(chunkBaseName)) {
117118
chunkNameIndex.set(chunkBaseName, [])
118119
}
119120
chunkNameIndex.get(chunkBaseName)!.push(chunkFileName)
121+
// Store mapping from chunk file name to base name for prefix matching
122+
chunkFileNamesByBaseName.set(chunkFileName, chunkBaseName)
120123
}
121124

122125
// Match CSS assets to chunks by comparing base names
@@ -130,11 +133,22 @@ export function CssCodeSplitPlugin(
130133
break
131134
}
132135
} else {
133-
// Fallback: check for prefix match
134-
for (const [chunkFileName] of chunkCSSMap) {
135-
if (chunkFileName.startsWith(`${cssBaseName}-`)) {
136-
chunkCSSMap.get(chunkFileName)?.push(cssFileName)
137-
break
136+
// Fallback: check for prefix match by examining the base name index
137+
let found = false
138+
for (const [baseName, chunkFileNames] of chunkNameIndex) {
139+
// Check if CSS file name could be a variant (e.g., with hash)
140+
if (
141+
baseName.startsWith(cssBaseName) ||
142+
cssBaseName.startsWith(baseName)
143+
) {
144+
for (const chunkFileName of chunkFileNames) {
145+
if (chunkFileName.startsWith(`${cssBaseName}-`)) {
146+
chunkCSSMap.get(chunkFileName)?.push(cssFileName)
147+
found = true
148+
break
149+
}
150+
}
151+
if (found) break
138152
}
139153
}
140154
}

src/utils/general.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ export function slash(string: string): string {
5252
export const noop = <T>(v: T): T => v
5353

5454
// Cache picomatch matchers to avoid recompilation
55+
// Limit cache size to prevent memory leaks in long-running processes
5556
const picomatchers = new Map<string, ReturnType<typeof picomatch>>()
57+
const MAX_PICOMATCH_CACHE_SIZE = 500
5658

5759
export function matchPattern(
5860
id: string,
@@ -69,6 +71,10 @@ export function matchPattern(
6971
let matcher = picomatchers.get(pattern)
7072
if (!matcher) {
7173
matcher = picomatch(pattern)
74+
// Implement simple cache eviction: clear cache when it gets too large
75+
if (picomatchers.size >= MAX_PICOMATCH_CACHE_SIZE) {
76+
picomatchers.clear()
77+
}
7278
picomatchers.set(pattern, matcher)
7379
}
7480
return matcher(id)

0 commit comments

Comments
 (0)