Skip to content

Commit 2ad6737

Browse files
authored
feat: import.meta.glob support caseSensitive option (#21707)
1 parent 8a13d63 commit 2ad6737

7 files changed

Lines changed: 72 additions & 0 deletions

File tree

‎docs/guide/features.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,20 @@ Only the globs that are relative paths are interpreted as relative to the resolv
620620

621621
All the resulting module keys are modified to be relative to the base if provided.
622622

623+
#### Case Sensitive Matching
624+
625+
By default, glob pattern matching is case-sensitive. You can use the `caseSensitive` option to change this behavior:
626+
627+
```ts twoslash
628+
import 'vite/client'
629+
// ---cut---
630+
const modules = import.meta.glob('./dir/module*.js', {
631+
caseSensitive: false,
632+
})
633+
```
634+
635+
With `caseSensitive: false`, the glob will match files regardless of case (e.g., `Module.js`, `module.js`, `MODULE.js` will all be matched by `module*.js`).
636+
623637
### Glob Import Caveats
624638

625639
Note that:

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ const knownOptions = {
137137
exhaustive: ['boolean'],
138138
query: ['object', 'string'],
139139
base: ['string'],
140+
caseSensitive: ['boolean'],
140141
}
141142

142143
const forceDefaultAs = ['raw', 'url']
@@ -461,6 +462,7 @@ export async function transformGlobImport(
461462
cwd,
462463
dot: !!options.exhaustive,
463464
expandDirectories: false,
465+
caseSensitiveMatch: options.caseSensitive ?? true,
464466
ignore: options.exhaustive ? [] : ['**/node_modules/**'],
465467
extglob: false,
466468
})

‎packages/vite/types/importGlob.d.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ export interface ImportGlobOptions<
3737
* Base path to resolve relative paths.
3838
*/
3939
base?: string
40+
/**
41+
* Whether the glob pattern matching should be case-sensitive. Defaults to `true`.
42+
*/
43+
caseSensitive?: boolean
4044
}
4145

4246
export type ImportGlobOptionsWithoutAs<

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,22 @@ test('import.meta.glob and dynamic import vars transformations should be visible
368368
)
369369
})
370370

371+
test('caseSensitiveMatch option', async () => {
372+
await expect
373+
.poll(async () =>
374+
JSON.parse(await page.textContent('.case-sensitive-true')),
375+
)
376+
.toStrictEqual(['./case-sensitive-dir/data-test.js'])
377+
378+
await expect
379+
.poll(async () =>
380+
JSON.parse(await page.textContent('.case-sensitive-false')),
381+
)
382+
.toStrictEqual([
383+
'./case-sensitive-dir/DATA-other.js',
384+
'./case-sensitive-dir/data-test.js',
385+
])
386+
})
371387
test('absolute base with files outside of root', async () => {
372388
await expect
373389
.poll(async () =>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'DATA-OTHER'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'data-test'

‎playground/glob-import/root/index.html‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,40 @@ <h2>Transform visibility</h2>
314314
JSON.stringify(result)
315315
</script>
316316

317+
<h2>Case Sensitive Match (default: true)</h2>
318+
<pre class="case-sensitive-true"></pre>
319+
320+
<h2>Case Sensitive Match (false)</h2>
321+
<pre class="case-sensitive-false"></pre>
322+
323+
<script type="module">
324+
const caseSensitiveModules = import.meta.glob(
325+
'./case-sensitive-dir/data-*.js',
326+
{
327+
eager: true,
328+
import: 'default',
329+
},
330+
)
331+
document.querySelector('.case-sensitive-true').textContent = JSON.stringify(
332+
Object.keys(caseSensitiveModules).sort(),
333+
null,
334+
2,
335+
)
336+
337+
const caseInsensitiveModules = import.meta.glob(
338+
'./case-sensitive-dir/data-*.js',
339+
{
340+
eager: true,
341+
import: 'default',
342+
caseSensitive: false,
343+
},
344+
)
345+
document.querySelector('.case-sensitive-false').textContent = JSON.stringify(
346+
Object.keys(caseInsensitiveModules).sort(),
347+
null,
348+
2,
349+
)
350+
</script>
317351
<h2>Absolute base outside root</h2>
318352
<pre class="absolute-base-outside-root"></pre>
319353

0 commit comments

Comments
 (0)