Skip to content

Commit 0291fee

Browse files
committed
refactor: remove bootstrapping responsability to library
1 parent 49d74c3 commit 0291fee

3 files changed

Lines changed: 35 additions & 57 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hypernova-angular",
3-
"version": "1.0.0",
3+
"version": "2.0.0-alpha.0",
44
"description": "Angular Bindings for Hypernova",
55
"main": "lib/index.js",
66
"author": "Felipe Guizar Diaz <felipegaiacharly@gmail.com>",

src/hypernova.module.factory.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { NgModuleFactory, ReflectiveInjector } from '@angular/core';
2+
3+
export const HYPERNOVA_DATA = 'Hypernova.Data';
4+
5+
export class HypernovaModuleFactory extends NgModuleFactory {
6+
constructor(moduleFactory, name, placeholder) {
7+
super();
8+
this.name = name;
9+
this.placeholder = placeholder;
10+
this.moduleFactory = moduleFactory;
11+
this.moduleType = moduleFactory.moduleType;
12+
13+
this.create = (parentInjector) => {
14+
const newInjector = ReflectiveInjector.resolveAndCreate([
15+
{
16+
provide: 'Hypernova.Data',
17+
useValue: this.placeholder.data,
18+
},
19+
{
20+
provide: 'Hypernova.Name',
21+
useValue: this.name,
22+
},
23+
{
24+
provide: 'Hypernova.Node',
25+
useValue: this.placeholder.node,
26+
},
27+
], parentInjector);
28+
29+
return this.moduleFactory.create(newInjector);
30+
};
31+
}
32+
}

src/index.js

Lines changed: 2 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
import hypernova, { load } from 'hypernova';
2-
import { BrowserModule } from '@angular/platform-browser';
3-
4-
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
5-
6-
import { NgModule } from '@angular/core';
7-
81
import { findNode, getData } from 'nova-helpers';
92

103
export { load } from 'hypernova';
114

5+
export { HypernovaModuleFactory, HYPERNOVA_DATA } from './hypernova.module.factory';
6+
127
export const loadById = (name, id) => {
138
const node = findNode(name, id);
149
const data = getData(name, id);
@@ -22,52 +17,3 @@ export const loadById = (name, id) => {
2217

2318
return null;
2419
};
25-
26-
const APP_ID = 'hypernova';
27-
28-
export const HYPERNOVA_DATA = 'Hypernova.Data';
29-
30-
const getBrowserAppModule = (Component, Module, node, propsData) => {
31-
function AppModule() {
32-
this.ngDoBootstrap = (app) => {
33-
app.bootstrap(Component, node);
34-
};
35-
}
36-
return NgModule({
37-
imports: [
38-
Module,
39-
BrowserModule.withServerTransition({ appId: APP_ID }),
40-
],
41-
entryComponents: [Component],
42-
providers: [
43-
{
44-
provide: HYPERNOVA_DATA,
45-
useValue: propsData,
46-
},
47-
],
48-
})(AppModule);
49-
};
50-
51-
export const mountComponent = (Component, Module, node, propsData) => {
52-
const BrowserAppModule = getBrowserAppModule(Component, Module, node, propsData);
53-
54-
platformBrowserDynamic().bootstrapModule(BrowserAppModule);
55-
};
56-
57-
export const renderAngular = (name, Component, Module) => hypernova({
58-
server() {
59-
throw new Error('Use hypernova-angular/server instead');
60-
},
61-
62-
client() {
63-
const payloads = load(name);
64-
if (payloads) {
65-
payloads.forEach((payload) => {
66-
const { node, data: propsData } = payload;
67-
68-
mountComponent(Component, Module, node, propsData);
69-
});
70-
}
71-
return Component;
72-
},
73-
});

0 commit comments

Comments
 (0)