5
5
* Use of this source code is governed by an MIT-style license that can be
6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
- import { DOCUMENT } from '@angular/common' ;
9
- import { ResourceLoader } from '@angular/compiler' ;
10
- import { Compiler , CompilerFactory , NgModuleFactory , StaticProvider , Type } from '@angular/core' ;
11
- import { platformDynamicServer } from '@angular/platform-server' ;
8
+ import { StaticProvider } from '@angular/core' ;
12
9
13
10
import { ORIGIN_URL , REQUEST } from '@nguniversal/aspnetcore-engine/tokens' ;
14
- import { ɵFileLoader } from '@nguniversal/common/engine' ;
11
+ import { ɵCommonEngine as CommonEngine , ɵRenderOptions as RenderOptions } from '@nguniversal/common/engine' ;
12
+ import { createDocument } from 'domino' ;
15
13
import { IEngineOptions } from './interfaces/engine-options' ;
16
14
import { IEngineRenderResult } from './interfaces/engine-render-result' ;
17
- import { renderModuleFactory } from './platform-server-utils' ;
18
15
19
16
/* @internal */
20
- function _getUniversalData ( doc : Document , appSelector : string ) : Omit < IEngineRenderResult , 'moduleRef' > {
17
+ function _getUniversalData ( content : string , appSelector : string ) : IEngineRenderResult {
18
+ const doc = createDocument ( content , true ) ;
21
19
22
20
const styles : string [ ] = [ ] ;
23
21
const scripts : string [ ] = [ ] ;
@@ -63,6 +61,7 @@ function _getUniversalData(doc: Document, appSelector: string): Omit<IEngineRend
63
61
} ;
64
62
}
65
63
64
+ const commonEngine = new CommonEngine ( ) ;
66
65
export async function ngAspnetCoreEngine ( options : Readonly < IEngineOptions > )
67
66
: Promise < IEngineRenderResult > {
68
67
if ( ! options . appSelector ) {
@@ -71,41 +70,20 @@ export async function ngAspnetCoreEngine(options: Readonly<IEngineOptions>)
71
70
for your root App component.` ) ;
72
71
}
73
72
74
- // Grab the DOM "selector" from the passed in Template <app-root> for example = "app-root"
75
- const appSelector = options . appSelector . substring ( 1 , options . appSelector . indexOf ( '>' ) ) ;
76
-
77
- const compilerFactory : CompilerFactory = platformDynamicServer ( ) . injector . get ( CompilerFactory ) ;
78
- const compiler : Compiler = compilerFactory . createCompiler ( [
79
- {
80
- providers : [
81
- { provide : ResourceLoader , useClass : ɵFileLoader , deps : [ ] }
82
- ]
83
- }
84
- ] ) ;
85
-
86
- const moduleOrFactory = options . ngModule ;
87
- if ( ! moduleOrFactory ) {
88
- throw new Error ( 'You must pass in a NgModule or NgModuleFactory to be bootstrapped' ) ;
89
- }
90
-
91
- const extraProviders = [
92
- ...( options . providers || [ ] ) ,
93
- getReqResProviders ( options . request . origin , options . request . data . request ) ,
94
- ] ;
95
73
96
- const factory = await getFactory ( moduleOrFactory , compiler ) ;
97
- const result = await renderModuleFactory ( factory , {
98
- document : options . document || options . appSelector ,
74
+ const renderOptions : RenderOptions = {
99
75
url : options . url || options . request . absoluteUrl ,
100
- extraProviders,
101
- } ) ;
76
+ document : options . document || options . appSelector ,
77
+ providers : [ ...( options . providers || [ ] ) , getReqResProviders ( options . request . origin , options . request . data . request ) ] ,
78
+ bootstrap : options . ngModule ,
79
+ } ;
102
80
103
- const doc = result . moduleRef . injector . get ( DOCUMENT ) ;
104
81
105
- return {
106
- moduleRef : result . moduleRef ,
107
- ..._getUniversalData ( doc , appSelector ) ,
108
- } ;
82
+ // Grab the DOM "selector" from the passed in Template <app-root> for example = "app-root"
83
+ const appSelector = options . appSelector . substring ( 1 , options . appSelector . indexOf ( '>' ) ) ;
84
+ const html = await commonEngine . render ( renderOptions ) ;
85
+
86
+ return _getUniversalData ( html , appSelector ) ;
109
87
}
110
88
111
89
/**
@@ -126,25 +104,3 @@ function getReqResProviders(origin: string, request: string): StaticProvider[] {
126
104
return providers ;
127
105
}
128
106
129
- /* @internal */
130
- const factoryCacheMap = new Map < Type < { } > , NgModuleFactory < { } > > ( ) ;
131
- async function getFactory (
132
- moduleOrFactory : Type < { } > | NgModuleFactory < { } > , compiler : Compiler
133
- ) : Promise < NgModuleFactory < { } > > {
134
- // If module has been compiled AoT
135
- if ( moduleOrFactory instanceof NgModuleFactory ) {
136
- return moduleOrFactory ;
137
- } else {
138
- const moduleFactory = factoryCacheMap . get ( moduleOrFactory ) ;
139
- // If module factory is cached
140
- if ( moduleFactory ) {
141
- return moduleFactory ;
142
- }
143
-
144
- // Compile the module and cache it
145
- const factory = await compiler . compileModuleAsync ( moduleOrFactory ) ;
146
- factoryCacheMap . set ( moduleOrFactory , factory ) ;
147
-
148
- return factory ;
149
- }
150
- }
0 commit comments