Svelte view engine: preprocess dependencies recursively

This commit is contained in:
Alice Gaudon 2021-05-12 13:56:49 +02:00
parent e851630be4
commit c60bd442e8

View File

@ -99,14 +99,10 @@ export default class SvelteViewEngine extends ViewEngine {
const intermediateFile = path.join(this.targetDir, canonicalName);
logger.info(canonicalName + ' > ', 'Pre-compiling', file, '->', intermediateFile);
const source = await this.fileCache.get(file, !config.get<boolean>('view.cache'));
const allBackendCalls: string[] = [];
for (const dependency of this.resolveDependencies(source, canonicalName)) {
allBackendCalls.push(...(await this.preprocess(dependency)).backendCalls);
}
const {backendCalls, code} = await this.preprocess(canonicalName, source);
const {backendCalls, code} = await this.preprocess(canonicalName);
allBackendCalls.push(...backendCalls);
// Server Side Render (initial HTML and CSS, no-js)
@ -155,7 +151,7 @@ export default class SvelteViewEngine extends ViewEngine {
return dependencies;
}
private async preprocess(canonicalName: string, code?: string): Promise<PreprocessingCacheEntry> {
private async preprocess(canonicalName: string): Promise<PreprocessingCacheEntry> {
// Cache
if (Object.keys(this.preprocessingCache).indexOf(canonicalName) >= 0) {
return this.preprocessingCache[canonicalName];
@ -169,9 +165,7 @@ export default class SvelteViewEngine extends ViewEngine {
await fs.mkdir(path.dirname(outputFile), {recursive: true});
// Read source file if code was not already provided
if (!code) {
code = await this.fileCache.get(file, !config.get<boolean>('view.cache'));
}
const code = await this.fileCache.get(file, !config.get<boolean>('view.cache'));
// Replace backend calls
const replacedBackendCalls = await this.replaceBackendCalls(canonicalName, code);
@ -193,8 +187,14 @@ export default class SvelteViewEngine extends ViewEngine {
// Write to output file
await fs.writeFile(outputFile, preprocessed.code);
// Preprocess dependencies
const backendCalls: string[] = replacedBackendCalls.backendCalls;
for (const dependency of this.resolveDependencies(code, canonicalName)) {
backendCalls.push(...(await this.preprocess(dependency)).backendCalls);
}
return this.preprocessingCache[canonicalName] = {
backendCalls: replacedBackendCalls.backendCalls,
backendCalls: backendCalls,
code: preprocessed.code,
};
}