ice-lab / icestark

:tiger: Micro Frontends solution for large application(面向大型应用的微前端解决方案),站点国内镜像:https://icestark.gitee.io
https://micro-frontends.ice.work
MIT License
2.04k stars 174 forks source link

微模块 vite4 + vue3 构建微模块 主应用使用报错问题 #752

Closed laibin-wang closed 1 week ago

laibin-wang commented 3 months ago

微模块 vite4 + vue3 构建微模块 主应用使用报错问题

微模块 以及vite 生成 改造

import { createApp, type App } from 'vue'

export { default } from './branch-detail/index.vue'

let vue: App<Element> | null = null

export function mount(ModuleCom: any, targetNode: Element, props: any) {
  vue = createApp(ModuleCom, props)
  vue.mount(targetNode)
}

export function unmount(targetNode: Element) {
  vue?.unmount()
  vue = null
  targetNode.innerHTML = ''
}

组件

<template>
  <div>branch-detailbranch-detailbranch-detailbranch-detail</div>
</template>

vite.config.ts

import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
  ],
  build: {
    lib: {
      entry: './src/main.ts',
      name: 'microModule',
      fileName: 'micro-module'
    },
    rollupOptions: {
      preserveEntrySignatures: 'exports-only',
    }
  },
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  }
})

主应用引入。也是基于vite4+vue

<template>
  <div class="home">
    <div>About page</div>
    <div>主应用注册微模块</div>
    <div ref="microAppDom"></div>
    <MicroModule name="module-a"></MicroModule>
  </div>
</template>

<script setup lang="ts">
import { MicroModule, mountModule, registerModule, registerModules } from '@ice/stark-module/lib/modules';
import { ref, onMounted } from 'vue'

const microAppDom = ref<HTMLDivElement | null>(null)

const moduleInfo = {
  name: 'microModule',
  url: 'http://localhost:4000/micro-module.js',
}
onMounted(() => {
  mountModule(moduleInfo, microAppDom.value, {
    data: '主应用测试数据',
  })
})

</script>

报错 image

ClarkXia commented 2 months ago

vite 构建的产物是 esm 规范的,微模块加载暂时不支持该规范

laibin-wang commented 2 months ago

这是来自QQ邮箱的假期自动回复邮件。你好,我最近正在休假中,无法亲自回复你的邮件。我将在假期结束后,尽快给你回复。

laibin-wang commented 1 week ago

这是来自QQ邮箱的假期自动回复邮件。你好,我最近正在休假中,无法亲自回复你的邮件。我将在假期结束后,尽快给你回复。