Skip to content
Merged
14 changes: 14 additions & 0 deletions docs/guide/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,20 @@ Only the globs that are relative paths are interpreted as relative to the resolv

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

#### Case Sensitive Matching

By default, glob pattern matching is case-sensitive. You can use the `caseSensitive` option to change this behavior:

```ts twoslash
import 'vite/client'
// ---cut---
const modules = import.meta.glob('./dir/module*.js', {
caseSensitive: false,
})
```

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`).

### Glob Import Caveats

Note that:
Expand Down
2 changes: 2 additions & 0 deletions packages/vite/src/node/plugins/importMetaGlob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ const knownOptions = {
exhaustive: ['boolean'],
query: ['object', 'string'],
base: ['string'],
caseSensitive: ['boolean'],
}

const forceDefaultAs = ['raw', 'url']
Expand Down Expand Up @@ -461,6 +462,7 @@ export async function transformGlobImport(
cwd,
dot: !!options.exhaustive,
expandDirectories: false,
caseSensitiveMatch: options.caseSensitive ?? true,
ignore: options.exhaustive ? [] : ['**/node_modules/**'],
extglob: false,
})
Expand Down
4 changes: 4 additions & 0 deletions packages/vite/types/importGlob.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export interface ImportGlobOptions<
* Base path to resolve relative paths.
*/
base?: string
/**
* Whether the glob pattern matching should be case-sensitive. Defaults to `true`.
*/
caseSensitive?: boolean
}

export type ImportGlobOptionsWithoutAs<
Expand Down
16 changes: 16 additions & 0 deletions playground/glob-import/__tests__/glob-import.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,22 @@ test('import.meta.glob and dynamic import vars transformations should be visible
)
})

test('caseSensitiveMatch option', async () => {
await expect
.poll(async () =>
JSON.parse(await page.textContent('.case-sensitive-true')),
)
.toStrictEqual(['./case-sensitive-dir/data-test.js'])

await expect
.poll(async () =>
JSON.parse(await page.textContent('.case-sensitive-false')),
)
.toStrictEqual([
'./case-sensitive-dir/DATA-other.js',
'./case-sensitive-dir/data-test.js',
])
})
test('absolute base with files outside of root', async () => {
await expect
.poll(async () =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'DATA-OTHER'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'data-test'
34 changes: 34 additions & 0 deletions playground/glob-import/root/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,40 @@ <h2>Transform visibility</h2>
JSON.stringify(result)
</script>

<h2>Case Sensitive Match (default: true)</h2>
<pre class="case-sensitive-true"></pre>

<h2>Case Sensitive Match (false)</h2>
<pre class="case-sensitive-false"></pre>

<script type="module">
const caseSensitiveModules = import.meta.glob(
'./case-sensitive-dir/data-*.js',
{
eager: true,
import: 'default',
},
)
document.querySelector('.case-sensitive-true').textContent = JSON.stringify(
Object.keys(caseSensitiveModules).sort(),
null,
2,
)

const caseInsensitiveModules = import.meta.glob(
'./case-sensitive-dir/data-*.js',
{
eager: true,
import: 'default',
caseSensitive: false,
},
)
document.querySelector('.case-sensitive-false').textContent = JSON.stringify(
Object.keys(caseInsensitiveModules).sort(),
null,
2,
)
</script>
<h2>Absolute base outside root</h2>
<pre class="absolute-base-outside-root"></pre>

Expand Down
Loading