|
| 1 | +import hypernova, { serialize, load } from 'hypernova'; |
| 2 | +import { first } from 'rxjs/operators'; |
| 3 | +import { BrowserModule } from '@angular/platform-browser'; |
| 4 | +import { |
| 5 | + ServerModule, |
| 6 | + platformDynamicServer, |
| 7 | + INITIAL_CONFIG, |
| 8 | + PlatformState, |
| 9 | +} from '@angular/platform-server'; |
| 10 | + |
| 11 | +import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; |
| 12 | + |
| 13 | +import { NgModule, ApplicationRef } from '@angular/core'; |
| 14 | + |
| 15 | +const APP_ID = 'hypernova'; |
| 16 | + |
| 17 | +export const HYPERNOVA_DATA = 'Hypernova.Data'; |
| 18 | + |
| 19 | +const getServerAppModule = (Component, Module) => { |
| 20 | + function AppModule() { |
| 21 | + this.ngDoBootstrap = (app) => { |
| 22 | + app.bootstrap(Component, `#${APP_ID}`); |
| 23 | + }; |
| 24 | + } |
| 25 | + return NgModule({ |
| 26 | + imports: [ |
| 27 | + Module, |
| 28 | + BrowserModule, |
| 29 | + ServerModule, |
| 30 | + ], |
| 31 | + entryComponents: [Component], |
| 32 | + })(AppModule); |
| 33 | +}; |
| 34 | + |
| 35 | +const getBrowserAppModule = (Component, Module, node) => { |
| 36 | + function AppModule() { |
| 37 | + this.ngDoBootstrap = (app) => { |
| 38 | + app.bootstrap(Component, node); |
| 39 | + }; |
| 40 | + } |
| 41 | + return NgModule({ |
| 42 | + imports: [ |
| 43 | + Module, |
| 44 | + BrowserModule.withServerTransition({ appId: APP_ID }), |
| 45 | + ], |
| 46 | + entryComponents: [Component], |
| 47 | + })(AppModule); |
| 48 | +}; |
| 49 | + |
| 50 | +const renderServer = (ServerAppModule, propsData) => { |
| 51 | + const platform = platformDynamicServer([ |
| 52 | + { |
| 53 | + provide: INITIAL_CONFIG, |
| 54 | + useValue: { |
| 55 | + document: `<div id="${APP_ID}"></div>`, |
| 56 | + }, |
| 57 | + }, |
| 58 | + { |
| 59 | + provide: HYPERNOVA_DATA, |
| 60 | + useValue: propsData, |
| 61 | + }, |
| 62 | + ]); |
| 63 | + |
| 64 | + return platform.bootstrapModule(ServerAppModule) |
| 65 | + .then((moduleRef) => { |
| 66 | + const applicationRef = moduleRef.injector.get(ApplicationRef); |
| 67 | + return applicationRef.isStable.pipe(first(isStable => isStable)) |
| 68 | + .toPromise() |
| 69 | + .then(() => { |
| 70 | + const platformState = platform.injector.get(PlatformState); |
| 71 | + |
| 72 | + platform.destroy(); |
| 73 | + return platformState.getDocument().getElementById(APP_ID).serialize(); |
| 74 | + }); |
| 75 | + }); |
| 76 | +}; |
| 77 | + |
| 78 | +export const renderAngular = (name, Component, Module) => hypernova({ |
| 79 | + server() { |
| 80 | + return async (propsData) => { |
| 81 | + const ServerAppModule = getServerAppModule(Component, Module); |
| 82 | + |
| 83 | + const html = await renderServer(ServerAppModule, propsData); |
| 84 | + |
| 85 | + return serialize(name, html, propsData); |
| 86 | + }; |
| 87 | + }, |
| 88 | + |
| 89 | + client() { |
| 90 | + const payloads = load(name); |
| 91 | + if (payloads) { |
| 92 | + payloads.forEach((payload) => { |
| 93 | + const { node, data: propsData } = payload; |
| 94 | + |
| 95 | + const BrowserAppModule = getBrowserAppModule(Component, Module, node); |
| 96 | + |
| 97 | + platformBrowserDynamic([ |
| 98 | + { |
| 99 | + provide: HYPERNOVA_DATA, |
| 100 | + useValue: propsData, |
| 101 | + }, |
| 102 | + ]).bootstrapModule(BrowserAppModule); |
| 103 | + }); |
| 104 | + } |
| 105 | + return Component; |
| 106 | + }, |
| 107 | +}); |
0 commit comments