mh-two-thousand-and-two
2024-04-12 3d2ec2fd0578d3ba0a414b0cc4e4a2ae60878596
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import { 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
}
 
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