1
+ import { readFileSync , writeFileSync } from 'fs' ;
2
+ import { dirname , extname , join , parse , resolve } from 'path' ;
3
+
1
4
import { BuildContext , BuildState , ChangedFile , File } from './util/interfaces' ;
2
5
import { changeExtension } from './util/helpers' ;
3
6
import { Logger } from './logger/logger' ;
4
- import { getJsOutputDest } from './bundle' ;
5
7
import { invalidateCache } from './rollup' ;
6
- import { dirname , extname , join , parse , resolve } from 'path' ;
7
- import { readFileSync , writeFileSync } from 'fs' ;
8
+
8
9
9
10
10
11
export function templateUpdate ( changedFiles : ChangedFile [ ] , context : BuildContext ) {
11
12
try {
12
13
const changedTemplates = changedFiles . filter ( changedFile => changedFile . ext === '.html' ) ;
13
14
const start = Date . now ( ) ;
14
- const bundleOutputDest = getJsOutputDest ( context ) ;
15
- let bundleSourceText = readFileSync ( bundleOutputDest , 'utf8 ') ;
15
+
16
+ const bundleFiles = context . fileCache . getAll ( ) . filter ( file => file . path . indexOf ( context . buildDir ) >= 0 && extname ( file . path ) === '.js ') ;
16
17
17
18
// update the corresponding transpiled javascript file with the template changed (inline it)
18
19
// as well as the bundle
19
20
for ( const changedTemplateFile of changedTemplates ) {
20
21
const file = context . fileCache . get ( changedTemplateFile . filePath ) ;
21
- if ( ! updateCorrespondingJsFile ( context , file . content , changedTemplateFile . filePath ) ) {
22
+ if ( ! updateCorrespondingJsFile ( context , file . content , changedTemplateFile . filePath ) ) {
22
23
throw new Error ( `Failed to inline template ${ changedTemplateFile . filePath } ` ) ;
23
24
}
24
- bundleSourceText = replaceExistingJsTemplate ( bundleSourceText , file . content , changedTemplateFile . filePath ) ;
25
+ // find the corresponding bundle
26
+ for ( const bundleFile of bundleFiles ) {
27
+ const newContent = replaceExistingJsTemplate ( bundleFile . content , file . content , changedTemplateFile . filePath ) ;
28
+ if ( newContent && newContent !== bundleFile . content ) {
29
+ context . fileCache . set ( bundleFile . path , { path : bundleFile . path , content : newContent } ) ;
30
+ writeFileSync ( bundleFile . path , newContent ) ;
31
+ break ;
32
+ }
33
+ }
25
34
}
26
35
27
36
// invaldiate any rollup bundles, if they're not using rollup no harm done
@@ -31,16 +40,14 @@ export function templateUpdate(changedFiles: ChangedFile[], context: BuildContex
31
40
const logger = new Logger ( `template update` ) ;
32
41
logger . setStartTime ( start ) ;
33
42
34
- writeFileSync ( bundleOutputDest , bundleSourceText , { encoding : 'utf8' } ) ;
35
-
36
- // congrats, all gud
43
+ // congrats, all good
37
44
changedTemplates . forEach ( changedTemplate => {
38
45
Logger . debug ( `templateUpdate, updated: ${ changedTemplate . filePath } ` ) ;
39
46
} ) ;
40
47
41
48
context . templateState = BuildState . SuccessfulBuild ;
42
49
logger . finish ( ) ;
43
- resolve ( ) ;
50
+ return Promise . resolve ( ) ;
44
51
45
52
} catch ( ex ) {
46
53
Logger . debug ( `templateUpdate error: ${ ex . message } ` ) ;
0 commit comments