@sh-winter/vite-plugin-qiankun
TypeScript icon, indicating that this package has built-in type declarations

0.1.1 • Public • Published

简介

简化 vite 子应用接入 qiankun 的流程

快速开始

1. 安装 vite 插件

// vite.config.ts

import qiankun from '@sh-winter/vite-plugin-qiankun'
import { name as packageName } from './package.json'

export default {
  base: `/${packageName}/`
  plugins: [
    qiankun({ packageName })
  ]
}

2. 导出 qiankun 生命周期钩子

// main.ts

import { exportLifeCycleHooks, qiankunWindow } from '@sh-winter/vite-plugin-qiankun/dist/helper'

// some code

function render(root: Element | Document = document) {
  app = createApp(App)
  app.mount(root.querySelector('#app'))
}

exportLifeCycleHooks({
  bootstrap() {
    // do nothing.
  },
  mount(props: { container: Element }) {
    render(props.container);
  },
  unmount() {
    app?.unmount();
  }
})

if (!qiankunWindow.__POWERED_BY_QIANKUN__) {
  render();
}

3. 注意事项

  1. es module 会导致 qiankun 沙箱失效,如果想要访问 qiankun 提供的代理 window,请使用 qiankunWindow
import { qiankunWindow } from '@sh-winter/vite-plugin-qiankun/dist/helper'
  1. 如果是 vue 子应用,开发环境下需要额外引入 vue-template-compiler transform 插件:transformAssetUrl
// vite.config.ts

import qiankun, { transformAssetUrl } from '@sh-winter/vite-plugin-qiankun'

export default {
  // ...
  plugins: [
    vue({
      template: {
        compilerOptions: {
          nodeTransforms: [transformAssetUrl as any]
        }
      }
    }),
    qiankun({ packageName })
  ]
}

查看示例

yarn example

Package Sidebar

Install

npm i @sh-winter/vite-plugin-qiankun

Weekly Downloads

4

Version

0.1.1

License

MIT

Unpacked Size

26.5 kB

Total Files

8

Last publish

Collaborators

  • sh-winter