1- import { BuildContext , BuildState } from './util/interfaces' ;
1+ import { BuildContext , BuildState , File } from './util/interfaces' ;
22import { Logger } from './logger/logger' ;
33import { getJsOutputDest } from './bundle' ;
4- import { join , parse , resolve } from 'path' ;
4+ import { dirname , extname , join , parse , resolve } from 'path' ;
55import { readFileSync , writeFile } from 'fs' ;
66
77
@@ -20,9 +20,10 @@ export function templateUpdate(event: string, htmlFilePath: string, context: Bui
2020 let bundleSourceText = readFileSync ( bundleOutputDest , 'utf8' ) ;
2121 let newTemplateContent = readFileSync ( htmlFilePath , 'utf8' ) ;
2222
23- bundleSourceText = replaceBundleJsTemplate ( bundleSourceText , newTemplateContent , htmlFilePath ) ;
23+ const successfullyUpdated = updateCorrespondingJsFile ( context , newTemplateContent , htmlFilePath ) ;
24+ bundleSourceText = replaceExistingJsTemplate ( bundleSourceText , newTemplateContent , htmlFilePath ) ;
2425
25- if ( bundleSourceText ) {
26+ if ( successfullyUpdated && bundleSourceText ) {
2627 // awesome, all good and template updated in the bundle file
2728 const logger = new Logger ( `template update` ) ;
2829 logger . setStartTime ( start ) ;
@@ -35,7 +36,7 @@ export function templateUpdate(event: string, htmlFilePath: string, context: Bui
3536
3637 } else {
3738 // congrats, all gud
38- Logger . debug ( `updateBundledJsTemplate , updated: ${ htmlFilePath } ` ) ;
39+ Logger . debug ( `templateUpdate , updated: ${ htmlFilePath } ` ) ;
3940 context . templateState = BuildState . SuccessfulBuild ;
4041 logger . finish ( ) ;
4142 resolve ( ) ;
@@ -47,12 +48,23 @@ export function templateUpdate(event: string, htmlFilePath: string, context: Bui
4748 }
4849
4950 } catch ( e ) {
50- Logger . debug ( `updateBundledJsTemplate error: ${ e } ` ) ;
51+ Logger . debug ( `templateUpdate error: ${ e } ` ) ;
5152 failed ( ) ;
5253 }
5354 } ) ;
5455}
5556
57+ function updateCorrespondingJsFile ( context : BuildContext , newTemplateContent : string , existingHtmlTemplatePath : string ) {
58+ const javascriptFiles = context . fileCache . getAll ( ) . filter ( ( file : File ) => dirname ( file . path ) === dirname ( existingHtmlTemplatePath ) && extname ( file . path ) === '.js' ) ;
59+ for ( const javascriptFile of javascriptFiles ) {
60+ const newContent = replaceExistingJsTemplate ( javascriptFile . content , newTemplateContent , existingHtmlTemplatePath ) ;
61+ if ( newContent !== javascriptFile . content ) {
62+ javascriptFile . content = newContent ;
63+ return true ;
64+ }
65+ }
66+ return false ;
67+ }
5668
5769export function inlineTemplate ( sourceText : string , sourcePath : string ) : string {
5870 const componentDir = parse ( sourcePath ) . dir ;
@@ -105,9 +117,9 @@ export function replaceTemplateUrl(match: TemplateUrlMatch, htmlFilePath: string
105117}
106118
107119
108- export function replaceBundleJsTemplate ( bundleSourceText : string , newTemplateContent : string , htmlFilePath : string ) : string {
120+ export function replaceExistingJsTemplate ( existingSourceText : string , newTemplateContent : string , htmlFilePath : string ) : string {
109121 let prefix = getTemplatePrefix ( htmlFilePath ) ;
110- let startIndex = bundleSourceText . indexOf ( prefix ) ;
122+ let startIndex = existingSourceText . indexOf ( prefix ) ;
111123
112124 let isStringified = false ;
113125
@@ -116,7 +128,7 @@ export function replaceBundleJsTemplate(bundleSourceText: string, newTemplateCon
116128 isStringified = true ;
117129 }
118130
119- startIndex = bundleSourceText . indexOf ( prefix ) ;
131+ startIndex = existingSourceText . indexOf ( prefix ) ;
120132 if ( startIndex === - 1 ) {
121133 return null ;
122134 }
@@ -126,24 +138,24 @@ export function replaceBundleJsTemplate(bundleSourceText: string, newTemplateCon
126138 suffix = stringify ( suffix ) ;
127139 }
128140
129- const endIndex = bundleSourceText . indexOf ( suffix , startIndex + 1 ) ;
141+ const endIndex = existingSourceText . indexOf ( suffix , startIndex + 1 ) ;
130142 if ( endIndex === - 1 ) {
131143 return null ;
132144 }
133145
134- const oldTemplate = bundleSourceText . substring ( startIndex , endIndex + suffix . length ) ;
146+ const oldTemplate = existingSourceText . substring ( startIndex , endIndex + suffix . length ) ;
135147 let newTemplate = getTemplateFormat ( htmlFilePath , newTemplateContent ) ;
136148
137149 if ( isStringified ) {
138150 newTemplate = stringify ( newTemplate ) ;
139151 }
140152
141153 let lastChange : string = null ;
142- while ( bundleSourceText . indexOf ( oldTemplate ) > - 1 && bundleSourceText !== lastChange ) {
143- lastChange = bundleSourceText = bundleSourceText . replace ( oldTemplate , newTemplate ) ;
154+ while ( existingSourceText . indexOf ( oldTemplate ) > - 1 && existingSourceText !== lastChange ) {
155+ lastChange = existingSourceText = existingSourceText . replace ( oldTemplate , newTemplate ) ;
144156 }
145157
146- return bundleSourceText ;
158+ return existingSourceText ;
147159}
148160
149161function stringify ( str : string ) {
0 commit comments