@@ -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,15 @@ 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.
743- // Vite reads a terminal stdin for shortcuts of its own, which would
744- // take the keys meant for the CLI's; otherwise its shortcuts are the
745- // only ones the terminal has.
753+ // Logs stream straight to the user; the IPC channel carries
754+ // app-restart requests. Vite reads a terminal stdin for shortcuts of
755+ // its own, which would take the keys meant for the CLI's; otherwise
756+ // its shortcuts are the only ones the terminal has.
746757 stdio : [
747758 commandShortcutsOwnTerminal ( ) ? "ignore" : "inherit" ,
748759 "inherit" ,
749760 "inherit" ,
761+ "ipc" ,
750762 ] ,
751763 env : {
752764 ...process . env ,
@@ -770,9 +782,14 @@ export class BundlerCompilerService
770782 this . viteServeProcesses [ key ] = childProcess ;
771783 await this . $cleanupService . addKillProcess ( childProcess . pid . toString ( ) ) ;
772784
785+ childProcess . on ( "message" , ( message : unknown ) =>
786+ this . handleViteServeMessage ( message , key , projectData . projectDir ) ,
787+ ) ;
788+
773789 childProcess . once ( "exit" , ( code : number ) => {
774790 if ( this . viteServeProcesses [ key ] === childProcess ) {
775791 delete this . viteServeProcesses [ key ] ;
792+ this . cancelAppRestart ( key ) ;
776793 }
777794 if ( code ) {
778795 this . $logger . warn (
@@ -804,6 +821,68 @@ export class BundlerCompilerService
804821 }
805822 }
806823
824+ /**
825+ * Unknown message shapes are ignored without a log, so a newer
826+ * `@nativescript/vite` can add message types without breaking this CLI.
827+ */
828+ private handleViteServeMessage (
829+ message : unknown ,
830+ platform : string ,
831+ projectDir : string ,
832+ ) : void {
833+ if (
834+ ! message ||
835+ typeof message !== "object" ||
836+ ( message as { type ?: unknown } ) . type !== VITE_APP_RESTART_MESSAGE_TYPE
837+ ) {
838+ return ;
839+ }
840+
841+ const { reason } = message as { reason ?: unknown } ;
842+ this . requestAppRestart (
843+ platform ,
844+ projectDir ,
845+ typeof reason === "string" && reason
846+ ? reason
847+ : "the Vite dev server requested it" ,
848+ ) ;
849+ }
850+
851+ private requestAppRestart (
852+ platform : string ,
853+ projectDir : string ,
854+ reason : string ,
855+ ) : void {
856+ const pending = this . pendingAppRestarts [ platform ] || {
857+ reasons : [ ] ,
858+ timer : null ,
859+ } ;
860+ if ( ! pending . reasons . includes ( reason ) ) {
861+ pending . reasons . push ( reason ) ;
862+ }
863+ clearTimeout ( pending . timer ) ;
864+ pending . timer = setTimeout ( ( ) => {
865+ delete this . pendingAppRestarts [ platform ] ;
866+ const request : IBundlerAppRestartRequest = {
867+ projectDir,
868+ platform,
869+ reason : pending . reasons . join ( "; " ) ,
870+ } ;
871+ this . emit ( BUNDLER_APP_RESTART_REQUESTED , request ) ;
872+ } , this . appRestartDebounceMs ) ;
873+ // A pending timer must not be what keeps the CLI alive.
874+ pending . timer . unref ?.( ) ;
875+ this . pendingAppRestarts [ platform ] = pending ;
876+ }
877+
878+ private cancelAppRestart ( platform : string ) : void {
879+ const pending = this . pendingAppRestarts [ platform ] ;
880+ if ( pending ) {
881+ clearTimeout ( pending . timer ) ;
882+ delete this . pendingAppRestarts [ platform ] ;
883+ }
884+ }
885+
807886 /**
808887 * Resolve true once `127.0.0.1:<port>` accepts a TCP connection, or
809888 * false after `timeoutMs`. Used to detect the Vite dev server is up.
@@ -1042,6 +1121,8 @@ export class BundlerCompilerService
10421121 // Tear down the Vite dev server we manage alongside the build watcher.
10431122 const viteServeProcess = this . viteServeProcesses [ platform ] ;
10441123 if ( viteServeProcess ) {
1124+ viteServeProcess . removeAllListeners ( "message" ) ;
1125+ this . cancelAppRestart ( platform ) ;
10451126 await this . terminate ( viteServeProcess ) ;
10461127 if ( this . viteServeProcesses [ platform ] === viteServeProcess ) {
10471128 delete this . viteServeProcesses [ platform ] ;
0 commit comments