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: content/configuration/vite.md
+74-1Lines changed: 74 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -143,6 +143,79 @@ The above config configures most things required to bundle a NativeScript applic
143
143
144
144
This page contains examples of common things you might want to change in the [Examples of configurations section](#configuration-examples) - for anything else not mentioned here, refer to the [Vite documentation](https://vite.dev/config/).
145
145
146
+
## Advanced: HMR update hooks
147
+
148
+
When using the HMR workflow (for example `npm run dev:ios` / `npm run dev:android` / `npm run dev:visionos`, etc.), you may want to run some custom logic after each HMR batch is applied on device.
149
+
150
+
`@nativescript/vite` exposes a low-level hook for this:
// Optional: unregister when you no longer need it
170
+
// offHmrUpdate(id);
171
+
```
172
+
173
+
Why you might use this:
174
+
175
+
- Debug or observe HMR behavior (what changed, and when) without modifying the bundler.
176
+
- Trigger specific app-level actions after an update is applied (for example, refresh caches, re-run a health check, emit telemetry in dev builds).
177
+
178
+
Notes:
179
+
180
+
- The `id` is required and prevents duplicate handlers across module reloads; re-registering with the same `id` replaces the previous handler.
181
+
182
+
### Example: Persisting data across hmr updates
183
+
184
+
If you need a stable runtime reference across HMR (for example, a singleton that other modules hold onto), a common pattern is to store the data or instance on [import.meta.hot.data](https://vite.dev/guide/api-hmr#hot-data).
185
+
186
+
You could also store it on `global` and update it on module re-evaluation by swapping its prototype. Let's explore this global approach with an example of a hypothetical `TabCustomizer` singleton that needs to reset some state on each HMR update.
// Prefer calling into the stable singleton so the handler remains valid.
213
+
tabCustomizer.resetAccessory(payload);
214
+
}, 'tab-customize');
215
+
```
216
+
217
+
Tip: if you only care about specific updates, you can inspect `payload.changedIds` and return early when the batch doesn’t include the modules you care about.
218
+
146
219
## CLI Flags
147
220
148
221
When running a NativeScript app the following flags have an effect on the final vite config
@@ -171,7 +244,7 @@ Additional env flags that are usually passed by the CLI automatically
171
244
172
245
We define a few useful globally available variables that you can use to alter logic in your applications.
173
246
174
-
-`__DEV__` - `true` when webpack is building in development mode
247
+
-`__DEV__` - `true` when vite is building in development mode
0 commit comments