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

Vite forces refresh on ANY file change when negative glob paths are used #13374

Closed
7 tasks done
indykoning opened this issue May 30, 2023 · 5 comments · Fixed by #13646
Closed
7 tasks done

Vite forces refresh on ANY file change when negative glob paths are used #13374

indykoning opened this issue May 30, 2023 · 5 comments · Fixed by #13646
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@indykoning
Copy link

indykoning commented May 30, 2023

Describe the bug

After using the following piece of code

const components = {
    ...import.meta.glob(['./components/*.vue', '!./components/*.lazy.vue'], { eager: true, import: 'default' }),
    ...import.meta.glob(['./components/*.lazy.vue'], { eager: false, import: 'default' })
};
for (const path in components) {
    Vue.component(path.split('/').pop().split('.').shift(), components[path])
}

to automatically import vue files and lazy load them if it contains lazy in the file name we noticed Vite would refresh the page when ANY files are created, removed or moved.

In this case the file containing this code would be in /resources/js/ and Vite would respond to changes in /storage/cache/
Removing the line !./components/*.lazy.vue from the code fixes the issue

Reproduction

https://stackblitz.com/edit/vitejs-vite-7jm5s1?file=main.js,components%2Ftest.lazy.vue,shouldntrefreshbecauseofthis&terminal=dev

Steps to reproduce

Add any relative negative glob import to your file (having a positive glob in there as well to really push the point home)
run yarn/npm run dev
add/remove/rename any file in the root folder of your project (where your vite config lives)

Check the terminal to see vite forcing a refresh due to a file change

System Info

System:
    OS: macOS 13.2
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 74.90 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.19.1 - ~/.volta/tools/image/node/16.19.1/bin/node
    Yarn: 1.22.19 - ~/.volta/tools/image/yarn/1.22.19/bin/yarn
    npm: 8.19.3 - ~/.volta/tools/image/node/16.19.1/bin/npm
  Browsers:
    Chrome: 113.0.5672.126
    Firefox: 111.0.1
    Safari: 16.3
  npmPackages:
    @vitejs/plugin-vue2: ^2.2.0 => 2.2.0
    vite: ^4.0.4 => 4.0.4

Used Package Manager

yarn

Logs

No response

Validations

@stackblitz
Copy link

stackblitz bot commented May 30, 2023

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@sun0day
Copy link
Member

sun0day commented Jun 26, 2023

After I tried the reproduction, vite dev server refreshed when adding, removing and renaming files. But it wouldn't refresh when the file content was edited.

@indykoning
Copy link
Author

You're right, i've checked and indeed it refreshes when any file is created, removed or moved. But not when changing the contents of this file.
I've updated my issue description to reflect this.

@sun0day
Copy link
Member

sun0day commented Jun 26, 2023

Not sure whether this is a valid bug. When you use !./components/*.lazy.vue, you are telling vite to watch files in the root except ./components/*.lazy.vue.

@indykoning
Copy link
Author

Yes, if it was solely import.meta.glob('!./components/*.lazy.vue') that you're using this would absolutely be intended behavior, because you want these new files to be imported as well.

However if you combine it with an inclusive glob
import.meta.glob(['./components/*.vue', '!./components/*.lazy.vue']
you would expect it to only import everything matching ./components/*.vue excluding those matching '!./components/*.lazy.vue'
Which end-result wise it does, it correctly seems to include and exclude all files.

But internally it does watch every single file in the root and refreshes like you describe, which is not really expected when using it as described in https://vitejs.dev/guide/features.html#negative-patterns

@github-actions github-actions bot locked and limited conversation to collaborators Aug 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants