Last active 1741880026

Revision 49fcb069db89f4639795cf0cfed78bc7647dccb5

convert-vue2-vue3-comp.js Raw
1const header = [
2 `import Modal from '@/components/Modal'`
3]
4const 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
30function 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
38function 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
50function getProps() {
51 let output = ""
52 if(exported.props) {
53 output = `const props = defineProps(${objToStr(exported.props)})`
54 }
55 return output
56}
57
58function 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
73function 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
87function 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
101function 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
115function 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
131let output = header.join("\n") + `\nimport { ref, watch, computed, onBeforeMount, onMounted, defineEmits } from 'vue'\n`
132output += `import { useRoute, useRouter } from 'vue-router'\n\n`
133output += `const emit = defineEmits([])'\n`
134output += `const route = useRoute()\nconst router = useRouter()\n`
135output += getProps() + "\n"
136output += getData() + "\n"
137output += getWatch() + "\n"
138output += getComputed() + "\n"
139output += getMethods() + "\n"
140output += getHooks() + "\n"
141
142console.log(output)