UNPKG

5.18 kBTypeScriptView Raw
1import { VNode } from 'vue'
2
3/*
4 * Template compilation options / results
5 */
6interface CompilerOptions {
7 modules?: ModuleOptions[]
8 directives?: Record<string, DirectiveFunction>
9 preserveWhitespace?: boolean
10 whitespace?: 'preserve' | 'condense'
11 outputSourceRange?: any
12}
13
14interface CompilerOptionsWithSourceRange extends CompilerOptions {
15 outputSourceRange: true
16}
17
18interface ErrorWithRange {
19 msg: string
20 start: number
21 end: number
22}
23
24interface CompiledResult<ErrorType> {
25 ast: ASTElement | undefined
26 render: string
27 staticRenderFns: string[]
28 errors: ErrorType[]
29 tips: ErrorType[]
30}
31
32interface CompiledResultFunctions {
33 render: () => VNode
34 staticRenderFns: (() => VNode)[]
35}
36
37interface ModuleOptions {
38 preTransformNode: (el: ASTElement) => ASTElement | undefined
39 transformNode: (el: ASTElement) => ASTElement | undefined
40 postTransformNode: (el: ASTElement) => void
41 genData: (el: ASTElement) => string
42 transformCode?: (el: ASTElement, code: string) => string
43 staticKeys?: string[]
44}
45
46type DirectiveFunction = (node: ASTElement, directiveMeta: ASTDirective) => void
47
48/*
49 * AST Types
50 */
51
52/**
53 * - 0: FALSE - whole sub tree un-optimizable
54 * - 1: FULL - whole sub tree optimizable
55 * - 2: SELF - self optimizable but has some un-optimizable children
56 * - 3: CHILDREN - self un-optimizable but have fully optimizable children
57 * - 4: PARTIAL - self un-optimizable with some un-optimizable children
58 */
59export type SSROptimizability = 0 | 1 | 2 | 3 | 4
60
61export interface ASTModifiers {
62 [key: string]: boolean
63}
64
65export interface ASTIfCondition {
66 exp: string | undefined
67 block: ASTElement
68}
69
70export interface ASTElementHandler {
71 value: string
72 params?: any[]
73 modifiers: ASTModifiers | undefined
74}
75
76export interface ASTElementHandlers {
77 [key: string]: ASTElementHandler | ASTElementHandler[]
78}
79
80export interface ASTDirective {
81 name: string
82 rawName: string
83 value: string
84 arg: string | undefined
85 modifiers: ASTModifiers | undefined
86}
87
88export type ASTNode = ASTElement | ASTText | ASTExpression
89
90export interface ASTElement {
91 type: 1
92 tag: string
93 attrsList: { name: string; value: any }[]
94 attrsMap: Record<string, any>
95 parent: ASTElement | undefined
96 children: ASTNode[]
97
98 processed?: true
99
100 static?: boolean
101 staticRoot?: boolean
102 staticInFor?: boolean
103 staticProcessed?: boolean
104 hasBindings?: boolean
105
106 text?: string
107 attrs?: { name: string; value: any }[]
108 props?: { name: string; value: string }[]
109 plain?: boolean
110 pre?: true
111 ns?: string
112
113 component?: string
114 inlineTemplate?: true
115 transitionMode?: string | null
116 slotName?: string
117 slotTarget?: string
118 slotScope?: string
119 scopedSlots?: Record<string, ASTElement>
120
121 ref?: string
122 refInFor?: boolean
123
124 if?: string
125 ifProcessed?: boolean
126 elseif?: string
127 else?: true
128 ifConditions?: ASTIfCondition[]
129
130 for?: string
131 forProcessed?: boolean
132 key?: string
133 alias?: string
134 iterator1?: string
135 iterator2?: string
136
137 staticClass?: string
138 classBinding?: string
139 staticStyle?: string
140 styleBinding?: string
141 events?: ASTElementHandlers
142 nativeEvents?: ASTElementHandlers
143
144 transition?: string | true
145 transitionOnAppear?: boolean
146
147 model?: {
148 value: string
149 callback: string
150 expression: string
151 }
152
153 directives?: ASTDirective[]
154
155 forbidden?: true
156 once?: true
157 onceProcessed?: boolean
158 wrapData?: (code: string) => string
159 wrapListeners?: (code: string) => string
160
161 // 2.4 ssr optimization
162 ssrOptimizability?: SSROptimizability
163}
164
165export interface ASTExpression {
166 type: 2
167 expression: string
168 text: string
169 tokens: (string | Record<string, any>)[]
170 static?: boolean
171 // 2.4 ssr optimization
172 ssrOptimizability?: SSROptimizability
173}
174
175export interface ASTText {
176 type: 3
177 text: string
178 static?: boolean
179 isComment?: boolean
180 // 2.4 ssr optimization
181 ssrOptimizability?: SSROptimizability
182}
183
184/*
185 * SFC parser related types
186 */
187interface SFCParserOptions {
188 pad?: true | 'line' | 'space'
189 deindent?: boolean
190}
191
192export interface SFCBlock {
193 type: string
194 content: string
195 attrs: Record<string, string>
196 start?: number
197 end?: number
198 lang?: string
199 src?: string
200 scoped?: boolean
201 module?: string | boolean
202}
203
204export interface SFCDescriptor {
205 template: SFCBlock | undefined
206 script: SFCBlock | undefined
207 styles: SFCBlock[]
208 customBlocks: SFCBlock[]
209}
210
211/*
212 * Exposed functions
213 */
214export function compile(
215 template: string,
216 options: CompilerOptionsWithSourceRange
217): CompiledResult<ErrorWithRange>
218
219export function compile(
220 template: string,
221 options?: CompilerOptions
222): CompiledResult<string>
223
224export function compileToFunctions(template: string): CompiledResultFunctions
225
226export function ssrCompile(
227 template: string,
228 options: CompilerOptionsWithSourceRange
229): CompiledResult<ErrorWithRange>
230
231export function ssrCompile(
232 template: string,
233 options?: CompilerOptions
234): CompiledResult<string>
235
236export function ssrCompileToFunctions(template: string): CompiledResultFunctions
237
238export function parseComponent(
239 file: string,
240 options?: SFCParserOptions
241): SFCDescriptor
242
243export function generateCodeFrame(
244 template: string,
245 start: number,
246 end: number
247): string
248
\No newline at end of file