Skip to content

Commit

Permalink
Initial filepath caching implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nebrelbug committed Sep 15, 2020
1 parent 0636a73 commit cd5a436
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
26 changes: 24 additions & 2 deletions deno_dist/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,51 +12,73 @@ type trimConfig = "nl" | "slurp" | false;
export interface EtaConfig {
/** Whether or not to automatically XML-escape interpolations. Default true */
autoEscape: boolean;

/** Configure automatic whitespace trimming. Default `[false, 'nl']` */
autoTrim: trimConfig | [trimConfig, trimConfig];

/** Compile to async function */
async: boolean;

/** Whether or not to cache templates if `name` or `filename` is passed */
cache: boolean;

/** XML-escaping function */
e: (str: string) => string;

/** Parsing options */
parse: {
/** Which prefix to use for evaluation. Default `""` */
exec: string;

/** Which prefix to use for interpolation. Default `"="` */
interpolate: string;

/** Which prefix to use for raw interpolation. Default `"~"` */
raw: string;
};

/** Array of plugins */
plugins: Array<{ processFnString?: Function; processAST?: Function }>;

/** Remove all safe-to-remove whitespace */
rmWhitespace: boolean;

/** Delimiters: by default `['<%', '%>']` */
tags: [string, string];

/** Holds template cache */
templates: Cacher<TemplateFunction>;

/** Name of the data object. Default `it` */
varName: string;

/** Absolute path to template file */
filename?: string;

/** Holds cache of resolved filepaths. Set to `false` to disable */
filepathCache?: object | false;
filepathCache?: Record<string, string> | false;

/** Function to include templates by name */
include?: Function;

/** Function to include templates by filepath */
includeFile?: Function;

/** Name of template */
name?: string;

/** Where should absolute paths begin? Default '/' */
root?: string;

/** Make data available on the global object instead of varName */
useWith?: boolean;

/** Whether or not to cache templates if `name` or `filename` is passed: duplicate of `cache` */
"view cache"?: boolean;
/** Directories that contain templates */

/** Directory or directories that contain templates */
views?: string | Array<string>;

[index: string]: any; // eslint-disable-line @typescript-eslint/no-explicit-any
}

Expand Down
24 changes: 24 additions & 0 deletions deno_dist/file-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@ function getPath(path: string, options: EtaConfig) {
var views = options.views;
var searchedPaths: Array<string> = [];

// If these four values are the same,
// getPath() will return the same result every time.
// We can cache the result to avoid expensive
// file operations.
var pathOptions = JSON.stringify({
filename: options.filename,
path: path,
root: options.root,
views: options.views,
});

if (
options.cache && options.filepathCache && options.filepathCache[pathOptions]
) {
return options.filepathCache[pathOptions];
}

/** Add a filepath to the list of paths we've checked for a template */
function addPathToSearched(pathSearched: string) {
if (!searchedPaths.includes(pathSearched)) {
Expand Down Expand Up @@ -160,6 +177,13 @@ function getPath(path: string, options: EtaConfig) {
);
}
}

// If caching and filepathCache are enabled,
// cache the input & output of this function.
if (options.cache && options.filepathCache) {
options.filepathCache[pathOptions] = includePath;
}

return includePath;
}

Expand Down
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export interface EtaConfig {
filename?: string

/** Holds cache of resolved filepaths. Set to `false` to disable */
filepathCache?: object | false
filepathCache?: Record<string, string> | false

/** Function to include templates by name */
include?: Function
Expand Down
22 changes: 22 additions & 0 deletions src/file-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ function getPath(path: string, options: EtaConfig) {
var views = options.views
var searchedPaths: Array<string> = []

// If these four values are the same,
// getPath() will return the same result every time.
// We can cache the result to avoid expensive
// file operations.
var pathOptions = JSON.stringify({
filename: options.filename,
path: path,
root: options.root,
views: options.views
})

if (options.cache && options.filepathCache && options.filepathCache[pathOptions]) {
return options.filepathCache[pathOptions]
}

/** Add a filepath to the list of paths we've checked for a template */
function addPathToSearched(pathSearched: string) {
if (!searchedPaths.includes(pathSearched)) {
Expand Down Expand Up @@ -146,6 +161,13 @@ function getPath(path: string, options: EtaConfig) {
throw EtaErr('Could not find the template "' + path + '". Paths tried: ' + searchedPaths)
}
}

// If caching and filepathCache are enabled,
// cache the input & output of this function.
if (options.cache && options.filepathCache) {
options.filepathCache[pathOptions] = includePath
}

return includePath
}

Expand Down

0 comments on commit cd5a436

Please sign in to comment.