You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Vite does pre-bundling for dependency but it just caches JS file and leaves the .vue file to the original file at node_modules/the-plugin-package-name. It will lead to bugs when the plugin uses the Singleton pattern to manage states.
Because the Singleton file might be cached on the node_modules/.vite/deps file, but when the .vue file imports it, the .vue file would use the instance from node_module/the-plugin-package-name. And two instances of the Singleton will be created. One lives in node_modules/.vite/deps and the others lives in node_modules/the-plugin-package-name.
// The .vue file is point to original file// node_modules/@meforma/vue-toaster/src/index.jsimportToaster2from"/Users/admin/Work/test-projects/vite-demo-vue-plugin/node_modules/@meforma/vue-toaster/src/Toaster.vue";// node_modules/@meforma/vue-toaster/src/api.jsimportToasterfrom"/Users/admin/Work/test-projects/vite-demo-vue-plugin/node_modules/@meforma/vue-toaster/src/Toaster.vue";// The js file is cached here so Singleton pattern will breaks// node_modules/@meforma/vue-toaster/src/helpers/event-bus.jsvarEvent=class{constructor(){this.queue={};}
...
}
When the Toaster.vue uses the eventBus, it will use the instance in node_modules/@meforma/vue-toaster/. But when we call toast.clear() from our Vue app the clear method will use the eventBus instance in node_modules/.vite/deps. They are two different instances so the clear method will not work
Workaround
The problem will be solved by excluding the package from pre-bundling by optimizeDeps.entries
However, it's a not long-term fix and the bug will consume hours of debugging before leading to this solution. This can happen to any Vue/React plugin where there some files can't be cached by Vite.
I think Vite should explicitly document this to reduce the effort of developers when facing this issue. Or Vite should automatically ignore from pre-bundling these packages that has some files that can't be cached
Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/core instead.
Describe the bug
Problem
Vite does pre-bundling for dependency but it just caches JS file and leaves the
.vue
file to the original file atnode_modules/the-plugin-package-name
. It will lead to bugs when the plugin uses the Singleton pattern to manage states.Because the Singleton file might be cached on the
node_modules/.vite/deps
file, but when the.vue
file imports it, the.vue
file would use the instance fromnode_module/the-plugin-package-name
. And two instances of the Singleton will be created. One lives innode_modules/.vite/deps
and the others lives innode_modules/the-plugin-package-name
.Let's consider this example. I use @meforma/vue-toaster plugin. The plugin have a Toaster.vue file and a Singleton event-bus.js to manage show/hide state of the toast. Vite cache event bus on node_modules/.vite/deps but leave the Toaster.vue to the original file
When the
Toaster.vue
uses theeventBus
, it will use the instance innode_modules/@meforma/vue-toaster/
. But when we calltoast.clear()
from our Vue app theclear
method will use theeventBus
instance innode_modules/.vite/deps
. They are two different instances so theclear
method will not workWorkaround
The problem will be solved by excluding the package from pre-bundling by optimizeDeps.entries
However, it's a not long-term fix and the bug will consume hours of debugging before leading to this solution. This can happen to any Vue/React plugin where there some files can't be cached by Vite.
I think Vite should explicitly document this to reduce the effort of developers when facing this issue. Or Vite should automatically ignore from pre-bundling these packages that has some files that can't be cached
Reproduction
https://github.com/duannx/vite-demo-vue-plugin
System Info
Used Package Manager
yarn
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: