Skip to content

Commit 1ff2a35

Browse files
committed
Merge README.md from rixo/svelte-loader#svelte-hmr
1 parent 931dcc9 commit 1ff2a35

File tree

1 file changed

+57
-49
lines changed

1 file changed

+57
-49
lines changed

README.md

Lines changed: 57 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,12 @@ This should create an additional `styles.css.map` file.
145145

146146
### Hot Reload
147147

148-
Hot reloading is turned off by default, you can turn it on using the `hotReload` option as shown below:
148+
This loader supports component-level HMR via the community supported [svelte-hmr](https://github.com/rixo/svelte-hmr) package. This package serves as a testbed and early access for Svelte HMR, while we figure out how to best include HMR support in the compiler itself (which is tricky to do without unfairly favoring any particular dev tooling). Feedback, suggestion, or help to move HMR forward is welcomed at [svelte-hmr](https://github.com/rixo/svelte-hmr/issues) (for now).
149+
150+
Configure inside your `webpack.config.js`:
149151

150152
```javascript
153+
module.exports = {
151154
...
152155
module: {
153156
rules: [
@@ -158,68 +161,73 @@ Hot reloading is turned off by default, you can turn it on using the `hotReload`
158161
use: {
159162
loader: 'svelte-loader',
160163
options: {
161-
hotReload: true
164+
// NOTE Svelte's dev mode MUST be enabled for HMR to work
165+
// -- in a real config, you'd probably set it to false for prod build,
166+
// based on a env variable or so
167+
dev: true,
168+
169+
// NOTE emitCss: true is currently not supported with HMR
170+
emitCss: false,
171+
// Enable HMR
172+
hotReload: true, // Default: false
173+
// Extra HMR options
174+
hotOptions: {
175+
// Prevent preserving local component state
176+
noPreserveState: false,
177+
178+
// If this string appears anywhere in your component's code, then local
179+
// state won't be preserved, even when noPreserveState is false
180+
noPreserveStateKey: '@!hmr',
181+
182+
// Prevent doing a full reload on next HMR update after fatal error
183+
noReload: false,
184+
185+
// Try to recover after runtime errors in component init
186+
optimistic: false,
187+
188+
// --- Advanced ---
189+
190+
// Prevent adding an HMR accept handler to components with
191+
// accessors option to true, or to components with named exports
192+
// (from <script context="module">). This have the effect of
193+
// recreating the consumer of those components, instead of the
194+
// component themselves, on HMR updates. This might be needed to
195+
// reflect changes to accessors / named exports in the parents,
196+
// depending on how you use them.
197+
acceptAccessors: true,
198+
acceptNamedExports: true,
199+
}
162200
}
163201
}
164202
}
165203
...
166204
]
167-
}
168-
...
205+
},
206+
plugins: [
207+
new webpack.HotModuleReplacementPlugin(),
208+
...
209+
]
210+
}
169211
```
170212

171-
#### Hot reload rules and caveats:
172-
173-
- `_rerender` and `_register` are reserved method names, please don't use them in `methods:{...}`
174-
- Turning `dev` mode on (`dev:true`) is **not** necessary.
175-
- Modifying the HTML (template) part of your component will replace and re-render the changes in place. Current local state of the component will also be preserved (this can be turned off per component see [Stop preserving state](#stop-preserving-state)).
176-
- When modifying the `<script>` part of your component, instances will be replaced and re-rendered in place too.
177-
However if your component has lifecycle methods that produce global side-effects, you might need to reload the whole page.
178-
- If you are using `svelte/store`, a full reload is required if you modify `store` properties
179-
213+
You also need to add the [HotModuleReplacementPlugin](https://webpack.js.org/plugins/hot-module-replacement-plugin/). There are multiple ways to achieve this.
180214

181-
Components will **not** be hot reloaded in the following situations:
182-
1. `process.env.NODE_ENV === 'production'`
183-
2. Webpack is minifying code
184-
3. Webpack's `target` is `node` (i.e SSR components)
185-
4. `generate` option has a value of `ssr`
215+
If you're using webpack-dev-server, you can just pass it the [`hot` option](https://webpack.js.org/configuration/dev-server/#devserverhot) to add the plugin automatically.
186216

187-
#### Stop preserving state
217+
Otherwise, you can add it to your webpack config directly:
188218

189-
Sometimes it might be necessary for some components to avoid state preservation on hot reload.
190-
191-
This can be configured on a per-component basis by adding a property `noPreserveState = true` to the component's constructor using the `setup()` method. For example:
192219
```js
193-
export default {
194-
setup(comp){
195-
comp.noPreserveState = true;
196-
},
197-
data(){return {...}},
198-
oncreate(){...}
199-
}
200-
```
220+
const webpack = require('webpack');
201221

202-
Or, on a global basis by adding `{noPreserveState: true}` to `hotOptions`. For example:
203-
```js
204-
{
205-
test: /\.(html|svelte)$/,
206-
exclude: /node_modules/,
207-
use: [
208-
{
209-
loader: 'svelte-loader',
210-
options: {
211-
hotReload: true,
212-
hotOptions: {
213-
noPreserveState: true
214-
}
215-
}
216-
}
217-
]
218-
}
222+
module.exports = {
223+
...
224+
plugins: [
225+
new webpack.HotModuleReplacementPlugin(),
226+
...
227+
]
228+
}
219229
```
220230

221-
**Please Note:** If you are using `svelte/store`, `noPreserveState` has no effect on `store` properties. Neither locally, nor globally.
222-
223231
#### External Dependencies
224232

225233
If you rely on any external dependencies (files required in a preprocessor for example) you might want to watch these files for changes and re-run svelte compile.

0 commit comments

Comments
 (0)