Skip to content

Commit 63e040f

Browse files
authored
fix(glob): fix HMR for array patterns with exclusions (#20872)
1 parent 24a61a3 commit 63e040f

File tree

5 files changed

+78
-14
lines changed

5 files changed

+78
-14
lines changed

packages/vite/src/node/plugins/importMetaGlob.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ export function importGlobPlugin(config: ResolvedConfig): Plugin {
7373
const affirmed: string[] = []
7474
const negated: string[] = []
7575
for (const glob of globs) {
76-
;(glob[0] === '!' ? negated : affirmed).push(glob)
76+
if (glob[0] === '!') {
77+
negated.push(glob.slice(1))
78+
} else {
79+
affirmed.push(glob)
80+
}
7781
}
7882
const affirmedMatcher = picomatch(affirmed)
7983
const negatedMatcher = picomatch(negated)

playground/glob-import/__tests__/glob-import.spec.ts

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,40 @@ if (!isBuild) {
210210
.poll(async () => JSON.parse(await resultElement.textContent()))
211211
.toStrictEqual(['/pkg-pages/foo.js'])
212212
})
213+
214+
test('hmr for adding/removing files with array patterns and exclusions', async () => {
215+
const resultElement = page.locator('.array-result')
216+
await expect
217+
.poll(async () => JSON.parse(await resultElement.textContent()))
218+
.toStrictEqual({
219+
'./array-test-dir/included.js': 'included',
220+
})
221+
222+
addFile('array-test-dir/new-file.js', 'export default "new"')
223+
await expect
224+
.poll(async () => JSON.parse(await resultElement.textContent()))
225+
.toStrictEqual({
226+
'./array-test-dir/included.js': 'included',
227+
'./array-test-dir/new-file.js': 'new',
228+
})
229+
230+
removeFile('array-test-dir/new-file.js')
231+
await expect
232+
.poll(async () => JSON.parse(await resultElement.textContent()))
233+
.toStrictEqual({
234+
'./array-test-dir/included.js': 'included',
235+
})
236+
})
213237
}
214238

239+
test('array pattern with exclusions', async () => {
240+
await expect
241+
.poll(async () => JSON.parse(await page.textContent('.array-result')))
242+
.toStrictEqual({
243+
'./array-test-dir/included.js': 'included',
244+
})
245+
})
246+
215247
test('tree-shake eager css', async () => {
216248
expect(await page.textContent('.no-tree-shake-eager-css-result')).toMatch(
217249
'.no-tree-shake-eager-css',
@@ -237,26 +269,34 @@ test('escapes special chars in globs without mangling user supplied glob suffix'
237269
.filter((f) => f.isDirectory())
238270
.map((f) => `/escape/${f.name}/glob.js`)
239271
.sort()
240-
const foundRelativeNames = (await page.textContent('.escape-relative'))
241-
.split('\n')
242-
.sort()
243-
expect(expectedNames).toEqual(foundRelativeNames)
244-
const foundAliasNames = (await page.textContent('.escape-alias'))
245-
.split('\n')
246-
.sort()
247-
expect(expectedNames).toEqual(foundAliasNames)
272+
await expect
273+
.poll(async () => {
274+
const text = await page.textContent('.escape-relative')
275+
return text.split('\n').sort()
276+
})
277+
.toEqual(expectedNames)
278+
await expect
279+
.poll(async () => {
280+
const text = await page.textContent('.escape-alias')
281+
return text.split('\n').sort()
282+
})
283+
.toEqual(expectedNames)
248284
})
249285

250286
test('subpath imports', async () => {
251-
expect(await page.textContent('.subpath-imports')).toMatch('bar foo')
287+
await expect
288+
.poll(async () => await page.textContent('.subpath-imports'))
289+
.toMatch('bar foo')
252290
})
253291

254292
test('#alias imports', async () => {
255-
expect(await page.textContent('.hash-alias-imports')).toMatch('bar foo')
293+
await expect
294+
.poll(async () => await page.textContent('.hash-alias-imports'))
295+
.toMatch('bar foo')
256296
})
257297

258298
test('import base glob raw', async () => {
259-
expect(await page.textContent('.result-base')).toBe(
260-
JSON.stringify(baseRawResult, null, 2),
261-
)
299+
await expect
300+
.poll(async () => await page.textContent('.result-base'))
301+
.toBe(JSON.stringify(baseRawResult, null, 2))
262302
})
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'excluded'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'included'

playground/glob-import/index.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,21 @@ <h2>Base</h2>
187187
2,
188188
)
189189
</script>
190+
191+
<h2>Array Pattern with Exclusions</h2>
192+
<pre class="array-result"></pre>
193+
194+
<script type="module">
195+
const arrayModules = import.meta.glob(
196+
['./array-test-dir/*.js', '!./array-test-dir/excluded.js'],
197+
{
198+
eager: true,
199+
import: 'default',
200+
},
201+
)
202+
document.querySelector('.array-result').textContent = JSON.stringify(
203+
arrayModules,
204+
null,
205+
2,
206+
)
207+
</script>

0 commit comments

Comments
 (0)