优化一波生成,加载组件等

This commit is contained in:
吕金泽
2022-03-30 23:54:24 +08:00
parent 2e19ca3493
commit b91328dcc7
35 changed files with 460 additions and 547 deletions
+48 -35
View File
@@ -1,42 +1,55 @@
import { babelParse } from '@vue/compiler-sfc'
import { compileFile } from '@/compiler/sfc-compiler.js'
import { ElLoading } from 'element-plus'
import { ElLoading, ElMessage } from 'element-plus'
function appComponent(app, item){
export function appComponent(app, item){
var compiled = {}
compileFile('TestCode.vue', item.code, compiled)
var code = compiled.js
var ast = babelParse(code, {
sourceType: 'module'
})
var replaceCode = (node, subCode) => code.substring(0, node.start) + subCode + code.substring(node.end);
for(var i = ast.program.body.length - 1; i>=0; i--){
var node = ast.program.body[i]
if(node.type === 'ImportDeclaration'){
code = replaceCode(node, node.specifiers.map(it => `const ${it.local?.name || it.imported?.name || '*'} = ___magic__import__('${node.source.value}', '${it.imported?.name || '*'}');`).join('\r\n'));
} else if (node.type === 'ExportDefaultDeclaration'){
code = replaceCode(node, `return ${node.declaration.name}`)
}
}
code = `(function(){
${code}
})()`
var componentStyle = document.createElement("style");
componentStyle.innerHTML = compiled.css
document.head.appendChild(componentStyle);
app.component(item.name, eval(code))
}
const install = (app) => {
const loading = ElLoading.service({
lock: true,
background: 'rgba(255, 255, 255, 0)',
})
app.config.globalProperties.$post('/system/component/list').then((res) => {
res.data.forEach(it => {
appComponent(app, it)
if(compiled.errors.length > 0){
ElMessage({
type: 'error',
duration: 0,
showClose: true,
message: `
组件“${item.name}”发生错误:
${compiled.errors[0]}
`
})
loading.close()
})
throw compiled.errors[0]
}else{
var code = compiled.js
var ast = babelParse(code, {
sourceType: 'module'
})
var replaceCode = (node, subCode) => code.substring(0, node.start) + subCode + code.substring(node.end);
for(var i = ast.program.body.length - 1; i>=0; i--){
var node = ast.program.body[i]
if(node.type === 'ImportDeclaration'){
code = replaceCode(node, node.specifiers.map(it => `const ${it.local?.name || it.imported?.name || '*'} = ___magic__import__('${node.source.value}', '${it.imported?.name || '*'}');`).join('\r\n'));
} else if (node.type === 'ExportDefaultDeclaration'){
code = replaceCode(node, `return ${node.declaration.name}`)
}
}
code = `(function(){
${code}
})()`
var componentStyle = document.createElement("style");
componentStyle.innerHTML = compiled.css
document.head.appendChild(componentStyle);
app.component(item.name, eval(code))
}
}
export default install
//
// const install = (app) => {
// const loading = ElLoading.service({
// lock: true,
// background: 'rgba(255, 255, 255, 0)',
// })
// app.config.globalProperties.$post('/system/component/list').then((res) => {
// res.data.forEach(it => {
// appComponent(app, it)
// })
// loading.close()
// })
// }
// export default install
+24 -19
View File
@@ -1,26 +1,26 @@
function gen(groupPath, data){
var permissionPrefix = groupPath.replace('/', ':')
var html = `
<template>
<mb-list ref="magicList" v-bind="listOptions" />
<mb-dialog ref="formDialog" @confirm-click="magicForm.save($event)" width="50%">
var permissionPrefix = groupPath.replace(/^\//,'').replace(/\/\//, '/').replace('/', ':')
var html = `<template>
<mb-list ref="magicList" v-bind="listOptions" />
<mb-dialog ref="formDialog" :title="magicFormTitle" @confirm-click="magicForm.save($event)" width="50%">
<template #content>
<mb-form ref="magicForm" @reload="magicList.reload" v-bind="formOptions" />
<mb-form ref="magicForm" @reload="magicList.reload" v-bind="formOptions" />
</template>
</mb-dialog>
</template>
<script setup>
import { ref, reactive, getCurrentInstance } from 'vue'
</mb-dialog>
</template>
<script setup>
import { ref, reactive, getCurrentInstance, nextTick } from 'vue'
const { proxy } = getCurrentInstance()
const formDialog = ref()
const magicList = ref()
const magicForm = ref()
const magicFormTitle = ref()
const listOptions = reactive({
tools: [{
type: 'add',
permission: '${permissionPrefix}:save',
click: () => {
formOptions.detail.formData = null
magicFormTitle.value = '添加'
formDialog.value.show()
}
}],
@@ -70,8 +70,8 @@ function gen(groupPath, data){
type: 'text',
icon: 'ElEdit',
click: (row) => {
magicForm.value.getDetail(row.id)
formDialog.value.show()
magicFormTitle.value = '修改'
formDialog.value.show(() => magicForm.value.getDetail(row.id))
}
}, {
permission: '${permissionPrefix}:delete',
@@ -101,8 +101,7 @@ function gen(groupPath, data){
},
form: {
request: {
url: "${groupPath}/save",
method: "post"
url: "${groupPath}/save"
},
rows: [{
gutter: 24,
@@ -110,11 +109,18 @@ function gen(groupPath, data){
for(var i in data){
var d = data[i]
if(d.save){
var props = ''
if(d.dictType){
props = `props: {
type: '${d.dictType}'
}`
}
html += `{
span: 12,
name: '${d.columnName}',
label: '${d.columnComment}',
${d.component}
${d.component},
${props}
},`
}
}
@@ -123,8 +129,7 @@ function gen(groupPath, data){
}]
}
})
`
console.log(html)
</script>`
return html
}
export default gen