Last active 1741880026

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