微应用部署说明文档
该文档仅限于基座应用以及微应用的路由都使用
hash模式的微应用部署该文档仅限于微应用部署到同一个服务器(同一个 IP 和端口)
一、打包
通常的做法是主应用部署在一级目录,微应用部署在二/三级目录。
微应用想部署在非根目录,在微应用打包之前必须配置 webpack 构建时的 publicPath 为目录名称。
部署之后注意三点:
activeRule不能和微应用的真实访问路径一样,否则在主应用页面刷新会直接变成微应用页面。- 微应用的真实访问路径就是微应用的
entry,entry可以为相对路径。 - 微应用的
entry路径最后面的/不可省略,否则publicPath会设置错误,例如子项的访问路径是http://localhost:8080/app1,那么entry就是http://localhost:8080/app1/。
二、部署
1.微应用都放在在一个特殊名称(不会和微应用重名)的文件夹下
假设我们有一个主应用和 2 个微应用(分别为 heaven-sub-app1、heaven-sub-app1 ),打包后如下放置:
└── html/ # 根文件夹|├── child/ # 存放所有微应用的文件夹| ├── heaven-sub-app1/ # 存放微应用 heaven-sub-app1 的文件夹| ├── heaven-sub-app2/ # 存放微应用 heaven-sub-app2 的文件夹├── index.html # 主应用的index.html├── css/ # 主应用的css文件夹├── js/ # 主应用的js文件夹
此时需要设置微应用构建时的 publicPath,然后才能打包放到对应的目录里。
| 项目 | publicPath | 真实访问路径 |
|---|---|---|
| heaven-sub-app1 | /child/heaven-sub-app1/ | http://localhost:8080/child/heaven-sub-app1/ |
| heaven-sub-app2 | /child/heaven-sub-app2/ | http://localhost:8080/child/heaven-sub-app2/ |
此时 heaven-sub-app1 vue.config.js 代码是这样的。注:${name} 为当前项目名称。
const { defineConfig } = require('@vue/cli-service');const path = require('path');const { name } = require('./package.json');const resolve = (dir) => path.join(__dirname, dir);module.exports = defineConfig({publicPath: `/child/${name}`,devServer: {port: 9081,// 允许跨域让基座加载子应用headers: {'Access-Control-Allow-Origin': '*'}},transpileDependencies: true,chainWebpack: (config) => {// 配置别名config.resolve.alias.set('@', resolve('src'));},configureWebpack: {// 配置打包格式output: {library: `${name}-[name]`,libraryTarget: 'umd',// 后续 libraryTarget 可能会废弃// library: {// name: `${name}-[name]`,// type: 'umd'// },// webpack5 jsonpFunction 已废弃// jsonpFunction: `webpackJsonp_${name}`chunkLoadingGlobal: `webpackJsonp_${name}`}}});
2.此时nginx配置是这样的。
server {listen 80;server_name localhost;location / {root /home/html/heaven-main-app;index index.html index.htm;}......}
至此主应用已经和微应用都能跑起来了。
