Skip to content

Commit 56fe565

Browse files
committed
feat: add initial support for standalone bootstrap
1 parent d9408fe commit 56fe565

File tree

11 files changed

+412
-105
lines changed

11 files changed

+412
-105
lines changed

apps/nativescript-demo-ng/src/app/app-routing.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { ItemsComponent } from './item/items.component';
77
// import { HomeComponent } from './home/home.component';
88
// import { BootGuardService } from './boot-guard.service';
99

10-
const routes: Routes = [
10+
export const routes: Routes = [
1111
{ path: '', redirectTo: '/rootlazy', pathMatch: 'full' },
1212
{ path: 'items', component: ItemsComponent },
1313
{ path: 'item/:id', component: ItemDetailComponent },

apps/nativescript-demo-ng/src/app/app.component.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import { Component, ViewContainerRef, OnInit, OnDestroy } from '@angular/core';
1+
import { Component, ViewContainerRef, OnInit, OnDestroy, NO_ERRORS_SCHEMA } from '@angular/core';
2+
import { NativeDialogModule, NativeScriptHttpClientModule, PageRouterOutlet } from '@nativescript/angular';
3+
import { AppRoutingModule } from './app-routing.module';
24

35
// registerElement('ns-app', () => GridLayout);
46
@Component({
57
selector: 'ns-app',
68
moduleId: module.id,
79
templateUrl: './app.component.html',
8-
standalone: false,
10+
imports: [PageRouterOutlet],
11+
schemas: [NO_ERRORS_SCHEMA],
912
})
1013
export class AppComponent implements OnInit, OnDestroy {
1114
constructor(private vcRef: ViewContainerRef) {}

apps/nativescript-demo-ng/src/app/app.module.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@ import { ModalComponent } from './modal/modal.component';
1313
// import { HomeComponent } from './home/home.component';
1414
// import { NativeScriptMaterialBottomNavigationModule } from '@nativescript-community/ui-material-bottom-navigation/angular';
1515

16-
@NgModule({
17-
bootstrap: [AppComponent],
18-
imports: [
19-
NativeScriptModule,
20-
NativeScriptHttpClientModule,
21-
AppRoutingModule,
22-
NativeDialogModule,
23-
// NativeScriptMaterialBottomNavigationModule
24-
],
25-
declarations: [
26-
AppComponent,
27-
// ItemsComponent,
28-
// ItemDetailComponent,
29-
// ModalComponent,
30-
// HomeComponent
31-
],
32-
providers: [],
33-
schemas: [NO_ERRORS_SCHEMA],
34-
})
35-
export class AppModule {}
16+
// @NgModule({
17+
// bootstrap: [AppComponent],
18+
// imports: [
19+
// NativeScriptModule,
20+
// NativeScriptHttpClientModule,
21+
// AppRoutingModule,
22+
// NativeDialogModule,
23+
// // NativeScriptMaterialBottomNavigationModule
24+
// ],
25+
// declarations: [
26+
// AppComponent,
27+
// // ItemsComponent,
28+
// // ItemDetailComponent,
29+
// // ModalComponent,
30+
// // HomeComponent
31+
// ],
32+
// providers: [],
33+
// schemas: [NO_ERRORS_SCHEMA],
34+
// })
35+
// export class AppModule {}

apps/nativescript-demo-ng/src/main.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1-
import { NativeScriptNgZone, platformNativeScript, runNativeScriptAngularApp } from '@nativescript/angular';
1+
import {
2+
bootstrapApplication,
3+
NativeDialogModule,
4+
provideNativeScriptHttpClient,
5+
provideNativeScriptNgZone,
6+
provideNativeScriptRouter,
7+
runNativeScriptAngularApp,
8+
} from '@nativescript/angular';
29
import { Trace } from '@nativescript/core';
310

4-
import { AppModule } from './app/app.module';
11+
// import { AppModule } from './app/app.module';
12+
import { withInterceptorsFromDi } from '@angular/common/http';
13+
import { importProvidersFrom } from '@angular/core';
514
import { setWindowBackgroundColor } from '@nativescript/core/utils/ios';
15+
import { routes } from './app/app-routing.module';
16+
import { AppComponent } from './app/app.component';
617

718
Trace.enable();
819
Trace.setCategories('ns-route-reuse-strategy,ns-router');
@@ -12,8 +23,13 @@ runNativeScriptAngularApp({
1223
if (global.isIOS) {
1324
setWindowBackgroundColor('#a6120d');
1425
}
15-
return platformNativeScript().bootstrapModule(AppModule, {
16-
ngZone: new NativeScriptNgZone(),
26+
return bootstrapApplication(AppComponent, {
27+
providers: [
28+
provideNativeScriptHttpClient(withInterceptorsFromDi()),
29+
provideNativeScriptRouter(routes),
30+
importProvidersFrom(NativeDialogModule),
31+
provideNativeScriptNgZone(),
32+
],
1733
});
1834
},
1935
});

packages/angular/src/lib/application.ts

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
1-
import { ApplicationRef, EnvironmentProviders, NgModuleRef, NgZone, PlatformRef, Provider, Type, ɵinternalCreateApplication as internalCreateApplication } from '@angular/core';
1+
import { ApplicationRef, EnvironmentProviders, NgModuleRef, NgZone, PlatformRef, Provider } from '@angular/core';
2+
import {
3+
Application,
4+
ApplicationEventData,
5+
Color,
6+
LaunchEventData,
7+
LayoutBase,
8+
profile,
9+
removeTaggedAdditionalCSS,
10+
TextView,
11+
Utils,
12+
View,
13+
} from '@nativescript/core';
14+
import { Observable, Subject } from 'rxjs';
215
import { filter, map, take } from 'rxjs/operators';
3-
import { Application, ApplicationEventData, Color, LaunchEventData, LayoutBase, profile, removeTaggedAdditionalCSS, StackLayout, TextView, View, Utils, Trace } from '@nativescript/core';
416
import { AppHostView } from './app-host-view';
517
import { NativeScriptLoadingService } from './loading.service';
618
import { APP_ROOT_VIEW, DISABLE_ROOT_VIEW_HANDLING, NATIVESCRIPT_ROOT_MODULE_ID } from './tokens';
7-
import { Observable, Subject } from 'rxjs';
819
import { NativeScriptDebug } from './trace';
9-
import { NATIVESCRIPT_MODULE_PROVIDERS, NATIVESCRIPT_MODULE_STATIC_PROVIDERS } from './nativescript';
1020

1121
export interface AppLaunchView extends LayoutBase {
1222
// called when the animation is to begin
@@ -45,7 +55,7 @@ export const postAngularBootstrap$ = new Subject<NgModuleEvent>();
4555
*/
4656
export const onBeforeLivesync: Observable<NgModuleRef<any>> = preAngularDisposal$.pipe(
4757
filter((v) => v.moduleType === 'main' && v.reason === 'hotreload'),
48-
map((v) => v.reference as NgModuleRef<any>)
58+
map((v) => v.reference as NgModuleRef<any>),
4959
);
5060
/**
5161
* @deprecated
@@ -55,7 +65,7 @@ export const onAfterLivesync: Observable<{
5565
error?: Error;
5666
}> = postAngularBootstrap$.pipe(
5767
filter((v) => v.moduleType === 'main'),
58-
map((v) => ({ moduleRef: v.reference as NgModuleRef<any> }))
68+
map((v) => ({ moduleRef: v.reference as NgModuleRef<any> })),
5969
);
6070
export interface AppRunOptions<T, K> {
6171
/**
@@ -92,7 +102,11 @@ if (import.meta['webpackHot']) {
92102
};
93103
}
94104

95-
function emitModuleBootstrapEvent<T>(ref: NgModuleRef<T> | ApplicationRef, name: 'main' | 'loading', reason: NgModuleReason) {
105+
function emitModuleBootstrapEvent<T>(
106+
ref: NgModuleRef<T> | ApplicationRef,
107+
name: 'main' | 'loading',
108+
reason: NgModuleReason,
109+
) {
96110
postAngularBootstrap$.next({
97111
moduleType: name,
98112
reference: ref,
@@ -136,7 +150,7 @@ function runZoneSyncTask(fn: () => void) {
136150
},
137151
() => {
138152
//
139-
}
153+
},
140154
);
141155
try {
142156
// console.log(task.state);
@@ -198,22 +212,12 @@ function runSynchronously(fn: () => void, done?: () => void): void {
198212
}
199213
}
200214

201-
function createProvidersConfig(options?: ApplicationConfig) {
202-
return {
203-
appProviders: [...NATIVESCRIPT_MODULE_PROVIDERS, ...NATIVESCRIPT_MODULE_STATIC_PROVIDERS, ...(options?.providers ?? [])],
204-
// platformProviders: INTERNAL_BROWSER_PLATFORM_PROVIDERS
205-
};
206-
}
207-
208215
export interface ApplicationConfig {
209216
/**
210217
* List of providers that should be available to the root component and all its children.
211218
*/
212219
providers: Array<Provider | EnvironmentProviders>;
213220
}
214-
export function bootstrapApplication(rootComponent: Type<unknown>, options?: ApplicationConfig): Promise<ApplicationRef> {
215-
return internalCreateApplication({ rootComponent, ...createProvidersConfig(options) });
216-
}
217221

218222
export function runNativeScriptAngularApp<T, K>(options: AppRunOptions<T, K>) {
219223
let mainModuleRef: NgModuleRef<T> | ApplicationRef = null;
@@ -301,7 +305,9 @@ export function runNativeScriptAngularApp<T, K>(options: AppRunOptions<T, K>) {
301305
}
302306
mainModuleRef = ref;
303307

304-
(ref instanceof ApplicationRef ? ref.components[0] : ref).onDestroy(() => (mainModuleRef = mainModuleRef === ref ? null : mainModuleRef));
308+
(ref instanceof ApplicationRef ? ref.components[0] : ref).onDestroy(
309+
() => (mainModuleRef = mainModuleRef === ref ? null : mainModuleRef),
310+
);
305311
updatePlatformRef(ref, reason);
306312
const styleTag = ref.injector.get(NATIVESCRIPT_ROOT_MODULE_ID);
307313
(ref instanceof ApplicationRef ? ref.components[0] : ref).onDestroy(() => {
@@ -317,7 +323,7 @@ export function runNativeScriptAngularApp<T, K>(options: AppRunOptions<T, K>) {
317323
NativeScriptDebug.bootstrapLogError(`Error bootstrapping app module:\n${err.message}\n\n${err.stack}`);
318324
showErrorUI(err);
319325
throw err;
320-
}
326+
},
321327
),
322328
() => {
323329
if (currentBootstrapId !== bootstrapId) {
@@ -335,10 +341,16 @@ export function runNativeScriptAngularApp<T, K>(options: AppRunOptions<T, K>) {
335341
return;
336342
}
337343
loadingModuleRef = loadingRef;
338-
(loadingModuleRef instanceof ApplicationRef ? loadingModuleRef.components[0] : loadingModuleRef).onDestroy(() => (loadingModuleRef = loadingModuleRef === loadingRef ? null : loadingModuleRef));
344+
(loadingModuleRef instanceof ApplicationRef
345+
? loadingModuleRef.components[0]
346+
: loadingModuleRef
347+
).onDestroy(() => (loadingModuleRef = loadingModuleRef === loadingRef ? null : loadingModuleRef));
339348
updatePlatformRef(loadingRef, reason);
340349
const styleTag = loadingModuleRef.injector.get(NATIVESCRIPT_ROOT_MODULE_ID);
341-
(loadingModuleRef instanceof ApplicationRef ? loadingModuleRef.components[0] : loadingModuleRef).onDestroy(() => {
350+
(loadingModuleRef instanceof ApplicationRef
351+
? loadingModuleRef.components[0]
352+
: loadingModuleRef
353+
).onDestroy(() => {
342354
removeTaggedAdditionalCSS(styleTag);
343355
});
344356
setRootView(loadingRef);
@@ -352,7 +364,7 @@ export function runNativeScriptAngularApp<T, K>(options: AppRunOptions<T, K>) {
352364
loadingService.readyToDestroy$
353365
.pipe(
354366
filter((ready) => ready),
355-
take(1)
367+
take(1),
356368
)
357369
.subscribe(() => {
358370
destroyRef(loadingModuleRef, 'loading', reason);
@@ -364,11 +376,13 @@ export function runNativeScriptAngularApp<T, K>(options: AppRunOptions<T, K>) {
364376
emitModuleBootstrapEvent(loadingModuleRef, 'loading', reason);
365377
},
366378
(err) => {
367-
NativeScriptDebug.bootstrapLogError(`Error bootstrapping loading module:\n${err.message}\n\n${err.stack}`);
379+
NativeScriptDebug.bootstrapLogError(
380+
`Error bootstrapping loading module:\n${err.message}\n\n${err.stack}`,
381+
);
368382
showErrorUI(err);
369383
throw err;
370-
}
371-
)
384+
},
385+
),
372386
);
373387
} else if (options.launchView) {
374388
let launchView = options.launchView(reason);
@@ -397,10 +411,12 @@ export function runNativeScriptAngularApp<T, K>(options: AppRunOptions<T, K>) {
397411
});
398412
};
399413
} else {
400-
console.warn('App is bootstrapping asynchronously (likely APP_INITIALIZER) but did not provide a launchView or LoadingModule.');
414+
console.warn(
415+
'App is bootstrapping asynchronously (likely APP_INITIALIZER) but did not provide a launchView or LoadingModule.',
416+
);
401417
}
402418
}
403-
}
419+
},
404420
);
405421
} catch (err) {
406422
NativeScriptDebug.bootstrapLogError(`Error in Bootstrap Function:\n${err.message}\n\n${err.stack}`);
@@ -431,7 +447,8 @@ export function runNativeScriptAngularApp<T, K>(options: AppRunOptions<T, K>) {
431447
let oldAddEventListener;
432448
if (typeof Zone !== 'undefined' && global.NativeScriptGlobals?.events?.[Zone.__symbol__('addEventListener')]) {
433449
oldAddEventListener = global.NativeScriptGlobals.events.addEventListener;
434-
global.NativeScriptGlobals.events.addEventListener = global.NativeScriptGlobals.events[Zone.__symbol__('addEventListener')];
450+
global.NativeScriptGlobals.events.addEventListener =
451+
global.NativeScriptGlobals.events[Zone.__symbol__('addEventListener')];
435452
}
436453
if (!options.embedded) {
437454
Application.on(Application.launchEvent, launchCallback);

0 commit comments

Comments
 (0)