Skip to content
This repository was archived by the owner on May 1, 2020. It is now read-only.

Commit b085223

Browse files
committed
refactor(template): support looking up multiple in-memory bundles for inlining templates
1 parent b07a505 commit b085223

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

src/template.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,36 @@
1+
import { readFileSync, writeFileSync } from 'fs';
2+
import { dirname, extname, join, parse, resolve } from 'path';
3+
14
import { BuildContext, BuildState, ChangedFile, File } from './util/interfaces';
25
import { changeExtension } from './util/helpers';
36
import { Logger } from './logger/logger';
4-
import { getJsOutputDest } from './bundle';
57
import { invalidateCache } from './rollup';
6-
import { dirname, extname, join, parse, resolve } from 'path';
7-
import { readFileSync, writeFileSync } from 'fs';
8+
89

910

1011
export function templateUpdate(changedFiles: ChangedFile[], context: BuildContext) {
1112
try {
1213
const changedTemplates = changedFiles.filter(changedFile => changedFile.ext === '.html');
1314
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');
1617

1718
// update the corresponding transpiled javascript file with the template changed (inline it)
1819
// as well as the bundle
1920
for (const changedTemplateFile of changedTemplates) {
2021
const file = context.fileCache.get(changedTemplateFile.filePath);
21-
if (! updateCorrespondingJsFile(context, file.content, changedTemplateFile.filePath)) {
22+
if (!updateCorrespondingJsFile(context, file.content, changedTemplateFile.filePath)) {
2223
throw new Error(`Failed to inline template ${changedTemplateFile.filePath}`);
2324
}
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+
}
2534
}
2635

2736
// invaldiate any rollup bundles, if they're not using rollup no harm done
@@ -31,16 +40,14 @@ export function templateUpdate(changedFiles: ChangedFile[], context: BuildContex
3140
const logger = new Logger(`template update`);
3241
logger.setStartTime(start);
3342

34-
writeFileSync(bundleOutputDest, bundleSourceText, { encoding: 'utf8'});
35-
36-
// congrats, all gud
43+
// congrats, all good
3744
changedTemplates.forEach(changedTemplate => {
3845
Logger.debug(`templateUpdate, updated: ${changedTemplate.filePath}`);
3946
});
4047

4148
context.templateState = BuildState.SuccessfulBuild;
4249
logger.finish();
43-
resolve();
50+
return Promise.resolve();
4451

4552
} catch (ex) {
4653
Logger.debug(`templateUpdate error: ${ex.message}`);

0 commit comments

Comments
 (0)