Git Product home page Git Product logo

vite-plugin-qiankun-1's Introduction

简介

简化 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 })
  ]
}

查看示例

cd example
yarn

# 启动主应用(react)
yarn run:main
# or
yarn run:main-build

# 启动子应用(vue3)
yarn run:micro
# or
yarn run:micro-build

vite-plugin-qiankun-1's People

Contributors

sh-winter avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.