Skip to content

Commit 278aeec

Browse files
committed
feat: support Vite HMR
1 parent d9e7869 commit 278aeec

File tree

1 file changed

+46
-23
lines changed

1 file changed

+46
-23
lines changed

packages/angular/src/lib/application.ts

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -457,30 +457,53 @@ export function runNativeScriptAngularApp<T, K>(options: AppRunOptions<T, K>) {
457457
if (oldAddEventListener) {
458458
global.NativeScriptGlobals.events.addEventListener = oldAddEventListener;
459459
}
460-
if (import.meta['webpackHot']) {
461-
// handle HMR Application.run
462-
global['__dispose_app_ng_platform__'] = () => {
463-
disposePlatform('hotreload');
464-
};
465-
global['__dispose_app_ng_modules__'] = () => {
466-
disposeLastModules('hotreload');
467-
};
468-
global['__bootstrap_app_ng_modules__'] = () => {
469-
bootstrapRoot('hotreload');
470-
};
471-
global['__cleanup_ng_hot__'] = () => {
472-
Application.off(Application.launchEvent, launchCallback);
473-
Application.off(Application.exitEvent, exitCallback);
474-
disposeLastModules('hotreload');
460+
461+
// Detect HMR environment (webpack or Vite)
462+
const isWebpackHot = !!import.meta['webpackHot'];
463+
const isViteHot = !!import.meta['hot'];
464+
const isHotReloadEnabled = isWebpackHot || isViteHot;
465+
466+
// Always expose HMR globals for both webpack and Vite HMR support
467+
// These allow the HMR runtime to properly dispose and re-bootstrap Angular
468+
global['__dispose_app_ng_platform__'] = () => {
469+
disposePlatform('hotreload');
470+
};
471+
global['__dispose_app_ng_modules__'] = () => {
472+
disposeLastModules('hotreload');
473+
};
474+
global['__bootstrap_app_ng_modules__'] = () => {
475+
bootstrapRoot('hotreload');
476+
};
477+
global['__cleanup_ng_hot__'] = () => {
478+
Application.off(Application.launchEvent, launchCallback);
479+
Application.off(Application.exitEvent, exitCallback);
480+
disposeLastModules('hotreload');
481+
disposePlatform('hotreload');
482+
};
483+
global['__reboot_ng_modules__'] = (shouldDisposePlatform: boolean = false) => {
484+
disposeLastModules('hotreload');
485+
if (shouldDisposePlatform) {
475486
disposePlatform('hotreload');
476-
};
477-
global['__reboot_ng_modules__'] = (shouldDisposePlatform: boolean = false) => {
478-
disposeLastModules('hotreload');
479-
if (shouldDisposePlatform) {
480-
disposePlatform('hotreload');
481-
}
482-
bootstrapRoot('hotreload');
483-
};
487+
}
488+
bootstrapRoot('hotreload');
489+
};
490+
491+
if (isWebpackHot) {
492+
// Webpack-specific HMR handling
493+
import.meta['webpackHot'].decline();
494+
495+
if (!Application.hasLaunched()) {
496+
Application.run();
497+
return;
498+
}
499+
bootstrapRoot('hotreload');
500+
return;
501+
}
502+
503+
if (isViteHot) {
504+
// Vite-specific HMR handling
505+
// Vite HMR is handled by @nativescript/vite's HMR runtime
506+
// which will call __reboot_ng_modules__ when needed
484507

485508
if (!Application.hasLaunched()) {
486509
Application.run();

0 commit comments

Comments
 (0)