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
+57-49Lines changed: 57 additions & 49 deletions
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,73 @@ 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
+
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
+
}
162
200
}
163
201
}
164
202
}
165
203
...
166
204
]
167
-
}
168
-
...
205
+
},
206
+
plugins: [
207
+
newwebpack.HotModuleReplacementPlugin(),
208
+
...
209
+
]
210
+
}
169
211
```
170
212
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.
180
214
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.
186
216
187
-
#### Stop preserving state
217
+
Otherwise, you can add it to your webpack config directly:
188
218
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
219
```js
193
-
exportdefault {
194
-
setup(comp){
195
-
comp.noPreserveState=true;
196
-
},
197
-
data(){return {...}},
198
-
oncreate(){...}
199
-
}
200
-
```
220
+
constwebpack=require('webpack');
201
221
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
+
newwebpack.HotModuleReplacementPlugin(),
226
+
...
227
+
]
228
+
}
219
229
```
220
230
221
-
**Please Note:** If you are using `svelte/store`, `noPreserveState` has no effect on `store` properties. Neither locally, nor globally.
222
-
223
231
#### External Dependencies
224
232
225
233
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