Skip to content

Commit

Permalink
fix(glob): trigger HMR for glob in a package (#14117)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Aug 16, 2023
1 parent 72d5d4d commit 86cbf69
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 3 deletions.
6 changes: 4 additions & 2 deletions packages/vite/src/node/plugins/importMetaGlob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions playground/glob-import/__tests__/glob-import.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
page,
removeFile,
untilBrowserLogAfter,
untilUpdated,
viteTestUrl,
withRetry,
} from '~utils'
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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 () => {
Expand Down
4 changes: 4 additions & 0 deletions playground/glob-import/import-meta-glob-pkg/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const g = import.meta.glob('/pkg-pages/*.js')
document.querySelector('.in-package').textContent = JSON.stringify(
Object.keys(g).sort(),
)
5 changes: 5 additions & 0 deletions playground/glob-import/import-meta-glob-pkg/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "@vitejs/test-import-meta-glob-pkg",
"type": "module",
"main": "./index.js"
}
6 changes: 6 additions & 0 deletions playground/glob-import/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ <h2>Escape alias glob</h2>
<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">
Expand Down Expand Up @@ -151,6 +153,10 @@ <h2>Sub imports</h2>
.join(' ')
</script>

<script type="module">
import '@vitejs/test-import-meta-glob-pkg'
</script>

<script type="module">
console.log('Ran scripts')
</script>
3 changes: 3 additions & 0 deletions playground/glob-import/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
1 change: 1 addition & 0 deletions playground/glob-import/pkg-pages/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// empty
13 changes: 12 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 86cbf69

Please sign in to comment.