UNPKG

@stacksjs/stx

Version:

A performant UI Framework. Powered by Bun.

108 lines 4.66 kB
import { SourceMapGenerator } from './source-maps'; import { TemplateParser, } from './ast'; import type { ASTNode, AttributeNode, ComponentNode, DirectiveNode, DocumentNode, ElementNode, ExpressionNode, } from './ast'; import type { SourceMapV3 } from './source-maps'; // Export singleton instance export declare const compiler: TemplateCompiler; /** * Compilation options */ export declare interface CompileOptions { filename?: string mode?: CompileMode optimize?: boolean sourceMaps?: boolean module?: 'esm' | 'cjs' | 'iife' functionName?: string includeRuntime?: boolean dev?: boolean inlineDirectives?: string[] } /** * Compilation result */ export declare interface CompileResult { code: string map?: SourceMapV3 analysis: { /** Variables used in expressions */ usedVariables: Set<string> /** Directives used */ usedDirectives: Set<string> /** Components used */ usedComponents: Set<string> /** Whether template has dynamic content */ isDynamic: boolean /** Whether template uses slots */ hasSlots: boolean } } declare interface CodegenContext { code: string indentLevel: number sourceMapGenerator?: SourceMapGenerator currentLine: number currentColumn: number sourceFile: string mode: CompileMode optimize: boolean dev: boolean usedVariables: Set<string> usedDirectives: Set<string> usedComponents: Set<string> hasSlots: boolean varCounter: number } /** * Compilation mode */ export type CompileMode = 'ssr' | 'client' | 'universal' /** * Template pre-compiler */ export declare class TemplateCompiler { private parser: TemplateParser; constructor(); compile(template: string, options?: CompileOptions): CompileResult; compileFile(filePath: string, options?: CompileOptions): Promise<CompileResult>; private createContext(options: { sourceFile: string mode: CompileMode optimize: boolean dev: boolean sourceMaps: boolean }): CodegenContext; private generateRenderFunction(ast: DocumentNode, ctx: CodegenContext): string; private analyzeAST(ast: DocumentNode, ctx: CodegenContext): void; private generateNode(node: ASTNode, ctx: CodegenContext): string; private generateText(text: string, _ctx: CodegenContext): string; private generateExpression(node: ExpressionNode, _ctx: CodegenContext): string; private generateDirective(node: DirectiveNode, ctx: CodegenContext): string; private generateIf(condition: string | null, body: ASTNode[] | null, ctx: CodegenContext): string; private generateUnless(condition: string | null, body: ASTNode[] | null, ctx: CodegenContext): string; private generateForeach(params: string | null, body: ASTNode[] | null, ctx: CodegenContext): string; private generateFor(params: string | null, body: ASTNode[] | null, ctx: CodegenContext): string; private generateWhile(condition: string | null, body: ASTNode[] | null, ctx: CodegenContext): string; private generateIsset(params: string | null, body: ASTNode[] | null, ctx: CodegenContext): string; private generateEmpty(params: string | null, body: ASTNode[] | null, ctx: CodegenContext): string; private generateAuth(body: ASTNode[] | null, ctx: CodegenContext): string; private generateGuest(body: ASTNode[] | null, ctx: CodegenContext): string; private generateEnv(params: string | null, body: ASTNode[] | null, ctx: CodegenContext): string; private generateSwitch(params: string | null, body: ASTNode[] | null, ctx: CodegenContext): string; private generateSlot(params: string | null, body: ASTNode[] | null, ctx: CodegenContext): string; private generateSection(params: string | null, body: ASTNode[] | null, ctx: CodegenContext): string; private generatePush(params: string | null, body: ASTNode[] | null, ctx: CodegenContext): string; private generateOnce(body: ASTNode[] | null, ctx: CodegenContext): string; private generateScript(body: ASTNode[] | null, _ctx: CodegenContext): string; private generateStyle(node: { content: string, scoped: boolean }, _ctx: CodegenContext): string; private generateElement(node: ElementNode, ctx: CodegenContext): string; private generateAttribute(attr: AttributeNode, ctx: CodegenContext): string; private parseAttributeValue(value: string, ctx: CodegenContext): string; private generateComponent(node: ComponentNode, ctx: CodegenContext): string; private splitConditionalBody(body: ASTNode[]): { ifBody: ASTNode[] elseifs: Array<{ condition: string, body: ASTNode[] }> elseBody: ASTNode[] }; private wrapModule(functionCode: string, functionName: string, format: 'esm' | 'cjs' | 'iife', _ctx: CodegenContext): string; }