fix(glob): trigger HMR for glob in a package (#14117)

This commit is contained in:
翠 / green 2023-08-16 23:48:43 +09:00 committed by GitHub
parent 72d5d4d833
commit 86cbf6977d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 58 additions and 3 deletions

View File

@ -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)

View File

@ -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 () => {

View File

@ -0,0 +1,4 @@
export const g = import.meta.glob('/pkg-pages/*.js')
document.querySelector('.in-package').textContent = JSON.stringify(
Object.keys(g).sort(),
)

View File

@ -0,0 +1,5 @@
{
"name": "@vitejs/test-import-meta-glob-pkg",
"type": "module",
"main": "./index.js"
}

View File

@ -23,6 +23,8 @@
<pre class="escape-alias"></pre>
<h2>Sub imports</h2>
<pre class="sub-imports"></pre>
<h2>In package</h2>
<pre class="in-package"></pre>
<script type="module" src="./dir/index.js"></script>
<script type="module">
@ -151,6 +153,10 @@
.join(' ')
</script>
<script type="module">
import '@vitejs/test-import-meta-glob-pkg'
</script>
<script type="module">
console.log('Ran scripts')
</script>

View File

@ -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"
}
}

View File

@ -0,0 +1 @@
// empty

View File

@ -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'