-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
core.ts
76 lines (61 loc) · 2.24 KB
/
core.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { Cacher } from "./storage.ts";
import { compile } from "./compile.ts";
import { compileToString, compileBody } from "./compile-string.ts";
import { defaultConfig } from "./config.ts";
import { parse } from "./parse.ts";
import { render, renderAsync, renderString, renderStringAsync } from "./render.ts";
import { RuntimeErr, EtaError } from "./err.ts";
import { TemplateFunction } from "./compile.ts";
/* TYPES */
import type { EtaConfig, Options } from "./config.ts";
/* END TYPES */
export class Eta {
constructor(customConfig?: Partial<EtaConfig>) {
if (customConfig) {
this.config = { ...defaultConfig, ...customConfig };
} else {
this.config = { ...defaultConfig };
}
}
config: EtaConfig;
RuntimeErr = RuntimeErr;
compile = compile;
compileToString = compileToString;
compileBody = compileBody;
parse = parse;
render = render;
renderAsync = renderAsync;
renderString = renderString;
renderStringAsync = renderStringAsync;
filepathCache: Record<string, string> = {};
templatesSync = new Cacher<TemplateFunction>({});
templatesAsync = new Cacher<TemplateFunction>({});
// resolvePath takes a relative path from the "views" directory
resolvePath: null | ((this: Eta, template: string, options?: Partial<Options>) => string) = null;
readFile: null | ((this: Eta, path: string) => string) = null;
// METHODS
configure(customConfig: Partial<EtaConfig>) {
this.config = { ...this.config, ...customConfig };
}
withConfig(customConfig: Partial<EtaConfig>) {
return { ...this, config: { ...this.config, ...customConfig } };
}
loadTemplate(
name: string,
template: string | TemplateFunction, // template string or template function
options?: { async: boolean }
): void {
if (typeof template === "string") {
const templates = options && options.async ? this.templatesAsync : this.templatesSync;
templates.define(name, this.compile(template, options));
} else {
let templates = this.templatesSync;
if (template.constructor.name === "AsyncFunction" || (options && options.async)) {
templates = this.templatesAsync;
}
templates.define(name, template);
}
}
}
// for instance checking against thrown errors
export { EtaError };