Skip to content

Commit 421672b

Browse files
committed
feat: vite docs hmr ref
1 parent 5764319 commit 421672b

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

content/configuration/vite.md

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,79 @@ The above config configures most things required to bundle a NativeScript applic
143143

144144
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/).
145145

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:
151+
152+
```ts
153+
import {
154+
onHmrUpdate,
155+
offHmrUpdate,
156+
} from '@nativescript/vite/hmr/shared/runtime/hooks';
157+
158+
// Use a stable id so the handler is replaced across hmr updates
159+
const id = 'my-app:name-of-module-or-feature';
160+
161+
onHmrUpdate(({ type, version, changedIds, raw }) => {
162+
// type: "full-graph" | "delta"
163+
// version: monotonically increasing batch id
164+
// changedIds: module ids affected in this batch
165+
// raw: original websocket message payload
166+
console.log('[HMR]', { type, version, changedIds });
167+
}, id);
168+
169+
// 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.
187+
188+
```ts
189+
import { onHmrUpdate } from '@nativescript/vite/hmr/shared/runtime/hooks';
190+
191+
class TabCustomizer {
192+
resetAccessory(payload?: any) {
193+
console.log('payload.changedIds:', payload?.changedIds);
194+
// ...your reset logic
195+
}
196+
}
197+
198+
// Keep a stable singleton for runtime references, but make it HMR-updatable by
199+
// swapping its prototype on module re-evaluation.
200+
const g = global as any;
201+
const existing = g.tabCustomizer as TabCustomizer | undefined;
202+
203+
if (existing) {
204+
Object.setPrototypeOf(existing as any, TabCustomizer.prototype);
205+
} else {
206+
g.tabCustomizer = new TabCustomizer();
207+
}
208+
209+
export const tabCustomizer = g.tabCustomizer as TabCustomizer;
210+
211+
onHmrUpdate((payload) => {
212+
// 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+
146219
## CLI Flags
147220

148221
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
171244

172245
We define a few useful globally available variables that you can use to alter logic in your applications.
173246

174-
- `__DEV__` - `true` when webpack is building in development mode
247+
- `__DEV__` - `true` when vite is building in development mode
175248
```ts
176249
if (__DEV__) {
177250
// we are running a dev build

content/ui/tab-view.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ See [UIImage.RenderingMode](https://developer.apple.com/documentation/uikit/uiim
167167

168168
### iosBottomAccessory
169169

170+
Adds a [bottom accessory view](https://developer.apple.com/documentation/uikit/uitabbarcontroller/bottomaccessory) above the tab bar on iOS 26+.
171+
170172
```ts
171173
iosBottomAccessory: View // iOS 26+ only
172174
```

0 commit comments

Comments
 (0)