sveltejs / svelte

Cybernetically enhanced web apps
https://svelte.dev
MIT License
76.8k stars 3.98k forks source link

fix: use acceptExports to support partial hmr #11453

Closed dominikg closed 1 week ago

dominikg commented 2 weeks ago

improvement for hmr with vite-plugin-svelte. This requires a new version of vps to work so both

Svelte 5 rewrite

Please note that the Svelte codebase is currently being rewritten for Svelte 5. Changes should target Svelte 5, which lives on the default branch (main).

If your PR concerns Svelte 4 (including updates to svelte.dev.docs), please ensure the base branch is svelte-4 and not main.

Before submitting the PR, please make sure you do the following

Tests and linting

changeset-bot[bot] commented 2 weeks ago

🦋 Changeset detected

Latest commit: b612177ccc8abc0e5d093ca1b5a771d0c891a964

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package | Name | Type | | ------ | ----- | | svelte | Patch |

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

dummdidumm commented 2 weeks ago

I remember rixo saying that it's possible to check for that and fall back to the regular accepts if the environment doesn't support acceptExports? That way we can potentially support more than vite hmr

dominikg commented 2 weeks ago

I remember rixo saying that it's possible to check for that and fall back to the regular accepts if the environment doesn't support acceptExports? That way we can potentially support more than vite hmr

added a conditional, new output looks like this

if (import.meta.hot) {
    const s = $.source(App);

    App = $.hmr(s);

    if (import.meta.hot.acceptExports) import.meta.hot.acceptExports(["default"], (module) => {
        $.set(s, module.default);
    }); else import.meta.hot.accept((module) => {
        $.set(s, module.default);
    });
}
dominikg commented 1 week ago

vite-plugin-svelte 4 is ready for its first prerelease: https://github.com/sveltejs/vite-plugin-svelte/pull/893

would be good if svelte 5 had this PR and https://github.com/sveltejs/svelte/pull/11456 landing soon so we can raise the minimum peer dep and users can enjoy inspector and hmr :rocket: