diff --git a/docs/guide/features.md b/docs/guide/features.md
index 7f6dfaa2df82b7..97bc0b46dfc700 100644
--- a/docs/guide/features.md
+++ b/docs/guide/features.md
@@ -248,6 +248,21 @@ const modules = {
}
```
+You can also pass the second argument to ignore some files:
+
+```js
+const modules = import.meta.glob('./dir/*.js', '**/b*.*')
+```
+
+That will ignore all files whose name starts with `b`, the file `./dir/bar.js` will be ignored, the above will be transformed into the following:
+
+```js
+// code produced by vite
+const modules = {
+ './dir/foo.js': () => import('./dir/foo.js')
+}
+```
+
Note that:
- This is a Vite-only feature and is not a web or ES standard.
diff --git a/packages/playground/glob-import/__tests__/glob-import.spec.ts b/packages/playground/glob-import/__tests__/glob-import.spec.ts
index 15ffb4c7d97ede..49a69efbe5a744 100644
--- a/packages/playground/glob-import/__tests__/glob-import.spec.ts
+++ b/packages/playground/glob-import/__tests__/glob-import.spec.ts
@@ -3,7 +3,8 @@ import {
editFile,
isBuild,
removeFile,
- untilUpdated
+ untilUpdated,
+ sortObjectDeep
} from '../../testUtils'
const filteredResult = {
@@ -14,21 +15,17 @@ const filteredResult = {
// json exports key order is altered during build, but it doesn't matter in
// terms of behavior since module exports are not ordered anyway
-const json = isBuild
- ? {
- msg: 'baz',
- default: {
- msg: 'baz'
- }
- }
- : {
- default: {
- msg: 'baz'
- },
- msg: 'baz'
- }
+const json = {
+ msg: 'baz',
+ default: {
+ msg: 'baz'
+ }
+}
-const allResult = {
+const allResult = sortObjectDeep({
+ '/dir/_ignored.js': {
+ msg: 'ignored'
+ },
// JSON file should be properly transformed
'/dir/baz.json': json,
'/dir/foo.js': {
@@ -43,7 +40,7 @@ const allResult = {
},
msg: 'bar'
}
-}
+})
test('should work', async () => {
expect(await page.textContent('.result')).toBe(
@@ -53,36 +50,72 @@ test('should work', async () => {
if (!isBuild) {
test('hmr for adding/removing files', async () => {
- addFile('dir/a.js', '')
+ addFile('dir/+a.js', '')
+ await untilUpdated(
+ () => page.textContent('.result'),
+ JSON.stringify(
+ sortObjectDeep({
+ '/dir/+a.js': {},
+ ...allResult
+ }),
+ null,
+ 2
+ )
+ )
+
+ // edit the added file
+ editFile('dir/+a.js', () => 'export const msg ="a"')
+ await untilUpdated(
+ () => page.textContent('.result'),
+ JSON.stringify(
+ sortObjectDeep({
+ '/dir/+a.js': {
+ msg: 'a'
+ },
+ ...allResult
+ }),
+ null,
+ 2
+ )
+ )
+
+ removeFile('dir/+a.js')
+ await untilUpdated(
+ () => page.textContent('.result'),
+ JSON.stringify(allResult, null, 2)
+ )
+ })
+ test('hmr for adding/removing files with ignore option', async () => {
+ addFile('dir/_a.js', '')
await untilUpdated(
() => page.textContent('.result'),
JSON.stringify(
- {
- '/dir/a.js': {},
+ sortObjectDeep({
+ '/dir/_a.js': {},
...allResult
- },
+ }),
null,
2
)
)
// edit the added file
- editFile('dir/a.js', () => 'export const msg ="a"')
+ editFile('dir/_a.js', () => 'export const msg ="a"')
await untilUpdated(
() => page.textContent('.result'),
JSON.stringify(
- {
- '/dir/a.js': {
+ sortObjectDeep({
+ '/dir/_a.js': {
msg: 'a'
},
...allResult
- },
+ }),
null,
2
)
)
- removeFile('dir/a.js')
+ removeFile('dir/_a.js')
await untilUpdated(
() => page.textContent('.result'),
JSON.stringify(allResult, null, 2)
diff --git a/packages/playground/glob-import/dir/_ignored.js b/packages/playground/glob-import/dir/_ignored.js
new file mode 100644
index 00000000000000..26924e0d8414c6
--- /dev/null
+++ b/packages/playground/glob-import/dir/_ignored.js
@@ -0,0 +1 @@
+export const msg = 'ignored'
diff --git a/packages/playground/glob-import/dir/index.js b/packages/playground/glob-import/dir/index.js
index 7adc59a0b14f5e..bba7a299b166fe 100644
--- a/packages/playground/glob-import/dir/index.js
+++ b/packages/playground/glob-import/dir/index.js
@@ -1,3 +1,3 @@
-const modules = import.meta.globEager('./*.js')
+const modules = import.meta.globEager('./*.js', './_*.js')
export { modules }
diff --git a/packages/playground/glob-import/index.html b/packages/playground/glob-import/index.html
index b38a194e21b4f2..8f56cd18317d16 100644
--- a/packages/playground/glob-import/index.html
+++ b/packages/playground/glob-import/index.html
@@ -2,7 +2,8 @@