Files
owl/src/compiler/index.ts
T

31 lines
1.0 KiB
TypeScript
Raw Normal View History

2021-11-13 09:02:34 +01:00
import type { BDom } from "../blockdom";
import { CodeGenerator, Config } from "./code_generator";
import { parse } from "./parser";
export type Template = (context: any, vnode: any, key?: string) => BDom;
export type TemplateFunction = (blocks: any, utils: any) => Template;
interface CompileOptions extends Config {
name?: string;
}
let nextId = 1;
export function compile(template: string | Node, options: CompileOptions = {}): TemplateFunction {
2021-11-13 09:02:34 +01:00
// parsing
const ast = parse(template);
// some work
const hasSafeContext =
template instanceof Node
? !(template instanceof Element) || template.querySelector("[t-set], [t-call]") === null
: !template.includes("t-set") && !template.includes("t-call");
const name = options.name || `template_${nextId++}`;
2021-11-13 09:02:34 +01:00
// code generation
const codeGenerator = new CodeGenerator(name, ast, { ...options, hasSafeContext });
const code = codeGenerator.generateCode();
// template function
return new Function("bdom, helpers", code) as TemplateFunction;
}