convert-vue2-vue3-comp.js
· 3.6 KiB · JavaScript
Raw
const header = [
`import Modal from '@/components/Modal'`
]
const exported = {
props: {
data: {
type: Object
},
prefix: String
},
methods: {
format(input) {
return input
.replace(/%cmd%/, this.prefix + this.data.name)
.replace(/%name%/, this.data.name)
.replace(/%prefix%/, this.prefix)
},
close() {
this.$emit('close')
}
},
computed: {
active() {
return this.data && this.data.name !== null && this.data.name !== undefined
}
}
}
// end
function cleanCode(input) {
return input
.replace(/this\.([a-zA-Z$_]+)\(/g, '$1(')
.replace(/this\.([a-zA-Z$_]+)/g, '$1.value')
.replace(/\$emit/g, 'emit')
.replace(/\$(route|router)\.value/g, '$1')
}
function objToStr(input) {
return JSON.stringify(input, (key, val) => {
console.log(val, typeof val, val.constructor.name)
if(typeof(val) === "function") {
val = `-${val.name}-`
}
return val
}, 2)
.replace(/"([^"]+)":/g, '$1:')
.replace(/"-([a-zA-Z]+)-"/g, '$1')
}
function getProps() {
let output = ""
if(exported.props) {
output = `const props = defineProps(${objToStr(exported.props)})`
}
return output
}
function getData() {
let output = ""
if(exported.data) {
let data = exported.data()
for(var key in data) {
let v = data[key]
if(v && typeof v === "object") {
v = objToStr(data[key])
}
output += `let ${key} = ref(${v})\n`
}
}
return output
}
function getWatch() {
let output = ""
if(exported.watch) {
for(const key in exported.watch) {
let func = exported.watch[key].handler.toString()
.replace(/handler\((.*)\)\s*{(.*)}/s, (match, p1, p2) => {
return `(${p1}) => { ${cleanCode(p2)} }`
})
output += `watch(${key}, ${func})\n`
}
}
return output
}
function getComputed() {
let output = ""
if(exported.computed) {
for(const key in exported.computed) {
let func = exported.computed[key].toString()
.replace(new RegExp(`${key}\\s*\\((.*)\\)\\s*{(.*)}`,'s'), (match, p1, p2) => {
return `(${p1}) => { ${cleanCode(p2)} }`
})
output += `const ${key} = computed(${func})\n`
}
}
return output
}
function getMethods() {
let output = ""
if(exported.methods) {
for(const key in exported.methods) {
let func = exported.methods[key].toString()
.replace(/([a-zA-Z\s]+\s)?([a-zA-Z_]+)\s*\(([a-zA-Z,\s]*)\)\s*{(.*)}/s, (match, p1, p2, p3, p4) => {
return `${p1||''}function ${p2}(${p3}) {\n${cleanCode(p4)}\n}\n`
})
output += func
}
}
return output
}
function getHooks() {
let output = ""
if(exported.created) {
const rawfunc = exported.created.toString()
const func = rawfunc.slice(rawfunc.indexOf("{") + 1, rawfunc.lastIndexOf("}"));
output += `onBeforeMount(() => {\n${cleanCode(func)}\n})`
}
if(exported.mounted) {
const rawfunc = exported.mounted.toString()
const func = rawfunc.slice(rawfunc.indexOf("{") + 1, rawfunc.lastIndexOf("}"));
output += `onMounted(() => {\n${cleanCode(func)}\n})`
}
return output
}
let output = header.join("\n") + `\nimport { ref, watch, computed, onBeforeMount, onMounted, defineEmits } from 'vue'\n`
output += `import { useRoute, useRouter } from 'vue-router'\n\n`
output += `const emit = defineEmits([])'\n`
output += `const route = useRoute()\nconst router = useRouter()\n`
output += getProps() + "\n"
output += getData() + "\n"
output += getWatch() + "\n"
output += getComputed() + "\n"
output += getMethods() + "\n"
output += getHooks() + "\n"
console.log(output)
1 | const header = [ |
2 | `import Modal from '@/components/Modal'` |
3 | ] |
4 | const exported = { |
5 | props: { |
6 | data: { |
7 | type: Object |
8 | }, |
9 | prefix: String |
10 | }, |
11 | methods: { |
12 | format(input) { |
13 | return input |
14 | .replace(/%cmd%/, this.prefix + this.data.name) |
15 | .replace(/%name%/, this.data.name) |
16 | .replace(/%prefix%/, this.prefix) |
17 | }, |
18 | close() { |
19 | this.$emit('close') |
20 | } |
21 | }, |
22 | computed: { |
23 | active() { |
24 | return this.data && this.data.name !== null && this.data.name !== undefined |
25 | } |
26 | } |
27 | } |
28 | // end |
29 | |
30 | function cleanCode(input) { |
31 | return input |
32 | .replace(/this\.([a-zA-Z$_]+)\(/g, '$1(') |
33 | .replace(/this\.([a-zA-Z$_]+)/g, '$1.value') |
34 | .replace(/\$emit/g, 'emit') |
35 | .replace(/\$(route|router)\.value/g, '$1') |
36 | } |
37 | |
38 | function objToStr(input) { |
39 | return JSON.stringify(input, (key, val) => { |
40 | console.log(val, typeof val, val.constructor.name) |
41 | if(typeof(val) === "function") { |
42 | val = `-${val.name}-` |
43 | } |
44 | return val |
45 | }, 2) |
46 | .replace(/"([^"]+)":/g, '$1:') |
47 | .replace(/"-([a-zA-Z]+)-"/g, '$1') |
48 | } |
49 | |
50 | function getProps() { |
51 | let output = "" |
52 | if(exported.props) { |
53 | output = `const props = defineProps(${objToStr(exported.props)})` |
54 | } |
55 | return output |
56 | } |
57 | |
58 | function getData() { |
59 | let output = "" |
60 | if(exported.data) { |
61 | let data = exported.data() |
62 | for(var key in data) { |
63 | let v = data[key] |
64 | if(v && typeof v === "object") { |
65 | v = objToStr(data[key]) |
66 | } |
67 | output += `let ${key} = ref(${v})\n` |
68 | } |
69 | } |
70 | return output |
71 | } |
72 | |
73 | function getWatch() { |
74 | let output = "" |
75 | if(exported.watch) { |
76 | for(const key in exported.watch) { |
77 | let func = exported.watch[key].handler.toString() |
78 | .replace(/handler\((.*)\)\s*{(.*)}/s, (match, p1, p2) => { |
79 | return `(${p1}) => { ${cleanCode(p2)} }` |
80 | }) |
81 | output += `watch(${key}, ${func})\n` |
82 | } |
83 | } |
84 | return output |
85 | } |
86 | |
87 | function getComputed() { |
88 | let output = "" |
89 | if(exported.computed) { |
90 | for(const key in exported.computed) { |
91 | let func = exported.computed[key].toString() |
92 | .replace(new RegExp(`${key}\\s*\\((.*)\\)\\s*{(.*)}`,'s'), (match, p1, p2) => { |
93 | return `(${p1}) => { ${cleanCode(p2)} }` |
94 | }) |
95 | output += `const ${key} = computed(${func})\n` |
96 | } |
97 | } |
98 | return output |
99 | } |
100 | |
101 | function getMethods() { |
102 | let output = "" |
103 | if(exported.methods) { |
104 | for(const key in exported.methods) { |
105 | let func = exported.methods[key].toString() |
106 | .replace(/([a-zA-Z\s]+\s)?([a-zA-Z_]+)\s*\(([a-zA-Z,\s]*)\)\s*{(.*)}/s, (match, p1, p2, p3, p4) => { |
107 | return `${p1||''}function ${p2}(${p3}) {\n${cleanCode(p4)}\n}\n` |
108 | }) |
109 | output += func |
110 | } |
111 | } |
112 | return output |
113 | } |
114 | |
115 | function getHooks() { |
116 | let output = "" |
117 | if(exported.created) { |
118 | const rawfunc = exported.created.toString() |
119 | const func = rawfunc.slice(rawfunc.indexOf("{") + 1, rawfunc.lastIndexOf("}")); |
120 | output += `onBeforeMount(() => {\n${cleanCode(func)}\n})` |
121 | } |
122 | if(exported.mounted) { |
123 | const rawfunc = exported.mounted.toString() |
124 | const func = rawfunc.slice(rawfunc.indexOf("{") + 1, rawfunc.lastIndexOf("}")); |
125 | output += `onMounted(() => {\n${cleanCode(func)}\n})` |
126 | } |
127 | return output |
128 | } |
129 | |
130 | |
131 | let output = header.join("\n") + `\nimport { ref, watch, computed, onBeforeMount, onMounted, defineEmits } from 'vue'\n` |
132 | output += `import { useRoute, useRouter } from 'vue-router'\n\n` |
133 | output += `const emit = defineEmits([])'\n` |
134 | output += `const route = useRoute()\nconst router = useRouter()\n` |
135 | output += getProps() + "\n" |
136 | output += getData() + "\n" |
137 | output += getWatch() + "\n" |
138 | output += getComputed() + "\n" |
139 | output += getMethods() + "\n" |
140 | output += getHooks() + "\n" |
141 | |
142 | console.log(output) |