Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Comment on lines 60 to +64
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An id (e.g. package/foo/index.js?v=version) is set here and server.moduleGraph.getModulesByFile(id) was returning [].

server._importGlobMap.set(
id,

I guess it was wrong to use server.moduleGraph.getModuleById as the _importGlobMap uses an id instead of file.

Another way to fix this is to remove ?v=version before calling getModulesByFile or setting _importGlobMap.

}
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.