Skip to content

Commit

Permalink
feat: fix filepath caching
Browse files Browse the repository at this point in the history
  • Loading branch information
nebrelbug committed May 29, 2023
1 parent 5269d79 commit e715f07
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 42 deletions.
6 changes: 3 additions & 3 deletions src/compile-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ ${config.useWith ? "with(" + config.varName + "||{}){" : ""}
${compileBody.call(this, buffer)}
if (__eta.layout) {
__eta.res = ${
isAsync ? "await includeAsync" : "include"
} (__eta.layout, {body: __eta.res, ...__eta.layoutData});
__eta.res = ${isAsync ? "await includeAsync" : "include"} (__eta.layout, {body: __eta.res, ...${
config.varName
}, ...__eta.layoutData});
}
${config.useWith ? "}" : ""}
Expand Down
34 changes: 23 additions & 11 deletions src/file-handling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ export function readFile(this: EtaCore, path: string): string {
return res;
}

export function resolvePath(this: EtaCore, template: string, options?: Partial<Options>): string {
export function resolvePath(
this: EtaCore,
templatePath: string,
options?: Partial<Options>
): string {
let resolvedFilePath = "";

template += path.extname(template) ? "" : ".eta";

const views = this.config.views;

if (!views) {
Expand All @@ -39,34 +41,44 @@ export function resolvePath(this: EtaCore, template: string, options?: Partial<O

const baseFilePath = options && options.filepath;

// how we index cached template paths
const cacheIndex = JSON.stringify({
filename: baseFilePath, // filename of the template which called includeFile()
path: templatePath,
views: this.config.views,
});

templatePath += path.extname(templatePath) ? "" : ".eta";

// if the file was included from another template
if (baseFilePath) {
// check the cache
if (this.config.cacheFilepaths && this.filepathCache[baseFilePath]) {
return this.filepathCache[baseFilePath];

if (this.config.cacheFilepaths && this.filepathCache[cacheIndex]) {
return this.filepathCache[cacheIndex];
}

const absolutePathTest = absolutePathRegExp.exec(template);
const absolutePathTest = absolutePathRegExp.exec(templatePath);

if (absolutePathTest && absolutePathTest.length) {
const formattedPath = template.replace(/^\/*/, "");
const formattedPath = templatePath.replace(/^\/*|^\\*/, "");
resolvedFilePath = path.join(views, formattedPath);
} else {
resolvedFilePath = path.join(path.dirname(baseFilePath), template);
resolvedFilePath = path.join(path.dirname(baseFilePath), templatePath);
}
} else {
resolvedFilePath = path.join(views, template);
resolvedFilePath = path.join(views, templatePath);
}

if (dirIsChild(views, resolvedFilePath)) {
// add resolved path to the cache
if (baseFilePath && this.config.cacheFilepaths) {
this.filepathCache[baseFilePath] = resolvedFilePath;
this.filepathCache[cacheIndex] = resolvedFilePath;
}

return resolvedFilePath;
} else {
throw new EtaError(`Template '${template}' is not in the views directory`);
throw new EtaError(`Template '${templatePath}' is not in the views directory`);
}
}

Expand Down
8 changes: 4 additions & 4 deletions test/compile-string.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ __eta.res+=__eta.e(it.name)
if (__eta.layout) {
__eta.res = include (__eta.layout, {body: __eta.res, ...__eta.layoutData});
__eta.res = include (__eta.layout, {body: __eta.res, ...it, ...__eta.layoutData});
}
Expand Down Expand Up @@ -60,7 +60,7 @@ __eta.res+=it.name
if (__eta.layout) {
__eta.res = include (__eta.layout, {body: __eta.res, ...__eta.layoutData});
__eta.res = include (__eta.layout, {body: __eta.res, ...it, ...__eta.layoutData});
}
Expand Down Expand Up @@ -90,7 +90,7 @@ __eta.res+=__eta.e(it.lastname)
if (__eta.layout) {
__eta.res = include (__eta.layout, {body: __eta.res, ...__eta.layoutData});
__eta.res = include (__eta.layout, {body: __eta.res, ...it, ...__eta.layoutData});
}
Expand Down Expand Up @@ -140,7 +140,7 @@ __eta.res+=include("mypartial")
if (__eta.layout) {
__eta.res = include (__eta.layout, {body: __eta.res, ...__eta.layoutData});
__eta.res = include (__eta.layout, {body: __eta.res, ...it, ...__eta.layoutData});
}
Expand Down
33 changes: 11 additions & 22 deletions test/file-utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
/* global it, expect, describe */

import { renderFile, loadFile, templates } from "../src/index";
import { config } from "../src/config";
import path from "node:path";

const path = require("path"),
filePath = path.join(__dirname, "templates/simple.eta");
import { Eta } from "../src/index";

describe("File tests", () => {
it("loadFile works", () => {
loadFile(filePath, { filename: filePath });
expect(templates.get(filePath)).toBeTruthy();
expect(templates.get(filePath)({ name: "Ben" }, config)).toBeTruthy();
});
});
const viewsDir = path.join(__dirname, "templates");

const eta = new Eta({ views: viewsDir, cache: true });

describe("Filepath caching", () => {
it("Filepath caching works as expected", async () => {
// This test renders templates/has-include.eta with caching enabled, then checks to make sure
// `config.filepathCache` contains the expected result afterward

const viewsDir = path.join(__dirname, "templates");

const templateResult = await renderFile("has-include", {}, { views: viewsDir, cache: true });
const templateResult = await eta.render("has-include", {});

expect(templateResult).toEqual(
`This is the outermost template. Now we'll include a partial
Expand All @@ -36,16 +28,13 @@ Hi Test Runner`
// Filepath caching is based on the premise that given the same path, includer filename, root directory, and views directory (or directories)
// the getPath function will always return the same result (assuming that caching is enabled and we're not expecting the templates to change)

const pathToHasInclude = `{"filename":"${viewsDir}/has-include.eta","path":"./partial","views":"${viewsDir}"}`;

const pathToPartial = `{"filename":"${viewsDir}/partial.eta","path":"./simple","views":"${viewsDir}"}`;
const pathToPartial = `{"filename":"${viewsDir}/has-include.eta","path":"./partial","views":"${viewsDir}"}`;

const pathToSimple = `{"path":"has-include","views":"${viewsDir}"}`;
const pathToSimple = `{"filename":"${viewsDir}/partial.eta","path":"./simple","views":"${viewsDir}"}`;

expect(config.filepathCache).toEqual({
[pathToHasInclude]: `${viewsDir}/partial.eta`,
[pathToPartial]: `${viewsDir}/simple.eta`,
[pathToSimple]: `${viewsDir}/has-include.eta`,
expect(eta.filepathCache).toEqual({
[pathToPartial]: `${viewsDir}/partial.eta`,
[pathToSimple]: `${viewsDir}/simple.eta`,
});
});
});
2 changes: 1 addition & 1 deletion test/templates/has-include.eta
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
This is the outermost template. Now we'll include a partial

===========================================================
<%~ includeFile('./partial') %>
<%~ include('./partial') %>
2 changes: 1 addition & 1 deletion test/templates/partial.eta
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
This is a partial.
<%~ includeFile('./simple', {name: 'Test Runner'}) %>
<%~ include('./simple', {name: 'Test Runner'}) %>

0 comments on commit e715f07

Please sign in to comment.