Skip to content

Commit 1ea9bc2

Browse files
Improved support for Vue.js
1 parent 12a2566 commit 1ea9bc2

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

index.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,37 @@ const toJSAst = (file) => {
106106
return ast;
107107
};
108108

109-
const vueCleaningRegex = /<\/*script.*>|<\!--[\s\S]*-->|<style[\s\S]*style>|<\/*br>/ig;
109+
const vueCleaningRegex = /<\/*script.*>|<style[\s\S]*style>|<\/*br>/ig;
110110
const vueTemplateRegex = /(<template.*>)([\s\S]*)(<\/template>)/ig;
111+
const vueCommentRegex = /<\!--[\s\S]*?-->/ig;
112+
const vueBindRegex = /(:\[)([\s\S]*?)(\])/ig;
113+
const vuePropRegex = /(<[\sa-zA-Z]*?)(:|@|\.)([\s\S]*?=)/ig;
114+
111115

112116
/**
113117
* Convert a single vue file to AST
114118
*/
115119
const toVueAst = (file) => {
116120
const code = fs.readFileSync(file, "utf-8");
117121
const cleanedCode = code
118-
.replace(vueCleaningRegex, function(match){ return match.replaceAll(/\S/g, " ") })
119-
.replace(vueTemplateRegex, function(match, grA, grB, grC){
122+
.replace(vueCommentRegex, function(match){ return match.replaceAll(/\S/g, " ") })
123+
.replace(vueCleaningRegex, function(match){ return match.replaceAll(/\S/g, " ").substring(1) + ";" })
124+
.replace(vueBindRegex, function(match, grA, grB, grC){
120125
return grA.replaceAll(/\S/g, " ") +
121-
grB.replaceAll("{{", "{ ").replaceAll("}}", " }") +
126+
grB +
122127
grC.replaceAll(/\S/g, " ")
128+
})
129+
.replace(vuePropRegex, function(match, grA, grB, grC){
130+
return grA +
131+
grB.replaceAll(/\S/g, " ") +
132+
grC.replace(/\.|:|@/g, "-")
133+
})
134+
.replace(vueTemplateRegex, function(match, grA, grB, grC){
135+
return grA +
136+
grB
137+
.replaceAll("{{", "{ ")
138+
.replaceAll("}}", " }") +
139+
grC
123140
});
124141
const ast = babelParser.parse(
125142
cleanedCode,

0 commit comments

Comments
 (0)