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