uview-plus
Version:
零云®uview-plus已兼容vue3支持多语言,120+全面的组件和便捷的工具会让您信手拈来。近期新增拖动排序、条码、图片裁剪、下拉刷新、虚拟列表、签名、Markdown等。
45 lines (36 loc) • 1.25 kB
JavaScript
import { MagicString } from 'vue/compiler-sfc'
import { parseSFC } from './utils.js'
export async function registerUpApp(
code,
fileName = 'App.up',
rootToastHostPath = './uni_modules/uview-plus/libs/root/root-toast-host.vue'
) {
const ms = new MagicString(code)
const importCode = `import GlobalUpRoot from "./${fileName}.vue";
import UpRootToastHost from "${rootToastHostPath}";`
const vueUseComponentCode = 'app.component("global-up-root", GlobalUpRoot);\napp.component("ku-root-toast-host", UpRootToastHost);'
ms.prepend(`${importCode}\n`).replace(
/(createApp[\s\S]*?)(return\s\{\s*app)/,
`$1${vueUseComponentCode}\n$2`,
)
return ms
}
export async function rebuildUpApp(code, enabledVirtualHost = false) {
const ms = new MagicString(code)
const rootTagNameRE = /<(UpRootView|up-root-view)(?:\s*\/>|><\/\1>)/
ms.replace(rootTagNameRE, '<slot />\n <ku-root-toast-host />')
if (enabledVirtualHost) {
const sfc = await parseSFC(code)
if (sfc.script) {
return ms
}
const langType = sfc.scriptSetup?.lang
ms.append(`<script ${langType ? `lang="${langType}"` : ''}>
export default {
options: {
virtualHost: true,
}
}\n</script>`)
}
return ms
}