1
- import { BuildContext , BuildState } from './util/interfaces' ;
1
+ import { BuildContext , BuildState , File } from './util/interfaces' ;
2
2
import { Logger } from './logger/logger' ;
3
3
import { getJsOutputDest } from './bundle' ;
4
- import { join , parse , resolve } from 'path' ;
4
+ import { dirname , extname , join , parse , resolve } from 'path' ;
5
5
import { readFileSync , writeFile } from 'fs' ;
6
6
7
7
@@ -20,9 +20,10 @@ export function templateUpdate(event: string, htmlFilePath: string, context: Bui
20
20
let bundleSourceText = readFileSync ( bundleOutputDest , 'utf8' ) ;
21
21
let newTemplateContent = readFileSync ( htmlFilePath , 'utf8' ) ;
22
22
23
- bundleSourceText = replaceBundleJsTemplate ( bundleSourceText , newTemplateContent , htmlFilePath ) ;
23
+ const successfullyUpdated = updateCorrespondingJsFile ( context , newTemplateContent , htmlFilePath ) ;
24
+ bundleSourceText = replaceExistingJsTemplate ( bundleSourceText , newTemplateContent , htmlFilePath ) ;
24
25
25
- if ( bundleSourceText ) {
26
+ if ( successfullyUpdated && bundleSourceText ) {
26
27
// awesome, all good and template updated in the bundle file
27
28
const logger = new Logger ( `template update` ) ;
28
29
logger . setStartTime ( start ) ;
@@ -35,7 +36,7 @@ export function templateUpdate(event: string, htmlFilePath: string, context: Bui
35
36
36
37
} else {
37
38
// congrats, all gud
38
- Logger . debug ( `updateBundledJsTemplate , updated: ${ htmlFilePath } ` ) ;
39
+ Logger . debug ( `templateUpdate , updated: ${ htmlFilePath } ` ) ;
39
40
context . templateState = BuildState . SuccessfulBuild ;
40
41
logger . finish ( ) ;
41
42
resolve ( ) ;
@@ -47,12 +48,23 @@ export function templateUpdate(event: string, htmlFilePath: string, context: Bui
47
48
}
48
49
49
50
} catch ( e ) {
50
- Logger . debug ( `updateBundledJsTemplate error: ${ e } ` ) ;
51
+ Logger . debug ( `templateUpdate error: ${ e } ` ) ;
51
52
failed ( ) ;
52
53
}
53
54
} ) ;
54
55
}
55
56
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
+ }
56
68
57
69
export function inlineTemplate ( sourceText : string , sourcePath : string ) : string {
58
70
const componentDir = parse ( sourcePath ) . dir ;
@@ -105,9 +117,9 @@ export function replaceTemplateUrl(match: TemplateUrlMatch, htmlFilePath: string
105
117
}
106
118
107
119
108
- export function replaceBundleJsTemplate ( bundleSourceText : string , newTemplateContent : string , htmlFilePath : string ) : string {
120
+ export function replaceExistingJsTemplate ( existingSourceText : string , newTemplateContent : string , htmlFilePath : string ) : string {
109
121
let prefix = getTemplatePrefix ( htmlFilePath ) ;
110
- let startIndex = bundleSourceText . indexOf ( prefix ) ;
122
+ let startIndex = existingSourceText . indexOf ( prefix ) ;
111
123
112
124
let isStringified = false ;
113
125
@@ -116,7 +128,7 @@ export function replaceBundleJsTemplate(bundleSourceText: string, newTemplateCon
116
128
isStringified = true ;
117
129
}
118
130
119
- startIndex = bundleSourceText . indexOf ( prefix ) ;
131
+ startIndex = existingSourceText . indexOf ( prefix ) ;
120
132
if ( startIndex === - 1 ) {
121
133
return null ;
122
134
}
@@ -126,24 +138,24 @@ export function replaceBundleJsTemplate(bundleSourceText: string, newTemplateCon
126
138
suffix = stringify ( suffix ) ;
127
139
}
128
140
129
- const endIndex = bundleSourceText . indexOf ( suffix , startIndex + 1 ) ;
141
+ const endIndex = existingSourceText . indexOf ( suffix , startIndex + 1 ) ;
130
142
if ( endIndex === - 1 ) {
131
143
return null ;
132
144
}
133
145
134
- const oldTemplate = bundleSourceText . substring ( startIndex , endIndex + suffix . length ) ;
146
+ const oldTemplate = existingSourceText . substring ( startIndex , endIndex + suffix . length ) ;
135
147
let newTemplate = getTemplateFormat ( htmlFilePath , newTemplateContent ) ;
136
148
137
149
if ( isStringified ) {
138
150
newTemplate = stringify ( newTemplate ) ;
139
151
}
140
152
141
153
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 ) ;
144
156
}
145
157
146
- return bundleSourceText ;
158
+ return existingSourceText ;
147
159
}
148
160
149
161
function stringify ( str : string ) {
0 commit comments