From c60bd442e89428000bcffd772d843d18b6e8fe7f Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Wed, 12 May 2021 13:56:49 +0200 Subject: [PATCH] Svelte view engine: preprocess dependencies recursively --- src/frontend/SvelteViewEngine.ts | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/frontend/SvelteViewEngine.ts b/src/frontend/SvelteViewEngine.ts index b47cadf..38ae3b6 100644 --- a/src/frontend/SvelteViewEngine.ts +++ b/src/frontend/SvelteViewEngine.ts @@ -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('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 { + private async preprocess(canonicalName: string): Promise { // 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('view.cache')); - } + const code = await this.fileCache.get(file, !config.get('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, }; }