diff --git a/packages/vite/src/node/plugins/importMetaGlob.ts b/packages/vite/src/node/plugins/importMetaGlob.ts index 0daf6b04358b30..60172e0de3a143 100644 --- a/packages/vite/src/node/plugins/importMetaGlob.ts +++ b/packages/vite/src/node/plugins/importMetaGlob.ts @@ -58,8 +58,10 @@ export function getAffectedGlobModules( (!affirmed.length || affirmed.some((glob) => isMatch(file, glob))) && (!negated.length || negated.every((glob) => isMatch(file, glob))), ) - ) - modules.push(...(server.moduleGraph.getModulesByFile(id) || [])) + ) { + const mod = server.moduleGraph.getModuleById(id) + if (mod) modules.push(mod) + } } modules.forEach((i) => { if (i?.file) server.moduleGraph.onFileChange(i.file) diff --git a/playground/glob-import/__tests__/glob-import.spec.ts b/playground/glob-import/__tests__/glob-import.spec.ts index 931edfd99138de..8e73892219e192 100644 --- a/playground/glob-import/__tests__/glob-import.spec.ts +++ b/playground/glob-import/__tests__/glob-import.spec.ts @@ -11,6 +11,7 @@ import { page, removeFile, untilBrowserLogAfter, + untilUpdated, viteTestUrl, withRetry, } from '~utils' @@ -131,6 +132,12 @@ test('unassigned import processes', async () => { ) }) +test('import glob in package', async () => { + expect(await page.textContent('.in-package')).toBe( + JSON.stringify(['/pkg-pages/foo.js']), + ) +}) + if (!isBuild) { test('hmr for adding/removing files', async () => { const resultElement = page.locator('.result') @@ -190,6 +197,22 @@ if (!isBuild) { response = await request.catch(() => ({ status: () => -1 })) expect(response.status()).toBe(-1) }) + + test('hmr for adding/removing files in package', async () => { + const resultElement = page.locator('.in-package') + + addFile('pkg-pages/bar.js', '// empty') + await untilUpdated( + () => resultElement.textContent(), + JSON.stringify(['/pkg-pages/foo.js', '/pkg-pages/bar.js'].sort()), + ) + + removeFile('pkg-pages/bar.js') + await untilUpdated( + () => resultElement.textContent(), + JSON.stringify(['/pkg-pages/foo.js']), + ) + }) } test('tree-shake eager css', async () => { diff --git a/playground/glob-import/import-meta-glob-pkg/index.js b/playground/glob-import/import-meta-glob-pkg/index.js new file mode 100644 index 00000000000000..44705cf18f9f22 --- /dev/null +++ b/playground/glob-import/import-meta-glob-pkg/index.js @@ -0,0 +1,4 @@ +export const g = import.meta.glob('/pkg-pages/*.js') +document.querySelector('.in-package').textContent = JSON.stringify( + Object.keys(g).sort(), +) diff --git a/playground/glob-import/import-meta-glob-pkg/package.json b/playground/glob-import/import-meta-glob-pkg/package.json new file mode 100644 index 00000000000000..7138de851543cf --- /dev/null +++ b/playground/glob-import/import-meta-glob-pkg/package.json @@ -0,0 +1,5 @@ +{ + "name": "@vitejs/test-import-meta-glob-pkg", + "type": "module", + "main": "./index.js" +} diff --git a/playground/glob-import/index.html b/playground/glob-import/index.html index b726965ff62067..3b72430668e14d 100644 --- a/playground/glob-import/index.html +++ b/playground/glob-import/index.html @@ -23,6 +23,8 @@

Escape alias glob


 

Sub imports


+

In package

+

 
 
 
 
+
+
 
diff --git a/playground/glob-import/package.json b/playground/glob-import/package.json
index 7d9104ab431c28..d71d01109270f1 100644
--- a/playground/glob-import/package.json
+++ b/playground/glob-import/package.json
@@ -11,5 +11,8 @@
     "build": "vite build",
     "debug": "node --inspect-brk ../../packages/vite/bin/vite",
     "preview": "vite preview"
+  },
+  "dependencies": {
+    "@vitejs/test-import-meta-glob-pkg": "file:./import-meta-glob-pkg"
   }
 }
diff --git a/playground/glob-import/pkg-pages/foo.js b/playground/glob-import/pkg-pages/foo.js
new file mode 100644
index 00000000000000..8b1a393741c96c
--- /dev/null
+++ b/playground/glob-import/pkg-pages/foo.js
@@ -0,0 +1 @@
+// empty
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index b2abd4ecceef5b..e3ad26d4ccdb3e 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -670,7 +670,13 @@ importers:
 
   playground/fs-serve: {}
 
-  playground/glob-import: {}
+  playground/glob-import:
+    dependencies:
+      '@vitejs/test-import-meta-glob-pkg':
+        specifier: file:./import-meta-glob-pkg
+        version: file:playground/glob-import/import-meta-glob-pkg
+
+  playground/glob-import/import-meta-glob-pkg: {}
 
   playground/hmr: {}
 
@@ -10838,6 +10844,11 @@ packages:
       vue: 3.3.4
     dev: false
 
+  file:playground/glob-import/import-meta-glob-pkg:
+    resolution: {directory: playground/glob-import/import-meta-glob-pkg, type: directory}
+    name: '@vitejs/test-import-meta-glob-pkg'
+    dev: false
+
   file:playground/import-assertion/import-assertion-dep:
     resolution: {directory: playground/import-assertion/import-assertion-dep, type: directory}
     name: '@vitejs/test-import-assertion-dep'