7
7
ScriptTarget
8
8
} from 'typescript' ;
9
9
10
- import { getTypescriptSourceFile , findNodes , replaceImportModuleSpecifier , replaceNamedImport , replaceNode } from '../util/typescript-utils' ;
10
+ import { appendBefore , checkIfFunctionIsCalled , getTypescriptSourceFile , findNodes , insertNamedImportIfNeeded , replaceImportModuleSpecifier , replaceNamedImport , replaceNode } from '../util/typescript-utils' ;
11
11
12
12
export function getFallbackMainContent ( ) {
13
13
return `
@@ -86,6 +86,34 @@ function replaceBootstrapModuleFactory(filePath: string, fileContent: string) {
86
86
return modifiedContent ;
87
87
}
88
88
89
+ function getPlatformBrowserFunctionNode ( filePath : string , fileContent : string ) {
90
+ let modifiedFileContent = fileContent ;
91
+ const sourceFile = getTypescriptSourceFile ( filePath , modifiedFileContent , ScriptTarget . Latest , false ) ;
92
+ const allCalls = findNodes ( sourceFile , sourceFile , SyntaxKind . CallExpression , true ) as CallExpression [ ] ;
93
+ const callsToPlatformBrowser = allCalls . filter ( call => call . expression && call . expression . kind === SyntaxKind . Identifier && ( call . expression as Identifier ) . text === 'platformBrowser' ) ;
94
+ const toAppend = `enableProdMode();\n` ;
95
+ if ( callsToPlatformBrowser . length ) {
96
+ modifiedFileContent = appendBefore ( filePath , modifiedFileContent , callsToPlatformBrowser [ 0 ] . expression , toAppend ) ;
97
+ } else {
98
+ // just throw it at the bottom
99
+ modifiedFileContent + toAppend ;
100
+ }
101
+ return modifiedFileContent ;
102
+ }
103
+
104
+ function importAndEnableProdMode ( filePath : string , fileContent : string ) {
105
+ let modifiedFileContent = fileContent ;
106
+ modifiedFileContent = insertNamedImportIfNeeded ( filePath , modifiedFileContent , 'enableProdMode' , '@angular/core' ) ;
107
+
108
+ const isCalled = checkIfFunctionIsCalled ( filePath , modifiedFileContent , 'enableProdMode' ) ;
109
+ if ( ! isCalled ) {
110
+ // go ahead and insert this
111
+ modifiedFileContent = getPlatformBrowserFunctionNode ( filePath , modifiedFileContent ) ;
112
+ }
113
+
114
+ return modifiedFileContent ;
115
+ }
116
+
89
117
export function replaceBootstrap ( filePath : string , fileContent : string , appNgModulePath : string , appNgModuleClassName : string ) {
90
118
if ( ! fileContent . match ( / \b b o o t s t r a p M o d u l e \b / ) ) {
91
119
throw new Error ( `Could not find bootstrapModule in ${ filePath } ` ) ;
@@ -103,7 +131,6 @@ export function replaceBootstrap(filePath: string, fileContent: string, appNgMod
103
131
104
132
let modifiedFileContent = fileContent ;
105
133
modifiedFileContent = replaceNgModuleClassName ( filePath , modifiedFileContent , appNgModuleClassName ) ;
106
-
107
134
modifiedFileContent = replacePlatformBrowser ( filePath , modifiedFileContent ) ;
108
135
modifiedFileContent = replaceBootstrapModuleFactory ( filePath , modifiedFileContent ) ;
109
136
@@ -112,5 +139,8 @@ export function replaceBootstrap(filePath: string, fileContent: string, appNgMod
112
139
modifiedFileContent = replaceImportModuleSpecifier ( filePath , modifiedFileContent , '@angular/platform-browser-dynamic' , '@angular/platform-browser' ) ;
113
140
modifiedFileContent = replaceImportModuleSpecifier ( filePath , modifiedFileContent , originalImport , ngFactryImport ) ;
114
141
142
+ // check if prod mode is imported and enabled
143
+ modifiedFileContent = importAndEnableProdMode ( filePath , modifiedFileContent ) ;
144
+
115
145
return modifiedFileContent ;
116
146
}
0 commit comments