|
4 | 4 |
|
5 | 5 | On the server, wraps the component in a function to render it to a HTML string given its props. |
6 | 6 |
|
7 | | -On the client, calling this function with your component scans the DOM for any server-side rendered instances of it. It then resumes those components using the server-specified props. |
| 7 | +On the client, uses the `HypernovaModuleFactory` to provide the metadata necessary to bootstrap the components. |
| 8 | + |
| 9 | +**Note**: `renderAngular` and `mountComponent` are not supported anymore. They were removed to move the boostrapping responsability to the library consumer in order to support AOT and JIT compiling. |
8 | 10 |
|
9 | 11 | ## Install |
10 | 12 |
|
11 | 13 | ```sh |
12 | 14 | npm install hypernova-angular |
13 | 15 | ``` |
14 | 16 |
|
15 | | -## Usage |
| 17 | +## Server Usage |
16 | 18 |
|
17 | | -Here's how to use it in your browser module: |
| 19 | +Uses `renderAngular` to return hypernova bindings. |
18 | 20 |
|
19 | 21 | ```ts |
20 | | -import { renderAngular } from 'hypernova-angular' |
| 22 | +import { renderAngular } from 'hypernova-angular/server' |
21 | 23 |
|
22 | 24 | import { ExampleModule } from './components/example/example.module' |
23 | 25 | import { ExampleComponent } from './components/example/example.component' |
24 | 26 |
|
25 | | -renderAngular('Example', ExampleComponent, ExampleModule) |
| 27 | +hypernova({ |
| 28 | + getComponent (name) { |
| 29 | + if (name === 'Example') { |
| 30 | + return renderAngular(name, ExampleComponent, ExampleModule) |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | +``` |
| 35 | +
|
| 36 | +## Browser Usage |
| 37 | +
|
| 38 | +You can use [Ara CLI](https://github.com/ara-framework/ara-cli) to create a service (Nova) with everything ready to start. |
| 39 | +
|
| 40 | +```bash |
| 41 | +ara new:nova -t angular |
| 42 | +``` |
| 43 | +
|
| 44 | +Or, you can use the following configurations. |
| 45 | +
|
| 46 | +### App Module |
| 47 | +
|
| 48 | +The following code define a `AppModule` responsible of bootstrapping the Angular component in Hypernova placeholder. |
| 49 | +
|
| 50 | +- `Hypernova.Name`: Name of the component to bootstrap. |
| 51 | +- `Hypernova.Node`: The placeholder where the component will be rendered. |
| 52 | +
|
| 53 | +```ts |
| 54 | +import { NgModule, Inject } from '@angular/core'; |
| 55 | +import { BrowserModule } from '@angular/platform-browser'; |
| 56 | + |
| 57 | +import { ExampleModule } from './components/example/example.module' |
| 58 | +import { ExampleComponent } from './components/example/example.component'; |
| 59 | + |
| 60 | +const APP_ID = 'hypernova'; |
| 61 | + |
| 62 | +const components = { |
| 63 | + 'Example': ExampleComponent |
| 64 | +} |
| 65 | + |
| 66 | +@NgModule({ |
| 67 | + imports: [ |
| 68 | + ExampleModule, |
| 69 | + BrowserModule.withServerTransition({ appId: APP_ID }), |
| 70 | + ], |
| 71 | + entryComponents: [ExampleComponent] |
| 72 | +}) |
| 73 | +export class AppModule { |
| 74 | + constructor ( |
| 75 | + @Inject('Hypernova.Name') private name: string, |
| 76 | + @Inject('Hypernova.Node') private node: HTMLElement |
| 77 | + ){} |
| 78 | + |
| 79 | + ngDoBootstrap(app) { |
| 80 | + const Component = components[this.name] |
| 81 | + if (Component) { |
| 82 | + return app.bootstrap(Component, this.node) |
| 83 | + } |
| 84 | + }; |
| 85 | +} |
| 86 | +``` |
| 87 | +
|
| 88 | +Use the `components` dictionary to support more components. |
| 89 | +
|
| 90 | +```ts |
| 91 | +const components = { |
| 92 | + [Hypernova.Name]: AngularComponent |
| 93 | +} |
| 94 | +``` |
| 95 | +
|
| 96 | +### Browser JIT |
| 97 | +
|
| 98 | +`browser.main.ts` |
| 99 | +```ts |
| 100 | +import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; |
| 101 | + |
| 102 | +import { AppModule } from './app.module'; |
| 103 | +import { load, loadById, HypernovaModuleFactory } from 'hypernova-angular'; |
| 104 | + |
| 105 | +import { CompilerFactory, Compiler } from '@angular/core'; |
| 106 | + |
| 107 | +const platform = platformBrowserDynamic(); |
| 108 | + |
| 109 | +// Compile module (JIT) |
| 110 | +const compilerFactory: CompilerFactory = platform.injector.get(CompilerFactory); |
| 111 | + |
| 112 | +const compiler: Compiler = compilerFactory.createCompiler([]) |
| 113 | + |
| 114 | +const moduleFactory = compiler.compileModuleSync(AppModule); |
| 115 | + |
| 116 | +const render = (name: string, placeholder: any) => { |
| 117 | + |
| 118 | + // Wrap module factory to provide necessary metadata to boostrap it. |
| 119 | + const hypernovaModuleFactory = new HypernovaModuleFactory(moduleFactory, name, placeholder); |
| 120 | + |
| 121 | + platform.bootstrapModuleFactory(hypernovaModuleFactory); |
| 122 | +} |
| 123 | + |
| 124 | +// Nova Bridge support |
| 125 | +document.addEventListener('NovaMount', (event) => { |
| 126 | + const { name, id } = (<CustomEvent>event).detail; |
| 127 | + |
| 128 | + const placeholder = loadById(name, id); |
| 129 | + |
| 130 | + if (placeholder) { |
| 131 | + render(name, placeholder); |
| 132 | + } |
| 133 | +}) |
| 134 | + |
| 135 | +// Render in placeholders rendered by Hypernova Server |
| 136 | +load('Example').forEach(render.bind(this, 'Example')); |
| 137 | +``` |
| 138 | +
|
| 139 | +### Browser AOT |
| 140 | +
|
| 141 | +`browser.aot.main.ts` |
| 142 | +```ts |
| 143 | +import { platformBrowser } from '@angular/platform-browser'; |
| 144 | +import { AppModuleNgFactory } from './app.module.ngfactory'; |
| 145 | +import { load, loadById, HypernovaModuleFactory } from 'hypernova-angular'; |
| 146 | + |
| 147 | +enableProdMode(); |
| 148 | + |
| 149 | +const render = (name: string, placeholder: any) => { |
| 150 | + const hypernovaModuleFactory = new HypernovaModuleFactory(AppModuleNgFactory, name, placeholder); |
| 151 | + platformBrowser().bootstrapModuleFactory(hypernovaModuleFactory); |
| 152 | +} |
| 153 | + |
| 154 | +document.addEventListener('NovaMount', (event) => { |
| 155 | + const { name, id } = (<CustomEvent>event).detail; |
| 156 | + |
| 157 | + const placeholder = loadById(name, id); |
| 158 | + |
| 159 | + if (placeholder) { |
| 160 | + render(name, placeholder); |
| 161 | + } |
| 162 | +}) |
| 163 | + |
| 164 | +load('Example').forEach(render.bind(this, 'Example')); |
26 | 165 | ``` |
0 commit comments