You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+58-49
Original file line number
Diff line number
Diff line change
@@ -145,9 +145,12 @@ This should create an additional `styles.css.map` file.
145
145
146
146
### Hot Reload
147
147
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`:
149
151
150
152
```javascript
153
+
module.exports= {
151
154
...
152
155
module: {
153
156
rules: [
@@ -158,68 +161,74 @@ Hot reloading is turned off by default, you can turn it on using the `hotReload`
158
161
use: {
159
162
loader:'svelte-loader',
160
163
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
+
// Enable it for production to output separate css file
171
+
emitCss:false,
172
+
// Enable HMR only for dev mode
173
+
hotReload:true, // Default: false
174
+
// Extra HMR options
175
+
hotOptions: {
176
+
// Prevent preserving local component state
177
+
noPreserveState:false,
178
+
179
+
// If this string appears anywhere in your component's code, then local
180
+
// state won't be preserved, even when noPreserveState is false
181
+
noPreserveStateKey:'@!hmr',
182
+
183
+
// Prevent doing a full reload on next HMR update after fatal error
184
+
noReload:false,
185
+
186
+
// Try to recover after runtime errors in component init
187
+
optimistic:false,
188
+
189
+
// --- Advanced ---
190
+
191
+
// Prevent adding an HMR accept handler to components with
192
+
// accessors option to true, or to components with named exports
193
+
// (from <script context="module">). This have the effect of
194
+
// recreating the consumer of those components, instead of the
195
+
// component themselves, on HMR updates. This might be needed to
196
+
// reflect changes to accessors / named exports in the parents,
197
+
// depending on how you use them.
198
+
acceptAccessors:true,
199
+
acceptNamedExports:true,
200
+
}
162
201
}
163
202
}
164
203
}
165
204
...
166
205
]
167
-
}
168
-
...
206
+
},
207
+
plugins: [
208
+
newwebpack.HotModuleReplacementPlugin(),
209
+
...
210
+
]
211
+
}
169
212
```
170
213
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
-
214
+
You also need to add the [HotModuleReplacementPlugin](https://webpack.js.org/plugins/hot-module-replacement-plugin/). There are multiple ways to achieve this.
180
215
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`
216
+
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.
186
217
187
-
#### Stop preserving state
218
+
Otherwise, you can add it to your webpack config directly:
188
219
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:
192
220
```js
193
-
exportdefault {
194
-
setup(comp){
195
-
comp.noPreserveState=true;
196
-
},
197
-
data(){return {...}},
198
-
oncreate(){...}
199
-
}
200
-
```
221
+
constwebpack=require('webpack');
201
222
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
-
}
223
+
module.exports= {
224
+
...
225
+
plugins: [
226
+
newwebpack.HotModuleReplacementPlugin(),
227
+
...
228
+
]
229
+
}
219
230
```
220
231
221
-
**Please Note:** If you are using `svelte/store`, `noPreserveState` has no effect on `store` properties. Neither locally, nor globally.
222
-
223
232
#### External Dependencies
224
233
225
234
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