Skip to content

Commit 13e8609

Browse files
committed
docs: update readme with JIT and AOT configurations
1 parent 0291fee commit 13e8609

1 file changed

Lines changed: 139 additions & 3 deletions

File tree

README.md

Lines changed: 139 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,151 @@ On the client, calling this function with your component scans the DOM for any s
1212
npm install hypernova-angular
1313
```
1414

15-
## Usage
15+
## Server Usage
1616

17-
Here's how to use it in your browser module:
17+
Uses `renderAngular` to return hypernova bindings.
1818

1919
```ts
2020
import { renderAngular } from 'hypernova-angular'
2121

2222
import { ExampleModule } from './components/example/example.module'
2323
import { ExampleComponent } from './components/example/example.component'
2424

25-
renderAngular('Example', ExampleComponent, ExampleModule)
25+
hypernova({
26+
getComponent (name) {
27+
if (name === 'Example') {
28+
return renderAngular(name, ExampleComponent, ExampleModule)
29+
}
30+
}
31+
}
32+
```
33+
34+
## Browser Usage
35+
You can use [Ara CLI](https://github.com/ara-framework/ara-cli) to support client-side rendering.
36+
37+
```bash
38+
ara new:nova -t angular
39+
```
40+
41+
Also, you can use the following configurations.
42+
43+
### App Module
44+
45+
The following code define a `AppModule` responsible of bootstrapping the Angular component in Hypernova placeholder.
46+
47+
- `Hypernova.Name`: Name of the component to bootstrap.
48+
- `Hypernova.Node`: The placeholder where the component will be rendered.
49+
50+
```ts
51+
import { NgModule, Inject } from '@angular/core';
52+
import { BrowserModule } from '@angular/platform-browser';
53+
54+
import { ExampleModule } from './components/example/example.module'
55+
import { ExampleComponent } from './components/example/example.component';
56+
57+
const APP_ID = 'hypernova';
58+
59+
const components = {
60+
'Example': ExampleComponent
61+
}
62+
63+
@NgModule({
64+
imports: [
65+
ExampleModule,
66+
BrowserModule.withServerTransition({ appId: APP_ID }),
67+
],
68+
entryComponents: [ExampleComponent]
69+
})
70+
export class AppModule {
71+
constructor (
72+
@Inject('Hypernova.Name') private name: string,
73+
@Inject('Hypernova.Node') private node: HTMLElement
74+
){}
75+
76+
ngDoBootstrap(app) {
77+
const Component = components[this.name]
78+
if (Component) {
79+
return app.bootstrap(Component, this.node)
80+
}
81+
};
82+
}
83+
```
84+
85+
Use the `components` dictionary to support more components.
86+
87+
```ts
88+
const components = {
89+
[Hypernova.Name]: AngularComponent
90+
}
91+
```
92+
93+
### Browser JIT
94+
95+
`browser.main.ts`
96+
```ts
97+
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
98+
99+
import { AppModule } from './app.module';
100+
import { load, loadById, HypernovaModuleFactory } from 'hypernova-angular';
101+
102+
import { CompilerFactory, Compiler } from '@angular/core';
103+
104+
const platform = platformBrowserDynamic();
105+
106+
// Compile module (JIT)
107+
const compilerFactory: CompilerFactory = platform.injector.get(CompilerFactory);
108+
109+
const compiler: Compiler = compilerFactory.createCompiler([])
110+
111+
const moduleFactory = compiler.compileModuleSync(AppModule);
112+
113+
const render = (name: string, placeholder: any) => {
114+
115+
// Wrap module factory to provide necessary metadata to boostrap it.
116+
const hypernovaModuleFactory = new HypernovaModuleFactory(moduleFactory, name, placeholder);
117+
118+
platform.bootstrapModuleFactory(hypernovaModuleFactory);
119+
}
120+
121+
// Nova Bridge support
122+
document.addEventListener('NovaMount', (event) => {
123+
const { name, id } = (<CustomEvent>event).detail;
124+
125+
const placeholder = loadById(name, id);
126+
127+
if (placeholder) {
128+
render(name, placeholder);
129+
}
130+
})
131+
132+
// Render in placeholders rendered by Hypernova Server
133+
load('Example').forEach(render.bind(this, 'Example'));
134+
```
135+
136+
### Browser AOT
137+
138+
`browser.aot.main.ts`
139+
```ts
140+
import { platformBrowser } from '@angular/platform-browser';
141+
import { AppModuleNgFactory } from './app.module.ngfactory';
142+
import { load, loadById, HypernovaModuleFactory } from 'hypernova-angular';
143+
144+
enableProdMode();
145+
146+
const render = (name: string, placeholder: any) => {
147+
const hypernovaModuleFactory = new HypernovaModuleFactory(AppModuleNgFactory, name, placeholder);
148+
platformBrowser().bootstrapModuleFactory();
149+
}
150+
151+
document.addEventListener('NovaMount', (event) => {
152+
const { name, id } = (<CustomEvent>event).detail;
153+
154+
const placeholder = loadById(name, id);
155+
156+
if (placeholder) {
157+
render(name, placeholder);
158+
}
159+
})
160+
161+
load('Example').forEach(render.bind(this, 'Example'));
26162
```

0 commit comments

Comments
 (0)