{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 + +The [official Svelte plugin for Vite][vite-plugin-svelte] has HMR support. + +### Webpack + +The [official loader for Webpack][svelte-loader] now has HMR support. + +### Rollup + +Rollup does not natively support HMR, so we recommend using Vite. However, if you'd like to add HMR support to Rollup, the best way to get started is to refer to [svelte-template-hot], which demonstrates usage of both [rollup-plugin-hot] and [rollup-plugin-svelte-hot]. + +### Svelte Native + +The official [Svelte Native template][svelte-native-template] already includes HMR support. + +## License + +[ISC](LICENSE) + +[vite-plugin-svelte]: https://www.npmjs.com/package/@sveltejs/vite-plugin-svelte +[svelte-loader]: https://github.com/sveltejs/svelte-loader +[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 diff --git a/node_modules/svelte-hmr/index.js b/node_modules/svelte-hmr/index.js new file mode 100644 index 00000000..19eeca81 --- /dev/null +++ b/node_modules/svelte-hmr/index.js @@ -0,0 +1,12 @@ +const createMakeHotFactory = require('./lib/make-hot.js') +const { resolve } = require('path') +const { name, version } = require('./package.json') + +const resolveAbsoluteImport = target => resolve(__dirname, target) + +const createMakeHot = createMakeHotFactory({ + pkg: { name, version }, + resolveAbsoluteImport, +}) + +module.exports = { createMakeHot } diff --git a/node_modules/svelte-hmr/lib/css-only.js b/node_modules/svelte-hmr/lib/css-only.js new file mode 100644 index 00000000..4349ca87 --- /dev/null +++ b/node_modules/svelte-hmr/lib/css-only.js @@ -0,0 +1,18 @@ +/** + * Injects a `{}*` CSS rule to force Svelte compiler to scope every elements. + */ +export const injectScopeEverythingCssRule = (parse, code) => { + const { css } = parse(code) + if (!css) return code + const { + content: { end }, + } = css + return code.slice(0, end) + '*{}' + code.slice(end) +} + +export const normalizeJsCode = code => { + // Svelte now adds locations in dev mode, code locations can change when + // CSS change, but we're unaffected (not real behaviour changes) + code = code.replace(/\badd_location\s*\([^)]*\)\s*;?/g, '') + return code +} diff --git a/node_modules/svelte-hmr/lib/make-hot.js b/node_modules/svelte-hmr/lib/make-hot.js new file mode 100644 index 00000000..c4c0e706 --- /dev/null +++ b/node_modules/svelte-hmr/lib/make-hot.js @@ -0,0 +1,499 @@ +const globalName = '___SVELTE_HMR_HOT_API' +const globalAdapterName = '___SVELTE_HMR_HOT_API_PROXY_ADAPTER' + +const defaultHotOptions = { + // preserve all local state + preserveLocalState: false, + + // escape hatchs from preservation of local state + // + // disable preservation of state for this component + noPreserveStateKey: ['@hmr:reset', '@!hmr'], + // enable preservation of state for all variables in this component + preserveAllLocalStateKey: '@hmr:keep-all', + // enable preservation of state for a given variable (must be inline or + // above the target variable or variables; can be repeated) + preserveLocalStateKey: '@hmr:keep', + + // don't reload on fatal error + noReload: false, + // try to recover after runtime errors during component init + // defaults to false because some runtime errors are fatal and require a full reload + optimistic: false, + // auto accept modules of components that have named exports (i.e. exports + // from context="module") + acceptNamedExports: true, + // auto accept modules of components have accessors (either accessors compile + // option, or