From 86cbf6977d259cfbe87b287f42cf20668014aef3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Wed, 16 Aug 2023 23:48:43 +0900 Subject: [PATCH] fix(glob): trigger HMR for glob in a package (#14117) --- .../vite/src/node/plugins/importMetaGlob.ts | 6 +++-- .../glob-import/__tests__/glob-import.spec.ts | 23 +++++++++++++++++++ .../glob-import/import-meta-glob-pkg/index.js | 4 ++++ .../import-meta-glob-pkg/package.json | 5 ++++ playground/glob-import/index.html | 6 +++++ playground/glob-import/package.json | 3 +++ playground/glob-import/pkg-pages/foo.js | 1 + pnpm-lock.yaml | 13 ++++++++++- 8 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 playground/glob-import/import-meta-glob-pkg/index.js create mode 100644 playground/glob-import/import-meta-glob-pkg/package.json create mode 100644 playground/glob-import/pkg-pages/foo.js diff --git a/packages/vite/src/node/plugins/importMetaGlob.ts b/packages/vite/src/node/plugins/importMetaGlob.ts index 0daf6b043..60172e0de 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 5ccf9db5c..239278477 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 000000000..44705cf18 --- /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 000000000..7138de851 --- /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 b726965ff..3b7243066 100644 --- a/playground/glob-import/index.html +++ b/playground/glob-import/index.html @@ -23,6 +23,8 @@

 

Sub imports


+

In package

+

 
 
 
 
+
+
 
diff --git a/playground/glob-import/package.json b/playground/glob-import/package.json
index 7d9104ab4..d71d01109 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 000000000..8b1a39374
--- /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 8ecf2daea..7422db09d 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: {}
 
@@ -10801,6 +10807,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'