@@ -4,9 +4,25 @@ import {AotPlugin} from './plugin';
4
4
import { TypeScriptFileRefactor } from './refactor' ;
5
5
import { LoaderContext , ModuleReason } from './webpack' ;
6
6
7
+ interface Platform {
8
+ name : string ;
9
+ importLocation : string ;
10
+ }
11
+
7
12
const loaderUtils = require ( 'loader-utils' ) ;
8
13
const NormalModule = require ( 'webpack/lib/NormalModule' ) ;
9
14
15
+ // This is a map of changes which need to be made
16
+ const changeMap : { [ key : string ] : Platform } = {
17
+ platformBrowserDynamic : {
18
+ name : 'platformBrowser' ,
19
+ importLocation : '@angular/platform-browser'
20
+ } ,
21
+ platformDynamicServer : {
22
+ name : 'platformServer' ,
23
+ importLocation : '@angular/platform-server'
24
+ }
25
+ } ;
10
26
11
27
function _getContentOfKeyLiteral ( _source : ts . SourceFile , node : ts . Node ) : string {
12
28
if ( ! node ) {
@@ -205,9 +221,10 @@ function _replaceBootstrap(plugin: AotPlugin, refactor: TypeScriptFileRefactor)
205
221
= refactor . findAstNodes ( access , ts . SyntaxKind . CallExpression , true ) as ts . CallExpression [ ] ;
206
222
return previous . concat ( expressions ) ;
207
223
} , [ ] )
224
+ . filter ( ( call : ts . CallExpression ) => call . expression . kind == ts . SyntaxKind . Identifier )
208
225
. filter ( ( call : ts . CallExpression ) => {
209
- return call . expression . kind == ts . SyntaxKind . Identifier
210
- && ( call . expression as ts . Identifier ) . text == 'platformBrowserDynamic' ;
226
+ // Find if the expression matches one of the replacement targets
227
+ return ! ! changeMap [ ( call . expression as ts . Identifier ) . text ] ;
211
228
} ) ;
212
229
213
230
if ( calls . length == 0 ) {
@@ -222,15 +239,22 @@ function _replaceBootstrap(plugin: AotPlugin, refactor: TypeScriptFileRefactor)
222
239
refactor . replaceNode ( call . arguments [ 0 ] , entryModule . className + 'NgFactory' ) ;
223
240
} ) ;
224
241
225
- calls . forEach ( call => refactor . replaceNode ( call . expression , 'platformBrowser' ) ) ;
242
+ calls . forEach ( call => {
243
+ const platform = changeMap [ ( call . expression as ts . Identifier ) . text ] ;
244
+
245
+ // Replace with mapped replacement
246
+ refactor . replaceNode ( call . expression , platform . name ) ;
247
+
248
+ // Add the appropriate import
249
+ refactor . insertImport ( platform . name , platform . importLocation ) ;
250
+ } ) ;
226
251
227
252
bootstraps
228
253
. forEach ( ( bs : ts . PropertyAccessExpression ) => {
229
254
// This changes the call.
230
255
refactor . replaceNode ( bs . name , 'bootstrapModuleFactory' ) ;
231
256
} ) ;
232
257
233
- refactor . insertImport ( 'platformBrowser' , '@angular/platform-browser' ) ;
234
258
refactor . insertImport ( entryModule . className + 'NgFactory' , ngFactoryPath ) ;
235
259
}
236
260
0 commit comments