Skip to content

Commit

Permalink
Improved support for Vue.js
Browse files Browse the repository at this point in the history
  • Loading branch information
max-leuthaeuser committed Dec 9, 2022
1 parent 12a2566 commit 1ea9bc2
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,37 @@ const toJSAst = (file) => {
return ast;
};

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


/**
* Convert a single vue file to AST
*/
const toVueAst = (file) => {
const code = fs.readFileSync(file, "utf-8");
const cleanedCode = code
.replace(vueCleaningRegex, function(match){ return match.replaceAll(/\S/g, " ") })
.replace(vueTemplateRegex, function(match, grA, grB, grC){
.replace(vueCommentRegex, function(match){ return match.replaceAll(/\S/g, " ") })
.replace(vueCleaningRegex, function(match){ return match.replaceAll(/\S/g, " ").substring(1) + ";" })
.replace(vueBindRegex, function(match, grA, grB, grC){
return grA.replaceAll(/\S/g, " ") +
grB.replaceAll("{{", "{ ").replaceAll("}}", " }") +
grB +
grC.replaceAll(/\S/g, " ")
})
.replace(vuePropRegex, function(match, grA, grB, grC){
return grA +
grB.replaceAll(/\S/g, " ") +
grC.replace(/\.|:|@/g, "-")
})
.replace(vueTemplateRegex, function(match, grA, grB, grC){
return grA +
grB
.replaceAll("{{", "{ ")
.replaceAll("}}", " }") +
grC
});
const ast = babelParser.parse(
cleanedCode,
Expand Down

0 comments on commit 1ea9bc2

Please sign in to comment.