deno.land / x / eta@v1.12.3 / compile.ts

compile.ts
View Documentation
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
import compileToString from "./compile-string.ts";import { getConfig } from "./config.ts";import EtaErr from "./err.ts";
/* TYPES */
import type { EtaConfig, PartialConfig } from "./config.ts";import type { CallbackFn } from "./file-handlers.ts";import { getAsyncFunctionConstructor } from "./polyfills.ts";export type TemplateFunction = ( data: object, config: EtaConfig, cb?: CallbackFn,) => string;
/* END TYPES */
/** * Takes a template string and returns a template function that can be called with (data, config, [cb]) * * @param str - The template string * @param config - A custom configuration object (optional) * * **Example** * * ```js * let compiledFn = eta.compile("Hi <%= it.user %>") * // function anonymous() * let compiledFnStr = compiledFn.toString() * // "function anonymous(it,E,cb\n) {\nvar tR='',include=E.include.bind(E),includeFile=E.includeFile.bind(E);tR+='Hi ';tR+=E.e(it.user);if(cb){cb(null,tR)} return tR\n}" * ``` */
export default function compile( str: string, config?: PartialConfig,): TemplateFunction { const options: EtaConfig = getConfig(config || {});
/* ASYNC HANDLING */ // The below code is modified from mde/ejs. All credit should go to them. const ctor = options.async ? (getAsyncFunctionConstructor() as FunctionConstructor) : Function; /* END ASYNC HANDLING */
try { return new ctor( options.varName, "E", // EtaConfig "cb", // optional callback compileToString(str, options), ) as TemplateFunction; // eslint-disable-line no-new-func } catch (e) { if (e instanceof SyntaxError) { throw EtaErr( "Bad template syntax\n\n" + e.message + "\n" + Array(e.message.length + 1).join("=") + "\n" + compileToString(str, options) + "\n", // This will put an extra newline before the callstack for extra readability ); } else { throw e; } }}
eta
Embedded JS template engine for Node, Deno, and the browser. Lighweight, fast, and pluggable. Written in TypeScript
DocumentationDocumentation
GitHub Repositoryeta-dev/eta
GitHub Stars
527

Version Info

Tagged at
10 months ago

External Dependencies

1 external dependency