Skip to content

Commit d0c2ed5

Browse files
committed
refactor: separate entry points for server and browser
1 parent 2b83b1c commit d0c2ed5

3 files changed

Lines changed: 82 additions & 60 deletions

File tree

server.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./lib/server.js');

src/index.js

Lines changed: 3 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
1-
import hypernova, { serialize, load } from 'hypernova';
2-
import { first } from 'rxjs/operators';
1+
import hypernova, { load } from 'hypernova';
32
import { BrowserModule } from '@angular/platform-browser';
4-
import {
5-
ServerModule,
6-
platformDynamicServer,
7-
INITIAL_CONFIG,
8-
PlatformState,
9-
} from '@angular/platform-server';
103

114
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
125

13-
import { NgModule, ApplicationRef } from '@angular/core';
6+
import { NgModule } from '@angular/core';
147

158
import { findNode, getData } from 'nova-helpers';
169

@@ -34,22 +27,6 @@ const APP_ID = 'hypernova';
3427

3528
export const HYPERNOVA_DATA = 'Hypernova.Data';
3629

37-
const getServerAppModule = (Component, Module) => {
38-
function AppModule() {
39-
this.ngDoBootstrap = (app) => {
40-
app.bootstrap(Component, `#${APP_ID}`);
41-
};
42-
}
43-
return NgModule({
44-
imports: [
45-
Module,
46-
BrowserModule,
47-
ServerModule,
48-
],
49-
entryComponents: [Component],
50-
})(AppModule);
51-
};
52-
5330
const getBrowserAppModule = (Component, Module, node, propsData) => {
5431
function AppModule() {
5532
this.ngDoBootstrap = (app) => {
@@ -71,34 +48,6 @@ const getBrowserAppModule = (Component, Module, node, propsData) => {
7148
})(AppModule);
7249
};
7350

74-
const renderServer = (ServerAppModule, propsData) => {
75-
const platform = platformDynamicServer([
76-
{
77-
provide: INITIAL_CONFIG,
78-
useValue: {
79-
document: `<div id="${APP_ID}"></div>`,
80-
},
81-
},
82-
{
83-
provide: HYPERNOVA_DATA,
84-
useValue: propsData,
85-
},
86-
]);
87-
88-
return platform.bootstrapModule(ServerAppModule)
89-
.then((moduleRef) => {
90-
const applicationRef = moduleRef.injector.get(ApplicationRef);
91-
return applicationRef.isStable.pipe(first(isStable => isStable))
92-
.toPromise()
93-
.then(() => {
94-
const platformState = platform.injector.get(PlatformState);
95-
96-
platform.destroy();
97-
return platformState.getDocument().getElementById(APP_ID).serialize();
98-
});
99-
});
100-
};
101-
10251
export const mountComponent = (Component, Module, node, propsData) => {
10352
const BrowserAppModule = getBrowserAppModule(Component, Module, node, propsData);
10453

@@ -107,13 +56,7 @@ export const mountComponent = (Component, Module, node, propsData) => {
10756

10857
export const renderAngular = (name, Component, Module) => hypernova({
10958
server() {
110-
return async (propsData) => {
111-
const ServerAppModule = getServerAppModule(Component, Module);
112-
113-
const html = await renderServer(ServerAppModule, propsData);
114-
115-
return serialize(name, html, propsData);
116-
};
59+
throw new Error('Use hypernova-angular/server instead');
11760
},
11861

11962
client() {

src/server.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import hypernova, { serialize } from 'hypernova';
2+
3+
import {
4+
ServerModule,
5+
platformDynamicServer,
6+
INITIAL_CONFIG,
7+
PlatformState,
8+
} from '@angular/platform-server';
9+
10+
import { BrowserModule } from '@angular/platform-browser';
11+
12+
import { NgModule, ApplicationRef } from '@angular/core';
13+
14+
import { first } from 'rxjs/operators';
15+
16+
const APP_ID = 'hypernova';
17+
18+
export const HYPERNOVA_DATA = 'Hypernova.Data';
19+
20+
const getServerAppModule = (Component, Module) => {
21+
function AppModule() {
22+
this.ngDoBootstrap = (app) => {
23+
app.bootstrap(Component, `#${APP_ID}`);
24+
};
25+
}
26+
return NgModule({
27+
imports: [
28+
Module,
29+
BrowserModule,
30+
ServerModule,
31+
],
32+
entryComponents: [Component],
33+
})(AppModule);
34+
};
35+
36+
const renderServer = (ServerAppModule, propsData) => {
37+
const platform = platformDynamicServer([
38+
{
39+
provide: INITIAL_CONFIG,
40+
useValue: {
41+
document: `<div id="${APP_ID}"></div>`,
42+
},
43+
},
44+
{
45+
provide: HYPERNOVA_DATA,
46+
useValue: propsData,
47+
},
48+
]);
49+
50+
return platform.bootstrapModule(ServerAppModule)
51+
.then((moduleRef) => {
52+
const applicationRef = moduleRef.injector.get(ApplicationRef);
53+
return applicationRef.isStable.pipe(first(isStable => isStable))
54+
.toPromise()
55+
.then(() => {
56+
const platformState = platform.injector.get(PlatformState);
57+
58+
platform.destroy();
59+
return platformState.getDocument().getElementById(APP_ID).serialize();
60+
});
61+
});
62+
};
63+
64+
export const renderAngular = (name, Component, Module) => hypernova({
65+
server() {
66+
return async (propsData) => {
67+
const ServerAppModule = getServerAppModule(Component, Module);
68+
69+
const html = await renderServer(ServerAppModule, propsData);
70+
71+
return serialize(name, html, propsData);
72+
};
73+
},
74+
75+
client() {
76+
throw new Error('Use hypernova-angular instead');
77+
},
78+
});

0 commit comments

Comments
 (0)