Skip to content

Commit 82c8586

Browse files
committed
feat: added fastBootstrapApplication function
1 parent 96527b2 commit 82c8586

File tree

5 files changed

+90
-30
lines changed

5 files changed

+90
-30
lines changed

README.md

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ yarn add ngx-fastboot
4242

4343
## Usage
4444

45-
To use **ngx-fastboot**, import the `fast` function and call it with your Angular application's `bootstrapApplication` function, root component, and configuration options.
45+
To use **ngx-fastboot**, import the `fastBootstrapApplication` function and call it as your Angular application's `bootstrapApplication` function.
4646

4747
### Example
4848

@@ -54,7 +54,6 @@ import { AppComponent } from './app.component';
5454
import { bootstrapApplication } from '@angular/platform-browser';
5555
import provideMyDefaultExportFeatures from './default-export-features.config';
5656
import { provideMyOtherFeatures } from './my-other-features.config';
57-
import { fast } from 'ngx-fastboot';
5857

5958
bootstrapApplication(AppComponent, {
6059
providers: [
@@ -74,10 +73,9 @@ bootstrapApplication(AppComponent, {
7473
`main.ts`
7574
```typescript
7675
import { AppComponent } from './app.component';
77-
import { bootstrapApplication } from '@angular/platform-browser';
78-
import { fast } from 'ngx-fastboot';
76+
import { fastBootstrapApplication } from 'ngx-fastboot';
7977

80-
fast(bootstrapApplication, AppComponent, {
78+
fastBootstrapApplication(AppComponent, {
8179
providers: [
8280
MyProvider,
8381
() => import('./default-export-features.config'), // default export config
@@ -107,12 +105,11 @@ You can specify these providers in three main ways:
107105
`main.ts`
108106
```typescript
109107
import { AppComponent } from './app.component';
110-
import { bootstrapApplication } from '@angular/platform-browser';
111108
import { MyProvider } from 'providers/my-provider';
112109
import { OtherProvider } from 'providers/other-provider';
113-
import { fast } from 'ngx-fastboot';
110+
import { fastBootstrapApplication } from 'ngx-fastboot';
114111

115-
fast(bootstrapApplication, AppComponent, {
112+
fastBootstrapApplication(AppComponent, {
116113
providers: [
117114
MyProvider,
118115
OtherProvider,
@@ -143,10 +140,9 @@ export const OtherProvider = [
143140
`main.ts`
144141
```typescript
145142
import { AppComponent } from './app.component';
146-
import { bootstrapApplication } from '@angular/platform-browser';
147-
import { fast } from 'ngx-fastboot';
143+
import { fastBootstrapApplication } from 'ngx-fastboot';
148144

149-
fast(bootstrapApplication, AppComponent, {
145+
fastBootstrapApplication(AppComponent, {
150146
providers: [
151147
() => import('providers/my-provider').then((m) => m.MyProvider), // single
152148
() => import('providers/other-provider').then((m) => m.OtherProvider), // array
@@ -177,10 +173,9 @@ export default [
177173
`main.ts`
178174
```typescript
179175
import { AppComponent } from './app.component';
180-
import { bootstrapApplication } from '@angular/platform-browser';
181-
import { fast } from 'ngx-fastboot';
176+
import { fastBootstrapApplication } from 'ngx-fastboot';
182177

183-
fast(bootstrapApplication, AppComponent, {
178+
fastBootstrapApplication(AppComponent, {
184179
providers: [
185180
() => import('providers/my-provider'), // single
186181
() => import('providers/other-provider'), // array
@@ -197,11 +192,10 @@ fast(bootstrapApplication, AppComponent, {
197192
`main.ts`
198193
```typescript
199194
import { AppComponent } from './app.component';
200-
import { bootstrapApplication } from '@angular/platform-browser';
201195
import { MyStaticImportedProvider } from './providers/my-static-imported-provider';
202-
import { fast } from 'ngx-fastboot';
196+
import { fastBootstrapApplication } from 'ngx-fastboot';
203197

204-
fast(bootstrapApplication, AppComponent, {
198+
fastBootstrapApplication(AppComponent, {
205199
providers: [
206200
MyStaticImportedProvider,
207201
() => import('providers/my-provider').then((m) => m.MyProvider),
@@ -223,7 +217,7 @@ Similar to providers, you can manage the root component of the application both
223217
The classic method to bootstrap the root component involves a static import:
224218

225219
```typescript
226-
fast(bootstrapApplication, AppComponent, {
220+
fastBootstrapApplication(AppComponent, {
227221
providers: [...]
228222
});
229223
```
@@ -232,8 +226,7 @@ fast(bootstrapApplication, AppComponent, {
232226
To optimize bundle size, the root component can be loaded dynamically with a named import:
233227

234228
```typescript
235-
fast(
236-
bootstrapApplication,
229+
fastBootstrapApplication(
237230
() => import('./app-component').then((m) => m.AppComponent), {
238231
providers: [...]
239232
});
@@ -243,8 +236,7 @@ fast(
243236
Alternatively, you can use a dynamic default import if the root component is exported as the default from the module:
244237

245238
```typescript
246-
fast(
247-
bootstrapApplication,
239+
fastBootstrapApplication(
248240
() => import('./app-component'), {
249241
providers: [...]
250242
});
@@ -308,7 +300,7 @@ import { AppComponent } from './app.component';
308300
import { bootstrapApplication } from '@angular/platform-browser';
309301
import { fast } from 'ngx-fastboot';
310302

311-
fast(bootstrapApplication, AppComponent, {
303+
fastBootstrapApplication(AppComponent, {
312304
providers: [
313305
() => import('./app/configs/sentry.config'),
314306
],
@@ -323,13 +315,26 @@ fast(bootstrapApplication, AppComponent, {
323315

324316
## API
325317

326-
### `fast`
318+
### `fastBootstrapApplication`
327319

328320
Dynamically loads the specified providers in the configuration and bootstraps an Angular application.
329321

330322
#### Parameters
331323

332-
- **`bootstrap`**: The Angular application's bootstrap function (typically `bootstrapApplication`).
324+
- **`rootComponent`**: The root component of the application, which should be of type `FastComponent`.
325+
- **`options`**: (Optional) The application configuration, including the providers to be loaded. It should conform to the `FastApplicationConfig` type. Providers can be `Provider`, `EnvironmentProviders`, or lazy modules that return these providers.
326+
327+
#### Returns
328+
329+
A `Promise` that resolves to an `ApplicationRef` instance of the bootstrapped application. The bootstrap method is called with the root component and the updated configuration with the resolved providers.
330+
331+
### `fast`
332+
333+
Dynamically loads the specified providers in the configuration and bootstraps an Angular application passing a custom bootstrap function.
334+
335+
#### Parameters
336+
337+
- **`bootstrap`**: The bootstrap function (typically `bootstrapApplication`).
333338
- **`rootComponent`**: The root component of the application, which should be of type `FastComponent`.
334339
- **`options`**: (Optional) The application configuration, including the providers to be loaded. It should conform to the `FastApplicationConfig` type. Providers can be `Provider`, `EnvironmentProviders`, or lazy modules that return these providers.
335340

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,16 @@
5050
},
5151
"packageManager": "[email protected]",
5252
"engines": {
53-
"node": ">=20.10.0",
54-
"pnpm": ">=9.15.0"
53+
"node": ">=20.10.0",
54+
"pnpm": ">=9.15.0"
5555
},
5656
"pnpm": {
5757
"executionEnv": {
5858
"nodeVersion": "20.10.0"
5959
}
6060
},
6161
"peerDependencies": {
62-
"@angular/core": "^19.0.0",
62+
"@angular/core": "^19.0.0",
6363
"@angular/platform-browser": "^19.0.0"
6464
},
6565
"devDependencies": {

src/lib/fast-bootstrap-application.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { ApplicationConfig, ApplicationRef } from '@angular/core';
2+
import { bootstrapApplication } from '@angular/platform-browser';
3+
4+
import { fast } from './fast';
5+
import { resolveDependencies } from './resolve-dependencies';
6+
import { FastApplicationConfig, FastComponent } from './types';
7+
8+
/**
9+
* Dynamically loads the specified providers in the configuration and bootstraps an Angular application.
10+
*
11+
* This function start an Angular application with providers that are dynamically loaded and resolved.
12+
* The application configuration can include providers that need to be loaded asynchronously.
13+
* This function handles resolving these providers and passing them to the angular bootstrapApplication function.
14+
*
15+
* @param rootComponent - The root component of the application, which should be of type `FastComponent`
16+
* (ie. Type<unknown> or lazy module that return this component).
17+
* @param options - (Optional) The application configuration, including the providers to be loaded. It should conform
18+
* to the `FastApplicationConfig` type. Providers can be `Provider`, `EnvironmentProviders`,
19+
* or lazy modules that return these providers.
20+
*
21+
* @returns A Promise that resolves to an `ApplicationRef` instance of the bootstrapped application. The bootstrap
22+
* method is called with the root component and the updated configuration with the resolved providers.
23+
*
24+
* @example
25+
* ```typescript
26+
* import { AppComponent } from './app.component';
27+
* import { fastBootstrapApplication } from 'ngx-fastboot';
28+
*
29+
* fastBootstrapApplication(AppComponent, {
30+
* providers: [
31+
* MyProvider,
32+
* () => import('./my-provider.module'),
33+
* ]
34+
* }).then(() => console.log('App is bootstrapped'))
35+
* .catch(error => {
36+
* console.error('Error bootstrapping the app', error);
37+
* });
38+
* ```
39+
*/
40+
export const fastBootstrapApplication = async (
41+
rootComponent: FastComponent,
42+
options?: FastApplicationConfig,
43+
): Promise<ApplicationRef> => {
44+
const { component, providers } = await resolveDependencies(
45+
rootComponent,
46+
options?.providers ?? [],
47+
);
48+
const nextOptions: ApplicationConfig = {
49+
...options,
50+
providers,
51+
};
52+
53+
return fast(bootstrapApplication, component, nextOptions);
54+
};

src/lib/fast.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import { FastApplicationConfig, FastComponent } from './types';
77
/**
88
* Dynamically loads the specified providers in the configuration and bootstraps an Angular application.
99
*
10-
* This function uses the `bootstrapApplication` method to start an Angular application with providers
10+
* This function uses the custom applicationBoostrap function argument to start an Angular application with providers
1111
* that are dynamically loaded and resolved. The application configuration can include providers that need to
12-
* be loaded asynchronously. This function handles resolving these providers and passing them to the bootstrap method.
12+
* be loaded asynchronously. This function handles resolving these providers and passing them to the custom bootstrap function.
1313
*
1414
* @param bootstrap - The Angular application's bootstrap function (typically `bootstrapApplication`).
1515
* @param rootComponent - The root component of the application, which should be of type `FastComponent`

src/lib/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './fast';
2+
export * from './fast-bootstrap-application';
23
export * from './is-type-provider';
34
export * from './load-module';
45
export * from './resolve-provider';

0 commit comments

Comments
 (0)