From 278aeec09bf7725dbab8a20fbb4dd654cca953ff Mon Sep 17 00:00:00 2001 From: Nathan Walker Date: Sat, 27 Dec 2025 13:04:32 -0800 Subject: [PATCH] feat: support Vite HMR --- packages/angular/src/lib/application.ts | 69 ++++++++++++++++--------- 1 file changed, 46 insertions(+), 23 deletions(-) diff --git a/packages/angular/src/lib/application.ts b/packages/angular/src/lib/application.ts index f4eb37d..54b8515 100644 --- a/packages/angular/src/lib/application.ts +++ b/packages/angular/src/lib/application.ts @@ -457,30 +457,53 @@ export function runNativeScriptAngularApp(options: AppRunOptions) { if (oldAddEventListener) { global.NativeScriptGlobals.events.addEventListener = oldAddEventListener; } - if (import.meta['webpackHot']) { - // handle HMR Application.run - global['__dispose_app_ng_platform__'] = () => { - disposePlatform('hotreload'); - }; - global['__dispose_app_ng_modules__'] = () => { - disposeLastModules('hotreload'); - }; - global['__bootstrap_app_ng_modules__'] = () => { - bootstrapRoot('hotreload'); - }; - global['__cleanup_ng_hot__'] = () => { - Application.off(Application.launchEvent, launchCallback); - Application.off(Application.exitEvent, exitCallback); - disposeLastModules('hotreload'); + + // Detect HMR environment (webpack or Vite) + const isWebpackHot = !!import.meta['webpackHot']; + const isViteHot = !!import.meta['hot']; + const isHotReloadEnabled = isWebpackHot || isViteHot; + + // Always expose HMR globals for both webpack and Vite HMR support + // These allow the HMR runtime to properly dispose and re-bootstrap Angular + global['__dispose_app_ng_platform__'] = () => { + disposePlatform('hotreload'); + }; + global['__dispose_app_ng_modules__'] = () => { + disposeLastModules('hotreload'); + }; + global['__bootstrap_app_ng_modules__'] = () => { + bootstrapRoot('hotreload'); + }; + global['__cleanup_ng_hot__'] = () => { + Application.off(Application.launchEvent, launchCallback); + Application.off(Application.exitEvent, exitCallback); + disposeLastModules('hotreload'); + disposePlatform('hotreload'); + }; + global['__reboot_ng_modules__'] = (shouldDisposePlatform: boolean = false) => { + disposeLastModules('hotreload'); + if (shouldDisposePlatform) { disposePlatform('hotreload'); - }; - global['__reboot_ng_modules__'] = (shouldDisposePlatform: boolean = false) => { - disposeLastModules('hotreload'); - if (shouldDisposePlatform) { - disposePlatform('hotreload'); - } - bootstrapRoot('hotreload'); - }; + } + bootstrapRoot('hotreload'); + }; + + if (isWebpackHot) { + // Webpack-specific HMR handling + import.meta['webpackHot'].decline(); + + if (!Application.hasLaunched()) { + Application.run(); + return; + } + bootstrapRoot('hotreload'); + return; + } + + if (isViteHot) { + // Vite-specific HMR handling + // Vite HMR is handled by @nativescript/vite's HMR runtime + // which will call __reboot_ng_modules__ when needed if (!Application.hasLaunched()) { Application.run();