tag, what should you do?"/> tag, what should you do?">
Home >Web Front-end >Vue.js >Take you step by step to analyze the ts code in the vue file
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 e7a8c58ab982b920d50c74fc26d408552cacc6d41bbb37262a98f745aa00fbf0
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>Put the above code into
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)Running results: ##2. Step 2: Pass
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 explorer 3. Step 3: Traverse and analyze AST nodes at all levelsTraverse AST nodes<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>
Then according to the common
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 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!