123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- import Vue, { VNode } from "vue"
- /*
- * Template compilation options / results
- */
- interface CompilerOptions {
- modules?: ModuleOptions[];
- directives?: Record<string, DirectiveFunction>;
- preserveWhitespace?: boolean;
- whitespace?: 'preserve' | 'condense';
- outputSourceRange?: any
- }
- interface CompilerOptionsWithSourceRange extends CompilerOptions {
- outputSourceRange: true
- }
- interface ErrorWithRange {
- msg: string;
- start: number;
- end: number;
- }
- interface CompiledResult<ErrorType> {
- ast: ASTElement | undefined;
- render: string;
- staticRenderFns: string[];
- errors: ErrorType[];
- tips: ErrorType[];
- }
- interface CompiledResultFunctions {
- render: () => VNode;
- staticRenderFns: (() => VNode)[];
- }
- interface ModuleOptions {
- preTransformNode: (el: ASTElement) => ASTElement | undefined;
- transformNode: (el: ASTElement) => ASTElement | undefined;
- postTransformNode: (el: ASTElement) => void;
- genData: (el: ASTElement) => string;
- transformCode?: (el: ASTElement, code: string) => string;
- staticKeys?: string[];
- }
- type DirectiveFunction = (node: ASTElement, directiveMeta: ASTDirective) => void;
- /*
- * AST Types
- */
- /**
- * - 0: FALSE - whole sub tree un-optimizable
- * - 1: FULL - whole sub tree optimizable
- * - 2: SELF - self optimizable but has some un-optimizable children
- * - 3: CHILDREN - self un-optimizable but have fully optimizable children
- * - 4: PARTIAL - self un-optimizable with some un-optimizable children
- */
- export type SSROptimizability = 0 | 1 | 2 | 3 | 4
- export interface ASTModifiers {
- [key: string]: boolean;
- }
- export interface ASTIfCondition {
- exp: string | undefined;
- block: ASTElement;
- }
- export interface ASTElementHandler {
- value: string;
- params?: any[];
- modifiers: ASTModifiers | undefined;
- }
- export interface ASTElementHandlers {
- [key: string]: ASTElementHandler | ASTElementHandler[];
- }
- export interface ASTDirective {
- name: string;
- rawName: string;
- value: string;
- arg: string | undefined;
- modifiers: ASTModifiers | undefined;
- }
- export type ASTNode = ASTElement | ASTText | ASTExpression;
- export interface ASTElement {
- type: 1;
- tag: string;
- attrsList: { name: string; value: any }[];
- attrsMap: Record<string, any>;
- parent: ASTElement | undefined;
- children: ASTNode[];
- processed?: true;
- static?: boolean;
- staticRoot?: boolean;
- staticInFor?: boolean;
- staticProcessed?: boolean;
- hasBindings?: boolean;
- text?: string;
- attrs?: { name: string; value: any }[];
- props?: { name: string; value: string }[];
- plain?: boolean;
- pre?: true;
- ns?: string;
- component?: string;
- inlineTemplate?: true;
- transitionMode?: string | null;
- slotName?: string;
- slotTarget?: string;
- slotScope?: string;
- scopedSlots?: Record<string, ASTElement>;
- ref?: string;
- refInFor?: boolean;
- if?: string;
- ifProcessed?: boolean;
- elseif?: string;
- else?: true;
- ifConditions?: ASTIfCondition[];
- for?: string;
- forProcessed?: boolean;
- key?: string;
- alias?: string;
- iterator1?: string;
- iterator2?: string;
- staticClass?: string;
- classBinding?: string;
- staticStyle?: string;
- styleBinding?: string;
- events?: ASTElementHandlers;
- nativeEvents?: ASTElementHandlers;
- transition?: string | true;
- transitionOnAppear?: boolean;
- model?: {
- value: string;
- callback: string;
- expression: string;
- };
- directives?: ASTDirective[];
- forbidden?: true;
- once?: true;
- onceProcessed?: boolean;
- wrapData?: (code: string) => string;
- wrapListeners?: (code: string) => string;
- // 2.4 ssr optimization
- ssrOptimizability?: SSROptimizability;
- // weex specific
- appendAsTree?: boolean;
- }
- export interface ASTExpression {
- type: 2;
- expression: string;
- text: string;
- tokens: (string | Record<string, any>)[];
- static?: boolean;
- // 2.4 ssr optimization
- ssrOptimizability?: SSROptimizability;
- }
- export interface ASTText {
- type: 3;
- text: string;
- static?: boolean;
- isComment?: boolean;
- // 2.4 ssr optimization
- ssrOptimizability?: SSROptimizability;
- }
- /*
- * SFC parser related types
- */
- interface SFCParserOptions {
- pad?: true | 'line' | 'space';
- deindent?: boolean
- }
- export interface SFCBlock {
- type: string;
- content: string;
- attrs: Record<string, string>;
- start?: number;
- end?: number;
- lang?: string;
- src?: string;
- scoped?: boolean;
- module?: string | boolean;
- }
- export interface SFCDescriptor {
- template: SFCBlock | undefined;
- script: SFCBlock | undefined;
- styles: SFCBlock[];
- customBlocks: SFCBlock[];
- }
- /*
- * Exposed functions
- */
- export function compile(
- template: string,
- options: CompilerOptionsWithSourceRange
- ): CompiledResult<ErrorWithRange>
- export function compile(
- template: string,
- options?: CompilerOptions
- ): CompiledResult<string>;
- export function compileToFunctions(template: string): CompiledResultFunctions;
- export function ssrCompile(
- template: string,
- options: CompilerOptionsWithSourceRange
- ): CompiledResult<ErrorWithRange>;
- export function ssrCompile(
- template: string,
- options?: CompilerOptions
- ): CompiledResult<string>;
- export function ssrCompileToFunctions(template: string): CompiledResultFunctions;
- export function parseComponent(
- file: string,
- options?: SFCParserOptions
- ): SFCDescriptor;
- export function generateCodeFrame(
- template: string,
- start: number,
- end: number
- ): string;
|