-
-
Notifications
You must be signed in to change notification settings - Fork 110
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
HMR breaks context="module"
exports
#134
Comments
broken code from repo aboveApp.svelte <script lang="ts">
import Counter, { visible } from './Counter.svelte'
</script>
<main>
<Counter />
<button on:click={() => visible.set(true)}>Show</button>
<button on:click={() => visible.set(false)}>Hide</button>
</main> Counter.svelte <script lang="ts" context="module">
import { writable } from 'svelte/store'
let text = writable('')
export const visible = writable(false)
</script>
<h3>Update me</h3>
<p>Visible: {$visible}</p> what happensWhen you edit The instance of App.svelte on the other hand remains unchanged and that imported the original instance of the visible store. So now you have 2 instances of the same store and of course updates to the one App.svelte has won't propagate to the new Counter. how to avoid itMove the store to it's own module to avoid recreating it on update store.ts import { writable } from 'svelte/store'
export const visible = writable(false) App.svelte <script lang="ts">
import Counter from './Counter.svelte'
import {visible} from "./store";
</script>
<main>
<Counter />
<button on:click={() => visible.set(true)}>Show</button>
<button on:click={() => visible.set(false)}>Hide</button>
</main> Counter.svelte <script lang="ts">
import {visible} from "./store";
</script>
<h3>Update bla</h3>
<p>Visible: {$visible}</p> Now the hmr update of Counter no longer creates a new instance of the visible store and you can enjoy hmr. cc @rixo This is a limitation of the current implementation and trying to also invalidate all Components that import from an hmr updated Components context=module could cause a big cascade effect. |
Could If this is a lot of work to fix, a great temporary fix could be to fully reload if a component has exports in |
unfortunately that would be pretty devastating for sveltekit which exports flags from context=module : https://kit.svelte.dev/docs#ssr-and-javascript-prerender I'm chatting about it with @rixo and one approach would indeed be to split the current Counter.svelte into virtual Counter.module and Counter.component modules behind a Counter.svelte facade, with updates to Counter.module propagating to all importers via hmr bubbling. But thats a pretty bold+risky move as the implementation involves creating 2 pretty jarring sourcemaps and at the end of the day results in more requests during dev. Personal opinion: So i think the best we can do for now is collect more Feedback/Ideas and document the limitation in the same way we documented the behavior around local state preservation. |
I don't quite see why it's a better implementation, could you elaborate on it? In my case, I have a I considered both approaches and went for |
I don't understand why it affects SvelteKit. What if it does something equivalent, like an HMR update |
If vite-plugin-svelte triggered a full-reload for every hmr update of a svelte component with exports from The approach with the facade is worth investigating though as it could also lead to improvements for the case where you are only updating the instance script. I'm going to add a feature request for it to draft a solution and discuss possible issues with it |
Ah, I see. I assume checking for imports of |
Hi guys, was this ever fixed or any good resolution? |
@Sayyiditow Hi! For now, the best workaround you can have is probably to split The fix is in progress. Vite 3 has added the experimental option we need to address this on our side. I'm reserving some time to add it to svelte-hmr in the coming days, and hopefully it can make its way into the Vite plugin soon after.
|
@rixo Thanks for the update. We have some work around for now. |
should be fixed in 1.0.6 |
Tried 1.0.6 but still getting the same error when using svelte-materialify components such as that have context=module. Anyone who can report that the fix works? |
It seems to work for me |
@Sayyiditow if you still face this issue with @sveltejs/vite-plugin-svelte v 1.0.6, and svelte-hmr 0.15, please provide a link to a repo that reproduces it and instructions how excactly to trigger. |
Hey @dominikg , I just tried upgrading to Vite 3 with a svelte/routify project and am using:
and getting these errors:
These exports are in a Does this seem like the bug in this ticket, or something else? |
the fix for this (partial HMR) might have uncovered this. Can't tell if this is a bug in v-p-s / svelte-hmr or a problem in your project. Please file a new issue and provide a minimal reproduction. Ideally without routify in a clean vite+svelte project. |
Describe the bug
If you've exported a function using
<script context="module">
, HMR updates from that component causes other the exported function to no longer run from other components where it's imported.Reproduction
https://github.com/probablykasper/svelte-vite-context-module-exports-bug
npx degit probablykasper/svelte-vite-context-module-exports-bug svelte-vite-context-module-exports-bug
cd svelte-vite-context-module-exports-bug && npm i
npm run dev
Show
andHide
buttons workCounter.svelte
, for example by changing the<h3>
tagShow
andHide
buttons are brokenLogs
No response
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: