-
-
Notifications
You must be signed in to change notification settings - Fork 109
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
Same module imported and intialized twice #135
Comments
Same underlying issue as #124, the workaround now is to add that library to |
Thank you. When adding the library I'm using to I was wondering if excluding all possible dependencies could prevent weird behavior from ever happening (it's better to have longer build time than a buggy output), so I did this:
And it works most of the time, except when using After some testing I found out that replacing |
Excluding all isn't recommended since Vite needs to pre-bundle CJS-only packages to ESM (Explanation). You might be lucky with your setup of having all its dependencies obediently exports ESM code. Re |
You have to turn of source maps to use the debugger on this (otherwise they both get mapped to http://localhost:3000/src/main.js http://localhost:3000/src/main.js So this bug resides within the single optimized module. The optimized module should not be importing things from the non-optimized module. |
Should we close this in favor of #125? |
#125 suggests adding svelte libraries to the exclude rule, but it shares the same underline problem, so it's fine by me to close this issue in favor of it |
automatic handling of svelte dependencies optimizeDeps has been released. Please check if that fixes your issue https://github.com/sveltejs/vite-plugin-svelte/blob/main/packages/vite-plugin-svelte/CHANGELOG.md#100-next19 |
Hi @dominikg , yes it does! Awesome! |
When debugging Forgo using `npm link`, Vite's pre-bundling optimization executes the `forgo` module twice ([Vite bug](vitejs/vite#3910), [Svelte bug with workarounds](sveltejs/vite-plugin-svelte#135 (comment))). It doesn't happen when using the package from npm. This results in different code paths holding references to two different `Fragment` symbols, causing renders to fail because the two symbols aren't comparable using `===`. Using `Symbol.for()` makes all symbols named `FORGO_FRAGMENT` comparable, no matter where they come from. Alternatively, there's a workaround to have Vite disable pre-bundling optimizations for `forgo`, but this patch makes that unnecessary.
When debugging Forgo using `npm link`, Vite's pre-bundling optimization executes the `forgo` module twice ([Vite bug](vitejs/vite#3910), [Svelte bug with workarounds](sveltejs/vite-plugin-svelte#135 (comment))). It doesn't happen when using the package from npm. This results in different code paths holding references to two different `Fragment` symbols, causing renders to fail because the two symbols aren't comparable using `===`. Using `Symbol.for()` makes all symbols named `FORGO_FRAGMENT` comparable, no matter where they come from. Alternatively, there's a workaround to have Vite disable pre-bundling optimizations for `forgo`, but this patch makes that unnecessary.
Describe the bug
When using a library that contains Svelte components a module can get initialized twice. This happens only when running Vite in development mode.
The bug occurs when you have:
Running Vite in development mode results in module.js getting executed/initialized twice, thus causing reference mismatches and all sort of strange behavior.
On the other hand, when running Vite for bundling for production the module resolution process correctly detects that module.js has already been imported and does not replicate it in the output bundle.
Reproduction
A reproduction of this bug can be found at this repository:
https://github.com/cdellacqua/svelte-vite-double-import-demo
The repository contains an
app
directory with a minimal example based on the template obtained by runningnpm init vite@latest test-vite-svelte -- --template svelte
.It also contains
svelte-vite-double-import-lib
which is a minimal library that can cause this behavior.Steps to reproduce:
app
directorynpm install
npm run dev
Logs
System Info
System: OS: Linux 5.11 Pop!_OS 21.04 CPU: (16) x64 AMD Ryzen 7 2700X Eight-Core Processor Memory: 8.07 GB / 15.64 GB Container: Yes Shell: 3.2.2 - /usr/bin/fish Binaries: Node: 14.16.1 - /usr/bin/node Yarn: 1.22.10 - /usr/bin/yarn npm: 7.10.0 - /usr/bin/npm Browsers: Chrome: 92.0.4515.107 Firefox: 90.0
Severity
blocking all usage of vite-plugin-svelte
The text was updated successfully, but these errors were encountered: