JM-admin-ui/vitePlugin/gvaPosition/index.js
2023-10-11 15:11:50 +08:00

52 lines
1.3 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

export default function GvaPosition() {
return {
name: 'gva-position',
apply: 'serve',
transform(code, id) {
const index = id.lastIndexOf('.')
const ext = id.substr(index + 1)
if (ext.toLowerCase() === 'vue') {
return codeLineTrack(code, id)
}
},
}
}
const codeLineTrack = (code, id) => {
const lineList = code.split('\n')
const newList = []
lineList.forEach((item, index) => {
newList.push(addLineAttr(item, index + 1, id)) // 添加位置属性index+1为具体的代码行号
})
return newList.join('\n')
}
const addLineAttr = (lineStr, line, id) => {
if (!/^\s+</.test(lineStr)) {
return lineStr
}
const reg = /((((^(\s)+\<))|(^\<))[\w-]+)|(<\/template)/g
let leftTagList = lineStr.match(reg)
if (leftTagList) {
leftTagList = Array.from(new Set(leftTagList))
leftTagList.forEach((item) => {
const skip = [
'KeepAlive',
'template',
'keep-alive',
'transition',
'el-',
'El',
'router-view',
]
if (item && !skip.some((i) => item.indexOf(i) > -1)) {
const reg = new RegExp(`${item}`)
const location = `${item} code-location="${id}:${line}"`
lineStr = lineStr.replace(reg, location)
}
})
}
return lineStr
}