Skip to content

parseVueFile

解析Vue文件内容,提取template、script、style sections。

函数签名

typescript
function parseVueFile(code: string): VueSection[]

interface VueSection {
  type: VueSectionType
  content: string
  startLine: number
  endLine: number
}

type VueSectionType = 'template' | 'script' | 'style'

参数

参数名类型必填说明
codestringVue文件的完整代码内容

返回值

类型说明
VueSection[]解析出的Vue sections数组

工作原理

  1. 逐行解析:将代码按行分割,逐行检测 section 标签
  2. 识别section开始:检测 <template><script><style> 开始标签
  3. 收集内容:收集 section 标签之间的内容
  4. 识别section结束:检测 </template></script></style> 结束标签
  5. 保存section:将解析出的 section 保存到数组中,包含类型、内容和行号信息
  6. 返回结果:返回所有解析出的 sections

支持标准的Vue SFC格式,能够正确处理嵌套和多个同类型section。