您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“vue文件中的ts代碼怎么分析”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
我們知道vue文件是由'template'、'script'、'style'三種類型代碼組合成的。如果要分析<script lang="ts"></script>
標(biāo)簽內(nèi)的ts代碼,要怎么做呢?
@vue/compiler-dom 編譯器
這個(gè)parser來解析以下測(cè)試代碼為例:
<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 選擇 @vue/compiler-dom
可以發(fā)現(xiàn),右側(cè)的AST結(jié)構(gòu):代碼被解析成 template、script和style這三部分,我們通過AST節(jié)點(diǎn)屬性就可以獲取到script標(biāo)簽內(nèi)代碼的字符串信息(圖中的綠色框部分)。
代碼如下:
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)
運(yùn)行結(jié)果:
typescript
解析在第一步中,我們通過@vue/compiler-dom
提取出了 vue 文件 script標(biāo)簽內(nèi)的代碼字符串;接下來,把提取出的代碼字符串交給 typescript
處理,生成對(duì)應(yīng)的 AST。
以上面代碼為例:
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代碼轉(zhuǎn)化為AST // 第一個(gè)參數(shù)為命名,可以隨意填, // 第二個(gè)參數(shù)是需要生成AST的源代碼字符串 // 第三個(gè)參數(shù)表示TS編譯器版本 // 第四個(gè)參數(shù)表示是否添加parent節(jié)點(diǎn)信息 const ast = tsCompiler.createSourceFile('testCode', tsCode, tsCompiler.ScriptTarget.Latest, true) console.log(ast) return ast } parseVue(analyseCode)
運(yùn)行結(jié)果(圖片不是完整的)
完整的AST explorer
通過TypeScript 的 CompilerAPI : forEachChild
遍歷AST節(jié)點(diǎn)
const ast = parseVue(analyseCode) // 上面示例的函數(shù) const walk = (node) => { // AST遍歷函數(shù) tsCompiler.forEachChild(node, walk); // 遍歷AST節(jié)點(diǎn) console.log(node); // 輸出節(jié)點(diǎn)信息 } walk(ast)
然后根據(jù)代碼中常見的字面量、標(biāo)識(shí)符、表達(dá)式、語句、模塊語法、class 語法等語句
各自都有對(duì)應(yīng)的 AST 節(jié)點(diǎn)類型,就可以去做相應(yīng)的分析了
“vue文件中的ts代碼怎么分析”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。