Repository navigation
feat(vite): restart the app when the Vite dev server asks for it - #6208
Conversation
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
|
Verified end to end on an iOS 26.3 simulator. The Angular 22 fixture from NativeScript/NativeScript#11650 ran through this branch's CLI (
|
437a3bc to
acb5ec6
Compare
`vite serve` is now spawned with an IPC channel. When it sends
`{ type: 'nativescript:restart', reason }`, the CLI logs the reason and
restarts the app on that platform's devices in the running session,
without rebuilding or syncing. Requests that arrive within 300 ms
collapse into one restart. The restart joins the session's action chain,
so it runs after any sync or build already in progress. It reuses
RunController.restartApplication, which keeps debug sessions attached.
Other message shapes are ignored, and dev servers that never send one
behave as before.
Its stdin depends on whether the CLI's key shortcuts will read the terminal, which the startViteDevServer specs pin. The spawn comment no longer claims a silent server behaves as with plain inherit.
acb5ec6 to
8bb8f95
Compare
NathanWalker
left a comment
There was a problem hiding this comment.
Reviewed at 8bb8f954. This looks good to me. The full suite passes on the head (2451 passed, 9 skipped).
What I checked:
- Only
vite servesends the message. The onlyprocess.sendin@nativescript/viteis inns-cli-plugins.ts(apply: 'build'), so serve never fires it by accident. Webpack, and Vite without HMR or watch, never startvite serve, so nothing changes for them. - Restarts are scoped and queued. A request matches only the session's devices on that platform, case-insensitively, so visionOS is covered. An empty device list can't turn into "restart everything". A request that arrives mid-deploy waits behind the initial sync on the same queue.
- Teardown. Stopping the dev server, or it exiting on its own, drops the listener and any pending restart.
Non-blocking:
- The message can't name a device. There is one
vite serveper platform, so with a simulator and a phone attached, a per-device reason such asimport.meta.hot.decline()restarts both. If you might add a device or client field later, now is the time to define it: once Vite plugins depend on the current shape, a field added later would be ignored by this CLI version. - A restart can run just before a pending sync. If a restart request arrives while a file-change sync is still waiting in its timer, the sync runs right after the restart and can restart the app a second time. That's cosmetic.
- Two test gaps:
- The
getDevicesForPlatformfake matches the platform exactly and uses lowercase names, while real devices report"iOS"/"Android". Production ignores case, so it works, but the test doesn't cover it. - The
cancelAppRestartcall in theexithandler, for a dev server that exits with a restart pending, has no test.
- The
PR Checklist
What is the current behavior?
The CLI spawns
vite serve(the Vite HMR dev server) withstdio: "inherit", so the dev server has no channel back to the CLI. Some changes can't be applied as a hot update, and the dev server has no way to get the app process restarted for them.In
@nativescript/vitethin client mode (NativeScript/NativeScript#11650), most edits are applied in place: a hot update, or a soft reload that re-evaluates the app's own modules inside the running process. Some changes need a fresh process instead:node_modules, linked core, the dev client);vite.configchange, after which Vite restarts its server;import.meta.hot.decline();What is the new behavior?
vite serveis spawned with an IPC channel:stdio: ["inherit", "inherit", "inherit", "ipc"]. Its output still goes straight to the terminal.Contract. The dev server may send this message over that channel:
reasonis a human-readable string. One message may stand for several requests that the dev server already collapsed. Any other message shape is ignored without a log, so newer@nativescript/viteversions can add message types without breaking this CLI.Flow.
BundlerCompilerServicereceives the message and debounces per platform. Requests that arrive within 300 ms collapse into one, with their distinct reasons joined by;. It then emits the newBUNDLER_APP_RESTART_REQUESTEDevent with{ projectDir, platform, reason }.RunControllersubscribes to that event inrun()and unsubscribes instop(). It looks up the session's devices on that platform, logsRestarting the app on <platform>: <reason>at info level, and calls the existingrestartApplication({ projectDir, deviceIdentifiers }). That is the same path therkey shortcut uses: nothing is prepared, built or synced; it waits behind any sync or build already queued on the session's action chain; it goes throughrefreshApplication, so debug sessions get their debugger re-attached; and errors are reported per device. If the session has no device on that platform, nothing is restarted.Backwards compatibility.
@nativescript/viteversions never send anything over the channel, so behaviour is unchanged. The onlyprocess.senduse in released versions is thenativescript-cli-integrationplugin, which isapply: 'build'and doesn't run undervite serve. Vite itself doesn't use the channel.terminate()as before. A child with nomessagelistener exits normally, and the parent exits once the child does. The debounce timer isunref'd.vite serve, so nothing changes for them.Tests.
emittedFilesshape; a pending request is dropped when the dev server stops.Related to NativeScript/NativeScript#11650.
Summary by CodeRabbit