-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Description
Describe the bug
Suppose you have a main.js
script using import.meta.glob('/src/pages/*.js')
.
├ src
│ ├ pages
│ │ ├ a.js
│ │ └ b.js
│ └ main.js
...
main.js
is an SSR-evaluated module. When the dev server is running and you add a c.js
file, main.js
is not updated with the new import. Similarly, deleting a.js
does not update the imports. Only updating index.js
directly seems to re-transform the file.
It is notable that import.meta.glob
is already able to respond to file/directory changes fine when used outside an SSR context, which is why I believe this is a bug and not a feature request. In addition, there is an explicit test for this exact scenario of adding/removing files against import.meta.glob
for non-SSR:
vite/packages/playground/glob-import/__tests__/glob-import.spec.ts
Lines 54 to 67 in 2358dfc
if (!isBuild) { | |
test('hmr for adding/removing files', async () => { | |
addFile('dir/a.js', '') | |
await untilUpdated( | |
() => page.textContent('.result'), | |
JSON.stringify( | |
{ | |
'/dir/a.js': {}, | |
...allResult | |
}, | |
null, | |
2 | |
) | |
) |
Which indicates this is indeed supposed to be an existing feature.
A workaround for this is to re-save or touch
your SSR file after every update to the directory.
Reproduction
https://github.com/GrygrFlzr/vite-ssr-glob
git clone https://github.com/GrygrFlzr/vite-ssr-glob.git
cd vite-ssr-glob
npm install
npm run dev
- Load http://localhost:3000, which should currently show:
{ "/src/data/a.json": { "name": "a" }, "/src/data/b.json": { "name": "bc" } }
- Update
src/data/b.json
to the contents{}
. This will trigger HMR.
Demonstrates correct update on file change. - Add a new file
src/data/c.json
with the contents{}
. - Refresh http://localhost:3000 and observe lack of update.
Demonstrates lack of update on file addition. - Remove
src/data/a.json
. - Refresh http://localhost:3000 and observe lack of update.
Demonstrates lack of update on file removal. - Re-save
src/app.js
without changing its contents. This will invalidate the file and trigger HMR, correctly showing the current state of thesrc/data
directory.
System Info
System:
OS: Windows 10 10.0.19042
CPU: (16) x64 AMD Ryzen 7 3700X 8-Core Processor
Memory: 11.52 GB / 31.95 GB
Binaries:
Node: 14.16.0 - C:\Program Files\nodejs\node.EXE
Yarn: 1.22.10 - ~\AppData\Roaming\npm\yarn.CMD
npm: 7.6.1 - C:\Program Files\nodejs\npm.CMD
Browsers:
Chrome: 89.0.4389.90
Edge: Spartan (44.19041.423.0), Chromium (89.0.774.54)
Internet Explorer: 11.0.19041.1
npmPackages:
vite: ^2.1.3 => 2.1.3
Used package manager: npm