# svelte-hmr HMR commons for Svelte 3. This packages provides shared dependencies for implementing Svelte HMR in any bundler plugins. If you want to _use_ HMR in your Svelte project, what you need is a HMR enabled plugin for your bundler (e.g. Rollup or Webpack). You'll find a list of available tools at the end of this doc. On the other hand, if you are really developing a plugin... Sorry, no docs for now! Drop me a line, I'd be happy to help! ## Features - update Svelte components in place - preservation of component state, including local state (i.e. `let` vars in your components) - inject CSS instead of doing a full replace when only the component's CSS has changed, with compatible HMR APIs (`rollup-plugin-hot`, Nollup, and Snowpack for now) ## Options Those are the HMR options that are implemented by `svelte-hmr` itself, and so should be supported by any plugin listed bellow (especially if they include a link pointing to this section). How to pass those options is specific to each plugins, so refer to their specific docs on this point. #### noReload Type: `bool`
Default: `false` By default, `svelte-hmr` will trigger a full browser reload when it detects an error that will prevent subsequent HMR updates to be applied correctly. Set this to `true` to prevent automatic reloads. Note that Svelte Native does _not_ execute in a browser, and so this option has no effect there. #### noPreserveState **Deprecated: removed and default changed from version 0.12. Use `preserveLocalState` instead.** #### preserveLocalState Type: `bool`
Default: `false` Enable [preservation of local state](#preservation-of-local-state) for all variables in all components. #### noPreserveStateKey Type: `string`
Default: `'@hmr:reset'` (also accepts legacy `'@!hmr'`) Force disable preservation of local state for this component. This flag has priority over all other settings of state preservation. If it is present, all the state of the component will be reset on the next update, regardless of the value of all the other state preservation settings. ```svelte ``` #### preserveAllLocalStateKey Type: `string`
Default: `'@hmr:keep-all'` Force preservation of all local variables of this component. ```svelte ``` #### preserveLocalStateKey Type: `string`
Default: `'@hmr:keep'` Force preservation of a given local variable in this component. ```svelte ``` #### optimistic Type: `bool`
Default: `true` Set this to `false` to consider runtime errors during component init (i.e. when your `

{x}

``` If you replace `let x = 1` by `let x = 10` and save, the previous value of `x` will be preserved. That is, `x` will be 2 and not 10. The restoration of previous state happens _after_ the init code of the component has run, so the value will not be 11 either, despite the `x++` that is still here. If you want this behaviour for all the state of all your components, you can enable it by setting the `preserveLocalState` option to `true`. If you then want to disable it for just one particular file, or just temporarily, you can turn it off by adding a `// @hmr:reset` comment somewhere in your component. On the contrary, if you keep the default `preserveLocalState` to `false`, you can enable preservation of all the local state of a given component by adding the following comment: `// @hmr:keep-all`. You can also preserve only the state of some specific variables, by annotating them with: `// @hmr:keep`. For example: ```svelte ``` ## Svelte HMR tools ### Vite 2 The [official Svelte plugin for Vite][vite-plugin-svelte] has HRM support. ### Webpack The [official loader for Webpack][svelte-loader] now has HMR support. ### Rollup / Nollup - [svelte-template-hot] - [rollup-plugin-svelte-hot] #### HMR support for Rollup Rollup does not natively support HMR. You'll need to use one of the following solutions. The best way to get started is to refer to [svelte-template-hot], that demonstrates usage of both. - [rollup-plugin-hot] - [Nollup][nollup] ### Svelte Native The official [Svelte Native template][svelte-native-template] already includes HMR support. ### Snowpack Official [Snowpack plugin for Svelte][snowpack/plugin-svelte] has HMR support via `svelte-hmr`. Use [create-snowpack-app] with [app-template-svelte] to get started quickly: ```bash npx create-snowpack-app new-dir --template @snowpack/app-template-svelte [--use-yarn | --use-pnpm | --no-install] ``` ### Sapper Sapper can be supported with Webpack's loader. The link bellow is still very much a work in progress (no docs at time of writing), but I'm adding it for future reference. - [sapper-template-hot#webpack](https://github.com/rixo/sapper-template-hot#webpack) Some initial work has also been made on supporting Sapper with Rollup, and basic support for simple cases is available. But this one is still in very early stages (and, again, poorly documented for now, sorry). I could really use some help with this one actually, if you're in the mood ;) - [sapper-template-hot#rollup](https://github.com/rixo/sapper-template-hot#rollup) Note that further work on HMR for Sapper is not pursued anymore, by me or anyone I would be aware of. ### Vite 1 Please note that both Vite 1 itself and the `vite-plugin-svelte` for Vite 1 linked bellow are deprecated / not supported anymore. - [@intrnl/vite-plugin-svelte] - [svite] ## License [ISC](LICENSE) [vite-plugin-svelte]: https://www.npmjs.com/package/@sveltejs/vite-plugin-svelte [svelte-loader]: https://github.com/sveltejs/svelte-loader [nollup]: https://github.com/PepsRyuu/nollup [rollup-plugin-hot]: https://github.com/rixo/rollup-plugin-hot [rollup-plugin-svelte-hot]: https://github.com/rixo/rollup-plugin-svelte-hot [rollup-plugin-svelte]: https://github.com/rollup/rollup-plugin-svelte [svelte-template-hot]: https://github.com/rixo/svelte-template-hot [svelte-template]: https://github.com/sveltejs/template [svelte-native-template]: https://github.com/halfnelson/svelte-native-template [svelte-loader-hot]: https://github.com/rixo/svelte-loader-hot [svelte-template-webpack-hot]: https://github.com/rixo/svelte-template-webpack-hot [@intrnl/vite-plugin-svelte]: https://github.com/intrnl/vite-plugin-svelte [svite]: https://github.com/dominikg/svite [snowpack/plugin-svelte]: https://github.com/snowpackjs/snowpack/tree/master/plugins/plugin-svelte [create-snowpack-app]: https://github.com/snowpackjs/snowpack/tree/master/create-snowpack-app/cli [app-template-svelte]: https://github.com/snowpackjs/snowpack/tree/master/create-snowpack-app/app-template-svelte