77 ScriptTarget
88} from 'typescript' ;
99
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' ;
1111
1212export function getFallbackMainContent ( ) {
1313 return `
@@ -86,6 +86,34 @@ function replaceBootstrapModuleFactory(filePath: string, fileContent: string) {
8686 return modifiedContent ;
8787}
8888
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+
89117export function replaceBootstrap ( filePath : string , fileContent : string , appNgModulePath : string , appNgModuleClassName : string ) {
90118 if ( ! fileContent . match ( / \b b o o t s t r a p M o d u l e \b / ) ) {
91119 throw new Error ( `Could not find bootstrapModule in ${ filePath } ` ) ;
@@ -103,7 +131,6 @@ export function replaceBootstrap(filePath: string, fileContent: string, appNgMod
103131
104132 let modifiedFileContent = fileContent ;
105133 modifiedFileContent = replaceNgModuleClassName ( filePath , modifiedFileContent , appNgModuleClassName ) ;
106-
107134 modifiedFileContent = replacePlatformBrowser ( filePath , modifiedFileContent ) ;
108135 modifiedFileContent = replaceBootstrapModuleFactory ( filePath , modifiedFileContent ) ;
109136
@@ -112,5 +139,8 @@ export function replaceBootstrap(filePath: string, fileContent: string, appNgMod
112139 modifiedFileContent = replaceImportModuleSpecifier ( filePath , modifiedFileContent , '@angular/platform-browser-dynamic' , '@angular/platform-browser' ) ;
113140 modifiedFileContent = replaceImportModuleSpecifier ( filePath , modifiedFileContent , originalImport , ngFactryImport ) ;
114141
142+ // check if prod mode is imported and enabled
143+ modifiedFileContent = importAndEnableProdMode ( filePath , modifiedFileContent ) ;
144+
115145 return modifiedFileContent ;
116146}
0 commit comments