We know that vue files are composed of three types of codes: 'template', 'script', and 'style'. If you want to analyze the ts code in the <script lang="ts"></script>
tag, what should you do?
# through the @vue/compiler-dom compiler <template> <div> {{ testRef }} </div> </template> <script setup> import { ref } from 'vue' const testRef = ref('test') </script> <style scoped> .test-page { background-color: #fff; } </style>
AST explorer, parser select @vue/compiler-dom [Related recommendations:
vuejs video tutorial、web front-end development】
const vueCompiler = require('@vue/compiler-dom') const analyseCode = `<template> <div> {{ testRef }} </div> </template> <script setup> import { ref } from 'vue' const testRef = ref('test') </script> <style scoped> .test-page { background-color: #fff; } </style>` const parseVue = (vueCode) => { // 解析vue代码 const result = vueCompiler.parse(vueCode) const children = result.children // 获取script片段 let tsCode = '' children.forEach(element => { if (element.tag == 'script') { tsCode = element.children[0].content; } }) console.log(tsCode) } parseVue(analyseCode)
In the first step, we extracted the code string in the vue file script tag through ; next, extract The code string is handed over to typescript
for processing and the corresponding AST is generated. Take the above code as an example:
const vueCompiler = require('@vue/compiler-dom') const tsCompiler = require('typescript') const analyseCode = `<template> <div> {{ testRef }} </div> </template> <script setup> import { ref } from 'vue' const testRef = ref('test') </script> <style scoped> .test-page { background-color: #fff; } </style>` const parseVue = (vueCode) => { // 解析vue代码 const result = vueCompiler.parse(vueCode) const children = result.children // 获取script片段 let tsCode = '' children.forEach(element => { if (element.tag == 'script') { tsCode = element.children[0].content; } }) console.log(tsCode) // 将ts代码转化为AST // 第一个参数为命名,可以随意填, // 第二个参数是需要生成AST的源代码字符串 // 第三个参数表示TS编译器版本 // 第四个参数表示是否添加parent节点信息 const ast = tsCompiler.createSourceFile('testCode', tsCode, tsCompiler.ScriptTarget.Latest, true) console.log(ast) return ast } parseVue(analyseCode)
Running results (the picture is not complete)
Complete
AST explorer3. Step 3: Traverse and analyze AST nodes at all levelsTraverse AST nodes<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:js;toolbar:false;">const ast = parseVue(analyseCode) // 上面示例的函数
const walk = (node) => { // AST遍历函数
tsCompiler.forEachChild(node, walk); // 遍历AST节点
console.log(node); // 输出节点信息
}
walk(ast)</pre><div class="contentsignin">Copy after login</div></div>Then according to the common </p>literals, identifiers, expressions, statements, module syntax, class syntax and other statements in the code<p>each has a corresponding AST node Type, you can do the corresponding analysis (detailed knowledge of this piece can be searched online, and can be combined with the visualization tool <code> AST explorer
to observe) (Learning video sharing:
The above is the detailed content of Take you step by step to analyze the ts code in the vue file. For more information, please follow other related articles on the PHP Chinese website!