[REF] simplify template definition

This commit is contained in:
Géry Debongnie
2022-05-26 15:09:09 +02:00
committed by Sam Degueldre
parent 98b58b505b
commit 2b4d8874c7
52 changed files with 1519 additions and 1535 deletions
+7 -4
View File
@@ -987,17 +987,18 @@ export class CodeGenerator {
const key = `key + \`${this.generateComponentKey()}\``;
if (isDynamic) {
const templateVar = generateId("template");
if (!this.staticDefs.find((d) => d.id === "call")) {
this.staticDefs.push({ id: "call", expr: `app.callTemplate.bind(app)` });
}
this.define(templateVar, subTemplate);
block = this.createBlock(block, "multi", ctx);
this.helpers.add("call");
this.insertBlock(`call(this, ${templateVar}, ctx, node, ${key})`, block!, {
...ctx,
forceNewBlock: !block,
});
} else {
const id = generateId(`callTemplate_`);
this.helpers.add("getTemplate");
this.staticDefs.push({ id, expr: `getTemplate(${subTemplate})` });
this.staticDefs.push({ id, expr: `app.getTemplate(${subTemplate})` });
block = this.createBlock(block, "multi", ctx);
this.insertBlock(`${id}.call(this, ctx, node, ${key})`, block!, {
...ctx,
@@ -1254,7 +1255,9 @@ export class CodeGenerator {
}
}
compileTPortal(ast: ASTTPortal, ctx: Context) {
this.helpers.add("Portal");
if (!this.staticDefs.find((d) => d.id === "Portal")) {
this.staticDefs.push({ id: "Portal", expr: `app.Portal` });
}
let { block } = ctx;
const name = this.compileInNewTarget("slot", ast.content, ctx);
+3 -2
View File
@@ -1,10 +1,11 @@
import type { TemplateSet } from "../app/template_set";
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;
export type TemplateFunction = (app: TemplateSet, bdom: any, helpers: any) => Template;
interface CompileOptions extends Config {
name?: string;
@@ -26,5 +27,5 @@ export function compile(
const codeGenerator = new CodeGenerator(ast, { ...options, hasSafeContext });
const code = codeGenerator.generateCode();
// template function
return new Function("bdom, helpers", code) as TemplateFunction;
return new Function("app, bdom, helpers", code) as TemplateFunction;
}