@@ -8,6 +8,7 @@ import { performanceLog } from "../../common/decorators";
88import {
99 WEBPACK_PLUGIN_NAME ,
1010 BUNDLER_COMPILATION_COMPLETE ,
11+ BUNDLER_APP_RESTART_REQUESTED ,
1112 PackageManagers ,
1213 CONFIG_FILE_NAME_DISPLAY ,
1314 VITE_DIST_FOLDER_NAME ,
@@ -60,6 +61,12 @@ const debugLog = false;
6061/** Grace period a bundler child gets to honour SIGINT before it is killed. */
6162const BUNDLER_STOP_TIMEOUT_MS = 5000 ;
6263
64+ /**
65+ * IPC message `vite serve` (`@nativescript/vite`) sends when a change can only
66+ * be applied by restarting the app process.
67+ */
68+ const VITE_APP_RESTART_MESSAGE_TYPE = "nativescript:restart" ;
69+
6370export class BundlerCompilerService
6471 extends EventEmitter
6572 implements IBundlerCompilerService
@@ -69,6 +76,12 @@ export class BundlerCompilerService
6976 // modules and HMR updates from. Keyed by platform, managed by this CLI
7077 // so users no longer need a separate `concurrently`/`wait-on` process.
7178 private viteServeProcesses : IDictionary < child_process . ChildProcess > = { } ;
79+ private pendingAppRestarts : IDictionary < {
80+ reasons : string [ ] ;
81+ timer : NodeJS . Timeout ;
82+ } > = { } ;
83+ /** Restart requests within this window collapse into one restart. */
84+ private appRestartDebounceMs = 300 ;
7285 private expectedHashes : IStringDictionary = { } ;
7386
7487 constructor (
@@ -737,16 +750,17 @@ export class BundlerCompilerService
737750
738751 const options : { [ key : string ] : any } = {
739752 cwd : projectData . projectDir ,
740- // Output is inherited so the dev server's URLs/logs stream to the
741- // user. No IPC needed here — the build watcher provides the
742- // bundle-complete IPC; the dev server is fetched over HTTP/ws .
753+ // Logs stream straight to the user. The IPC channel only carries
754+ // app-restart requests; a server that never sends one behaves as
755+ // with plain "inherit" .
743756 // Vite reads a terminal stdin for shortcuts of its own, which would
744757 // take the keys meant for the CLI's; otherwise its shortcuts are the
745758 // only ones the terminal has.
746759 stdio : [
747760 commandShortcutsOwnTerminal ( ) ? "ignore" : "inherit" ,
748761 "inherit" ,
749762 "inherit" ,
763+ "ipc" ,
750764 ] ,
751765 env : {
752766 ...process . env ,
@@ -770,9 +784,14 @@ export class BundlerCompilerService
770784 this . viteServeProcesses [ key ] = childProcess ;
771785 await this . $cleanupService . addKillProcess ( childProcess . pid . toString ( ) ) ;
772786
787+ childProcess . on ( "message" , ( message : unknown ) =>
788+ this . handleViteServeMessage ( message , key , projectData . projectDir ) ,
789+ ) ;
790+
773791 childProcess . once ( "exit" , ( code : number ) => {
774792 if ( this . viteServeProcesses [ key ] === childProcess ) {
775793 delete this . viteServeProcesses [ key ] ;
794+ this . cancelAppRestart ( key ) ;
776795 }
777796 if ( code ) {
778797 this . $logger . warn (
@@ -804,6 +823,68 @@ export class BundlerCompilerService
804823 }
805824 }
806825
826+ /**
827+ * Unknown message shapes are ignored without a log, so a newer
828+ * `@nativescript/vite` can add message types without breaking this CLI.
829+ */
830+ private handleViteServeMessage (
831+ message : unknown ,
832+ platform : string ,
833+ projectDir : string ,
834+ ) : void {
835+ if (
836+ ! message ||
837+ typeof message !== "object" ||
838+ ( message as { type ?: unknown } ) . type !== VITE_APP_RESTART_MESSAGE_TYPE
839+ ) {
840+ return ;
841+ }
842+
843+ const { reason } = message as { reason ?: unknown } ;
844+ this . requestAppRestart (
845+ platform ,
846+ projectDir ,
847+ typeof reason === "string" && reason
848+ ? reason
849+ : "the Vite dev server requested it" ,
850+ ) ;
851+ }
852+
853+ private requestAppRestart (
854+ platform : string ,
855+ projectDir : string ,
856+ reason : string ,
857+ ) : void {
858+ const pending = this . pendingAppRestarts [ platform ] || {
859+ reasons : [ ] ,
860+ timer : null ,
861+ } ;
862+ if ( ! pending . reasons . includes ( reason ) ) {
863+ pending . reasons . push ( reason ) ;
864+ }
865+ clearTimeout ( pending . timer ) ;
866+ pending . timer = setTimeout ( ( ) => {
867+ delete this . pendingAppRestarts [ platform ] ;
868+ const request : IBundlerAppRestartRequest = {
869+ projectDir,
870+ platform,
871+ reason : pending . reasons . join ( "; " ) ,
872+ } ;
873+ this . emit ( BUNDLER_APP_RESTART_REQUESTED , request ) ;
874+ } , this . appRestartDebounceMs ) ;
875+ // A pending timer must not be what keeps the CLI alive.
876+ pending . timer . unref ?.( ) ;
877+ this . pendingAppRestarts [ platform ] = pending ;
878+ }
879+
880+ private cancelAppRestart ( platform : string ) : void {
881+ const pending = this . pendingAppRestarts [ platform ] ;
882+ if ( pending ) {
883+ clearTimeout ( pending . timer ) ;
884+ delete this . pendingAppRestarts [ platform ] ;
885+ }
886+ }
887+
807888 /**
808889 * Resolve true once `127.0.0.1:<port>` accepts a TCP connection, or
809890 * false after `timeoutMs`. Used to detect the Vite dev server is up.
@@ -1042,6 +1123,8 @@ export class BundlerCompilerService
10421123 // Tear down the Vite dev server we manage alongside the build watcher.
10431124 const viteServeProcess = this . viteServeProcesses [ platform ] ;
10441125 if ( viteServeProcess ) {
1126+ viteServeProcess . removeAllListeners ( "message" ) ;
1127+ this . cancelAppRestart ( platform ) ;
10451128 await this . terminate ( viteServeProcess ) ;
10461129 if ( this . viteServeProcesses [ platform ] === viteServeProcess ) {
10471130 delete this . viteServeProcesses [ platform ] ;
0 commit comments