全局注册 动态组件

This commit is contained in:
吕金泽
2022-03-05 11:29:26 +08:00
parent 54a2f86ab6
commit b630ed3828
8 changed files with 56 additions and 92 deletions
@@ -0,0 +1,36 @@
import { babelParse } from '@vue/compiler-sfc'
import { compileFile } from '@/compiler/sfc-compiler.js'
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) => {
app.config.globalProperties.$get('/component/list', { size: 999999 }).then((res) => {
res.data.list.forEach(it => {
appComponent(app, it)
})
})
}
export default install