Commit a1371c0d by zhuxichen

chore: update

parent 584ad1e8
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
<div class="about"> <div class="about">
<h1>This is an about page</h1> <h1>This is an about page</h1>
<h2>{{ emitStr }}</h2> <h2>{{ emitStr }}</h2>
<DynamicComCodeLoader @cb="fn" /> <DynamicComCodeLoader @cb="fn" componentKey="key" />
</div> </div>
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
......
...@@ -5,10 +5,11 @@ ...@@ -5,10 +5,11 @@
v-on="$attrs" v-on="$attrs"
v-if="asyncComponent" v-if="asyncComponent"
/> />
<div v-if="errorList.length > 0" class="error-remote-com">代码获取错误</div>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { defineAsyncComponent, ref } from 'vue' import { defineAsyncComponent, ref, shallowRef, watch } from 'vue'
import { loadModule } from 'vue3-sfc-loader' import { loadModule } from 'vue3-sfc-loader'
import { moduleCache } from './moduleCache' import { moduleCache } from './moduleCache'
import { import {
...@@ -19,9 +20,50 @@ import { ...@@ -19,9 +20,50 @@ import {
} from '@vue/compiler-sfc' } from '@vue/compiler-sfc'
const errorList = ref<Array<CompilerError | SyntaxError | unknown>>([]) const errorList = ref<Array<CompilerError | SyntaxError | unknown>>([])
const validaCode = (v: string) => {
const { errors: parseError, descriptor } = parse(v) const props = defineProps<{ componentKey: string }>()
if (parseError.length > 0) return errorList.value.push(...parseError)
const asyncComponent = shallowRef()
const getCode = async () => {
// 这里应该是一个接口,通过key去拿code
const res = await fetch('http://localhost:8080/remote-component.vue')
const code = await res.text()
return code
}
const generateRemoteComponent = async () => {
errorList.value = []
const code = await getCode()
const randomFileName = `${Date.now()}.vue`
const file = {
files: {
[randomFileName]: {
getContentData: () => code,
},
},
}
const options = {
moduleCache,
async getFile(url: string) {
return file.files[url]
},
addStyle(textContent: unknown) {
const style = Object.assign(document.createElement('style'), {
textContent,
})
const ref = document.head.getElementsByTagName('style')[0] || null
document.head.insertBefore(style, ref)
},
}
const { errors: parseError, descriptor } = parse(code)
if (parseError.length > 0) {
console.error(parseError)
return errorList.value.push(...parseError)
}
try { try {
if (descriptor?.scriptSetup?.content) { if (descriptor?.scriptSetup?.content) {
compileScript(descriptor, { id: 'remote-component' }) compileScript(descriptor, { id: 'remote-component' })
...@@ -29,37 +71,39 @@ const validaCode = (v: string) => { ...@@ -29,37 +71,39 @@ const validaCode = (v: string) => {
id: 'remote-component', id: 'remote-component',
...descriptor, ...descriptor,
}) })
// eslint-disable-next-line @typescript-eslint/no-unused-expressions if (errors.length > 0) {
errors.length > 0 && new Error(errors.join('\n')) new Error(errors.join('\n'))
}
} }
} catch (error) { } catch (error) {
console.error(error)
errorList.value.push(error) errorList.value.push(error)
} }
if (errorList.value.length) return
asyncComponent.value = defineAsyncComponent(() =>
loadModule(randomFileName, options),
)
} }
const options = {
moduleCache,
async getFile(url: string) {
const res = await fetch(url)
const code = await res.text()
console.log(code)
return code // 确保 generateRemoteComponent 已定义后再调用
watch(
() => props.componentKey,
() => {
if (props.componentKey) {
console.log(props.componentKey)
generateRemoteComponent()
}
}, },
// eslint-disable-next-line @typescript-eslint/no-explicit-any {
addStyle(textContent: any) { immediate: true,
const style = Object.assign(document.createElement('style'), {
textContent,
})
const ref = document.head.getElementsByTagName('style')[0] || null
document.head.insertBefore(style, ref)
}, },
} )
const asyncComponent = defineAsyncComponent(async () => {
const res = await loadModule(
'http://localhost:8080/remote-component.vue',
options,
)
return res
})
</script> </script>
<style scoped>
.error-remote-com {
color: red;
}
</style>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment