/
compile.ts
71 lines (65 loc) · 1.95 KB
/
compile.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
import compileToString from "./compile-string.ts";
import { getConfig } from "./config.ts";
import EtaErr from "./err.ts";
/* TYPES */
import { EtaConfig, PartialConfig } from "./config.ts";
import { 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 {
var options: EtaConfig = getConfig(config || {});
var ctor; // constructor
/* ASYNC HANDLING */
// The below code is modified from mde/ejs. All credit should go to them.
if (options.async) {
ctor = getAsyncFunctionConstructor() as FunctionConstructor;
} else {
ctor = 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;
}
}
}