diff --git a/src/compiler/code_generator.ts b/src/compiler/code_generator.ts index e3e2052c..8731b136 100644 --- a/src/compiler/code_generator.ts +++ b/src/compiler/code_generator.ts @@ -31,6 +31,7 @@ export interface Config { export interface CodeGenOptions extends Config { hasSafeContext?: boolean; + name?: string; } // using a non-html document so that HTML serializes as XML instead @@ -194,20 +195,20 @@ export class CodeGenerator { isDebug: boolean = false; targets: CodeTarget[] = []; target = new CodeTarget("template"); - templateName: string; + templateName?: string; dev: boolean; translateFn: (s: string) => string; translatableAttributes: string[]; ast: AST; staticCalls: { id: string; template: string }[] = []; - constructor(name: string, ast: AST, options: CodeGenOptions) { + constructor(ast: AST, options: CodeGenOptions) { this.translateFn = options.translateFn || ((s: string) => s); this.translatableAttributes = options.translatableAttributes || TRANSLATABLE_ATTRS; this.hasSafeContext = options.hasSafeContext || false; this.dev = options.dev || false; this.ast = ast; - this.templateName = name; + this.templateName = options.name; } generateCode(): string { @@ -229,6 +230,9 @@ export class CodeGenerator { ` let { text, createBlock, list, multi, html, toggler, component } = bdom;`, `let { withDefault, getTemplate, prepareList, withKey, zero, call, callSlot, capture, isBoundary, shallowEqual, setContextValue, toNumber, safeOutput } = helpers;`, ]; + if (this.templateName) { + mainCode.push(`// Template name: "${this.templateName}"`); + } for (let { id, template } of this.staticCalls) { mainCode.push(`const ${id} = getTemplate(${template});`); diff --git a/src/compiler/index.ts b/src/compiler/index.ts index c577be59..b6f62466 100644 --- a/src/compiler/index.ts +++ b/src/compiler/index.ts @@ -9,7 +9,6 @@ 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 { // parsing const ast = parse(template); @@ -19,10 +18,9 @@ export function compile(template: string | Node, options: CompileOptions = {}): 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++}`; // code generation - const codeGenerator = new CodeGenerator(name, ast, { ...options, hasSafeContext }); + const codeGenerator = new CodeGenerator(ast, { ...options, hasSafeContext }); const code = codeGenerator.generateCode(); // template function