From d4b94707489198dd31d13e5de7bfb211c433a16a Mon Sep 17 00:00:00 2001 From: ClarkXia Date: Thu, 18 Jan 2024 05:52:16 +0000 Subject: [PATCH] =?UTF-8?q?Deploying=20to=20gh-pages=20from=20@=20ice-lab/?= =?UTF-8?q?icestark@1a0d63660eda298bb5364e50ba7b9c044061e4b6=20?= =?UTF-8?q?=F0=9F=9A=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 404.html | 8 ++++---- .../{4a0f655e.b5111c40.js => 4a0f655e.86430c9b.js} | 2 +- .../{679a1ab7.0ea9605a.js => 679a1ab7.701a2731.js} | 2 +- .../{73c0d1f2.928b5dbc.js => 73c0d1f2.d768cb32.js} | 2 +- assets/js/a6c6feff.45ceaceb.js | 1 + assets/js/a6c6feff.89e1f6fa.js | 1 - .../{b0b9b2e4.eaca5c0b.js => b0b9b2e4.cbccc611.js} | 2 +- .../{c1cc29be.a97a0628.js => c1cc29be.c5f29862.js} | 2 +- assets/js/d4b1b049.71dd2fb1.js | 1 - assets/js/d4b1b049.abd30821.js | 1 + .../{fac9664c.9d768e0e.js => fac9664c.9ea3e450.js} | 2 +- assets/js/fdc41984.05355ff7.js | 1 + assets/js/fdc41984.d128b516.js | 1 - assets/js/{main.5c3e0571.js => main.74cefd04.js} | 4 ++-- ...1.js.LICENSE.txt => main.74cefd04.js.LICENSE.txt} | 0 ...ime~main.44d5b015.js => runtime~main.00bfc000.js} | 2 +- blog/00-icestark-2-4-0/index.html | 10 +++++----- blog/01-icestark-2-5-0/index.html | 12 ++++++------ blog/02-icestark-2-6-0/index.html | 12 ++++++------ blog/03-icestark-2-7-0/index.html | 12 ++++++------ blog/atom.xml | 10 +++++----- blog/index.html | 10 +++++----- blog/rss.xml | 10 +++++----- blog/tags/announcement/index.html | 10 +++++----- blog/tags/index.html | 10 +++++----- blog/tags/v-2/index.html | 10 +++++----- docs/api/ice-stark-app/index.html | 8 ++++---- docs/api/ice-stark/index.html | 8 ++++---- docs/api/stark-module/index.html | 8 ++++---- docs/faq/index.html | 8 ++++---- docs/guide/advanced/communication/index.html | 8 ++++---- docs/guide/advanced/performance/index.html | 8 ++++---- docs/guide/advanced/sandbox/index.html | 8 ++++---- docs/guide/concept/child/index.html | 8 ++++---- docs/guide/concept/layout/index.html | 8 ++++---- docs/guide/concept/workflow/index.html | 8 ++++---- docs/guide/ecosystem/index.html | 8 ++++---- docs/guide/index.html | 8 ++++---- docs/guide/micro-module/index.html | 8 ++++---- docs/guide/upgrade/index.html | 8 ++++---- docs/guide/use-child/others/index.html | 8 ++++---- docs/guide/use-child/react/index.html | 8 ++++---- docs/guide/use-child/vue/index.html | 8 ++++---- docs/guide/use-layout/react/index.html | 8 ++++---- docs/guide/use-layout/vue/index.html | 8 ++++---- error/index.html | 8 ++++---- index.html | 8 ++++---- search/index.html | 8 ++++---- 48 files changed, 157 insertions(+), 157 deletions(-) rename assets/js/{4a0f655e.b5111c40.js => 4a0f655e.86430c9b.js} (61%) rename assets/js/{679a1ab7.0ea9605a.js => 679a1ab7.701a2731.js} (99%) rename assets/js/{73c0d1f2.928b5dbc.js => 73c0d1f2.d768cb32.js} (98%) create mode 100644 assets/js/a6c6feff.45ceaceb.js delete mode 100644 assets/js/a6c6feff.89e1f6fa.js rename assets/js/{b0b9b2e4.eaca5c0b.js => b0b9b2e4.cbccc611.js} (96%) rename assets/js/{c1cc29be.a97a0628.js => c1cc29be.c5f29862.js} (98%) delete mode 100644 assets/js/d4b1b049.71dd2fb1.js create mode 100644 assets/js/d4b1b049.abd30821.js rename assets/js/{fac9664c.9d768e0e.js => fac9664c.9ea3e450.js} (65%) create mode 100644 assets/js/fdc41984.05355ff7.js delete mode 100644 assets/js/fdc41984.d128b516.js rename assets/js/{main.5c3e0571.js => main.74cefd04.js} (99%) rename assets/js/{main.5c3e0571.js.LICENSE.txt => main.74cefd04.js.LICENSE.txt} (100%) rename assets/js/{runtime~main.44d5b015.js => runtime~main.00bfc000.js} (58%) diff --git a/404.html b/404.html index 662d0c0a..602310cb 100644 --- a/404.html +++ b/404.html @@ -11,13 +11,13 @@ Page Not Found | icestark - - + +
Skip to main content

Page Not Found

We could not find what you were looking for.

Please contact the owner of the site that linked you to the original URL and let them know their link is broken.

- - + + \ No newline at end of file diff --git a/assets/js/4a0f655e.b5111c40.js b/assets/js/4a0f655e.86430c9b.js similarity index 61% rename from assets/js/4a0f655e.b5111c40.js rename to assets/js/4a0f655e.86430c9b.js index 8c2f81e2..4b36ee04 100644 --- a/assets/js/4a0f655e.b5111c40.js +++ b/assets/js/4a0f655e.86430c9b.js @@ -1 +1 @@ -"use strict";(self.webpackChunksite=self.webpackChunksite||[]).push([[801],{5641:function(e){e.exports=JSON.parse('{"title":"Recent posts","items":[{"title":"icestark 2.4.0","permalink":"/blog/00-icestark-2-4-0"},{"title":"icestark 2.5.0","permalink":"/blog/01-icestark-2-5-0"},{"title":"icestark 2.7.0","permalink":"/blog/03-icestark-2-7-0"},{"title":"icestark 2.6.0","permalink":"/blog/02-icestark-2-6-0"}]}')}}]); \ No newline at end of file +"use strict";(self.webpackChunksite=self.webpackChunksite||[]).push([[801],{5641:function(e){e.exports=JSON.parse('{"title":"Recent posts","items":[{"title":"icestark 2.4.0","permalink":"/blog/00-icestark-2-4-0"},{"title":"icestark 2.5.0","permalink":"/blog/01-icestark-2-5-0"},{"title":"icestark 2.6.0","permalink":"/blog/02-icestark-2-6-0"},{"title":"icestark 2.7.0","permalink":"/blog/03-icestark-2-7-0"}]}')}}]); \ No newline at end of file diff --git a/assets/js/679a1ab7.0ea9605a.js b/assets/js/679a1ab7.701a2731.js similarity index 99% rename from assets/js/679a1ab7.0ea9605a.js rename to assets/js/679a1ab7.701a2731.js index f8c21fe4..06351e21 100644 --- a/assets/js/679a1ab7.0ea9605a.js +++ b/assets/js/679a1ab7.701a2731.js @@ -1 +1 @@ -"use strict";(self.webpackChunksite=self.webpackChunksite||[]).push([[5474],{3905:function(e,t,n){n.d(t,{Zo:function(){return s},kt:function(){return m}});var r=n(7294);function a(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function i(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function p(e){for(var t=1;t=0||(a[n]=e[n]);return a}(e,t);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);for(r=0;r=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(a[n]=e[n])}return a}var o=r.createContext({}),c=function(e){var t=r.useContext(o),n=t;return e&&(n="function"==typeof e?e(t):p(p({},t),e)),n},s=function(e){var t=c(e.components);return r.createElement(o.Provider,{value:t},e.children)},u={inlineCode:"code",wrapper:function(e){var t=e.children;return r.createElement(r.Fragment,{},t)}},k=r.forwardRef((function(e,t){var n=e.components,a=e.mdxType,i=e.originalType,o=e.parentName,s=l(e,["components","mdxType","originalType","parentName"]),k=c(n),m=a,d=k["".concat(o,".").concat(m)]||k[m]||u[m]||i;return n?r.createElement(d,p(p({ref:t},s),{},{components:n})):r.createElement(d,p({ref:t},s))}));function m(e,t){var n=arguments,a=t&&t.mdxType;if("string"==typeof e||a){var i=n.length,p=new Array(i);p[0]=k;var l={};for(var o in t)hasOwnProperty.call(t,o)&&(l[o]=t[o]);l.originalType=e,l.mdxType="string"==typeof e?e:a,p[1]=l;for(var c=2;c")," \u6dfb\u52a0 ",(0,i.kt)("inlineCode",{parentName:"p"},'crossorigin="anonymous"')," \u5c5e\u6027\u3002"),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-js"},' icestark 2.4.0 | icestark - - + +
-

icestark 2.4.0

Announcing icestark 2.4.0#

icestark 2.4.0 发布的主要特性有:

解决 Script Error 错误#

微应用上线运行遇到错误时,监控平台通常会捕获到 Script Error 的输出:

script error

处于安全考虑,浏览器会刻意隐藏其他域的 JavaScript 文件抛出的具体错误信息,来避免敏感信息被不受控制的第三方脚本捕获。因此,浏览器只允许同域下的脚本捕获具体错误信息,而其他脚本只知道发生了一个错误,但无法获知错误的具体内容。

为解决跨域捕获 JavaScript 异常,需要为 <script > 添加 crossorigin="anonymous" 属性。

<script src="http://another-domain.com/app.js" crossorigin="anonymous"></script>

在最新的 icestark 版本中,可以通过 scriptAttributes 为微应用的 JavaScript 文件添加这一属性。

// 微应用配置
{
name: 'app1',
activePath: '/seller',
url: [
'//unpkg.com/app1/js/chunk.js',
'//unpkg.com/app1/js/index.js',
'//unpkg.com/app1/css/index.css',
],
...
// 为微应用 app1 的所有 js 资源添加 crossorigin="anonymous"
scriptAttributes: ["crossorigin=anonymous"],
// or
// 为 `//unpkg.com/app1/js/chunk.js` 资源添加 crossorigin="anonymous"
scriptAttributes: (url) => url.includes('chunk') ? ["crossorigin=anonymous"] : [],
}

有关 scriptAttributes 使用的更多内容,可参考 api - scriptAttributes

此外,还可以通过 scriptAttributes 来为某个 JavaScript 文件做特殊的标记。比如:

...
// 为微应用的 chunk 资源标记类型
scriptAttributes: (url) => url.includes('chunk') ? ["resourceType=chunk"] : [],
...

还需要注意的是,以下属性在 scriptAttributes 中不可设置:

  • async
  • id
  • type
  • src
  • async
  • icestark

在开发阶段,icestark 会进行 error 提示,比如试图通过 scriptAttributes 字段修改 JavaScript 文件的 src

@ice/stark-data 支持 Symbol key#

@ice/stark-data 是 icestark 提供的应用间通信解决方案。现在,已支持 Symbol 作为标识 key。

如果您对 Symbol 还不熟悉,可以先在这里了解下。

使用方式如下:

(1)在主应用中监听事件

import { event } from '@ice/stark-data';
+

icestark 2.4.0

Announcing icestark 2.4.0#

icestark 2.4.0 发布的主要特性有:

解决 Script Error 错误#

微应用上线运行遇到错误时,监控平台通常会捕获到 Script Error 的输出:

script error

处于安全考虑,浏览器会刻意隐藏其他域的 JavaScript 文件抛出的具体错误信息,来避免敏感信息被不受控制的第三方脚本捕获。因此,浏览器只允许同域下的脚本捕获具体错误信息,而其他脚本只知道发生了一个错误,但无法获知错误的具体内容。

为解决跨域捕获 JavaScript 异常,需要为 <script > 添加 crossorigin="anonymous" 属性。

<script src="http://another-domain.com/app.js" crossorigin="anonymous"></script>

在最新的 icestark 版本中,可以通过 scriptAttributes 为微应用的 JavaScript 文件添加这一属性。

// 微应用配置
{
name: 'app1',
activePath: '/seller',
url: [
'//unpkg.com/app1/js/chunk.js',
'//unpkg.com/app1/js/index.js',
'//unpkg.com/app1/css/index.css',
],
...
// 为微应用 app1 的所有 js 资源添加 crossorigin="anonymous"
scriptAttributes: ["crossorigin=anonymous"],
// or
// 为 `//unpkg.com/app1/js/chunk.js` 资源添加 crossorigin="anonymous"
scriptAttributes: (url) => url.includes('chunk') ? ["crossorigin=anonymous"] : [],
}

有关 scriptAttributes 使用的更多内容,可参考 api - scriptAttributes

此外,还可以通过 scriptAttributes 来为某个 JavaScript 文件做特殊的标记。比如:

...
// 为微应用的 chunk 资源标记类型
scriptAttributes: (url) => url.includes('chunk') ? ["resourceType=chunk"] : [],
...

还需要注意的是,以下属性在 scriptAttributes 中不可设置:

  • async
  • id
  • type
  • src
  • async
  • icestark

在开发阶段,icestark 会进行 error 提示,比如试图通过 scriptAttributes 字段修改 JavaScript 文件的 src

@ice/stark-data 支持 Symbol key#

@ice/stark-data 是 icestark 提供的应用间通信解决方案。现在,已支持 Symbol 作为标识 key。

如果您对 Symbol 还不熟悉,可以先在这里了解下。

使用方式如下:

(1)在主应用中监听事件

import { event } from '@ice/stark-data';
const sym = Symbol.for('freshMessage');
event.on('sym', () => {
// 重新获取消息数
})

(2)在微应用触发事件

import { event } from '@ice/stark-data';
const sym = Symbol.for('freshMessage');
@@ -24,7 +24,7 @@
export default Detail;

注意,您需要使用下面的指令将 build-plugin-icestark 升级到最新版本:

$npm i build-plugin-icestark@latest -D

对于还在使用 icestark 1.x 的用户,请查阅升级指南

增强的沙箱能力#

在最新的版本中,我们修复了 eval 函数在沙箱中执行的一些异常行为。该行为是 eval 函数错误被 bind 导致执行作用域为全局作用域。您可以通过下面的代码简单验证:

function foo (obj) {
eval.bind(window)('console.log(obj)');
}
boo({a: 1});
// Uncaught ReferenceError: obj is not defined

在最新的版本中,您可以在代码中正常使用 eval 函数,并得到它的预期行为。

在之前的版本中,沙箱中的 假值 不会被捕获。比如:

// 在沙箱中执行
window.a = false;
console.log(a);
// undefined

在最近的版本中,我们修正了这一行为。

后续的版本计划#

我们会持续扩展 icestark 的能力,提升微前端体验。在接下来的版本中,我们会:

  • 优化 icestark 本地开发以及沙箱的调试体验 (issue);
  • React ComponentApi 的使用方式对齐所有字段。这可能会导致某些字段在未来的版本中处于 @deprecated 阶段 (rfc);
  • 会修复使用 Prompt 组件二次弹框的问题;(issue);
  • 提供更简单、方便样式隔离方案。
- - + + \ No newline at end of file diff --git a/blog/01-icestark-2-5-0/index.html b/blog/01-icestark-2-5-0/index.html index 88bfa90f..84797541 100644 --- a/blog/01-icestark-2-5-0/index.html +++ b/blog/01-icestark-2-5-0/index.html @@ -11,16 +11,16 @@ icestark 2.5.0 | icestark - - + +
-

icestark 2.5.0

Announcing icestark 2.5.0#

在 2.5.0 版本中,我们主要统一了在使用 APIReact Component 接入的一些字段使用方式,并集中修复了一些常见问题。本次更新主要包括:

对齐 API 和 React Component 使用字段#

在之前的版本中,AppConfig 有些字段和 <AppRoute /> 接收的 props 存在一定的差异,对用户的使用造成了困恼。因此,我们整体梳理了相关字段,主要的变更如下:

1. <AppRoute />path 字段废弃,请使用 activePath 字段。#

activePathappConfig 定义的基础字段,除了支持 path 所有配置能力外,还支持函数写法,可自定义路由匹配逻辑:

{
name: 'app',
activePath: (url) => {
return url.includes('/seller'); // 当路由匹配上 /seller,则激活应用
}
}

值得注意的是,当使用函数写法的 activePath 是,需要有 name 字段标识,否则会有 Error 提示;对于其他非函数写法,icestark 会默认根据 activePath 的配置生成一个 name 值。

info

建议在 配置微应用 时,添加 name 字段。

2. AppConfig 提供 basename 字段#

basename 字段可以方便地自定义微应用路由的 basename。我们在 <AppRoute /> 的 Props 支持了这一字段,但是在 AppConfig 并没有支持这一字段。比如,当使用 createMicroApp 加载微应用时,需要通过主动调用 setBasename 来设置 basename

import { createMicroApp } from '@ice/stark';
import { setBasename } from '@ice/stark-app';
+

icestark 2.5.0

Announcing icestark 2.5.0#

在 2.5.0 版本中,我们主要统一了在使用 APIReact Component 接入的一些字段使用方式,并集中修复了一些常见问题。本次更新主要包括:

对齐 API 和 React Component 使用字段#

在之前的版本中,AppConfig 有些字段和 <AppRoute /> 接收的 props 存在一定的差异,对用户的使用造成了困恼。因此,我们整体梳理了相关字段,主要的变更如下:

1. <AppRoute />path 字段废弃,请使用 activePath 字段。#

activePathappConfig 定义的基础字段,除了支持 path 所有配置能力外,还支持函数写法,可自定义路由匹配逻辑:

{
name: 'app',
activePath: (url) => {
return url.includes('/seller'); // 当路由匹配上 /seller,则激活应用
}
}

值得注意的是,当使用函数写法的 activePath 是,需要有 name 字段标识,否则会有 Error 提示;对于其他非函数写法,icestark 会默认根据 activePath 的配置生成一个 name 值。

info

建议在 配置微应用 时,添加 name 字段。

2. AppConfig 提供 basename 字段#

basename 字段可以方便地自定义微应用路由的 basename。我们在 <AppRoute /> 的 Props 支持了这一字段,但是在 AppConfig 并没有支持这一字段。比如,当使用 createMicroApp 加载微应用时,需要通过主动调用 setBasename 来设置 basename

import { createMicroApp } from '@ice/stark';
import { setBasename } from '@ice/stark-app';
setBasename('/seller');
createMicroApp({
name: 'app',
activePath: '/seller',
url: ['/js/index.js'],
container: ref.current;
});

现在,你可以使用 basename 字段:

import { createMicroApp } from '@ice/stark';
-
createMicroApp({
name: 'app',
activePath: '/seller',
basename: '/seller',
url: ['/js/index.js'],
container: ref.current;
});

3. 在 <AppRouter /> 中使用 onLoadingApp、onFinishLoading、onError 等 Hooks#

在之前的版本中,使用 <AppRoute /> 不太方便对微应用执行的各个阶段进行监控或埋点(虽然这些能力均在 API 中支持)。因此,我们也在 <AppRoute /> 中透出了这些 Hooks。

更多有关 对齐 API 和 React Component 使用字段 可参见 RFC

重构路由匹配算法#

在新版本中,我们对 路由匹配算法 进行了重构。重构之后的版本并不会影响现有的代码功能,对于这一功能的变更,我们进行了充分的测试。但如果您的应用因此受到了一些影响,欢迎通过 issue 来告知我们。

优化 icestark 本地开发以及沙箱的调试体验#

当我们本地开发时候,source map 对定位源码非常有效。但是使用 fetch 来加载 js 资源时候,由于当前 origin 是主应用的 origin,导致 source map 文件加载失败。如图:

Source Map Revision 3 Proposal 中,我们找到解决这个问题的一些蛛丝马迹,通过在代码末尾添加 //# sourceURL=<url> */ 解决这个问题。

有两点需要注意:

  1. 该方式对 sandbox 同样有效,很好地缓解了 sandbox 的定位难题
  2. 如果是通过 <script /> 标签加载的资源,不受该问题限制

修复使用 Prompt 组件二次弹框的问题#

当微应用使用 ReactRouterDOM 的 Prompt 组件时,会出现两次 confirm 框确认,如图:

新版本中,我们修复了这一异常行为。可以访问 从 Prompt 来看微前端路由劫持原理 了解我们对一问题的追溯。

后续的版本计划#

我们会持续扩展 icestark 的能力,提升微前端体验。在接下来的版本中,我们会:

  • 提供更优的方式接入 es module 微应用 (rfc)
  • 优化 icestark 的错误提示信息 (issue)
  • 给新手用户提供更简单的接入指导
  • 提供多页签的微应用模板
- - +
createMicroApp({
name: 'app',
activePath: '/seller',
basename: '/seller',
url: ['/js/index.js'],
container: ref.current;
});

3. 在 <AppRouter /> 中使用 onLoadingApp、onFinishLoading、onError 等 Hooks#

在之前的版本中,使用 <AppRoute /> 不太方便对微应用执行的各个阶段进行监控或埋点(虽然这些能力均在 API 中支持)。因此,我们也在 <AppRoute /> 中透出了这些 Hooks。

更多有关 对齐 API 和 React Component 使用字段 可参见 RFC

重构路由匹配算法#

在新版本中,我们对 路由匹配算法 进行了重构。重构之后的版本并不会影响现有的代码功能,对于这一功能的变更,我们进行了充分的测试。但如果您的应用因此受到了一些影响,欢迎通过 issue 来告知我们。

优化 icestark 本地开发以及沙箱的调试体验#

当我们本地开发时候,source map 对定位源码非常有效。但是使用 fetch 来加载 js 资源时候,由于当前 origin 是主应用的 origin,导致 source map 文件加载失败。如图:

Source Map Revision 3 Proposal 中,我们找到解决这个问题的一些蛛丝马迹,通过在代码末尾添加 //# sourceURL=<url> */ 解决这个问题。

有两点需要注意:

  1. 该方式对 sandbox 同样有效,很好地缓解了 sandbox 的定位难题
  2. 如果是通过 <script /> 标签加载的资源,不受该问题限制

修复使用 Prompt 组件二次弹框的问题#

当微应用使用 ReactRouterDOM 的 Prompt 组件时,会出现两次 confirm 框确认,如图:

新版本中,我们修复了这一异常行为。可以访问 从 Prompt 来看微前端路由劫持原理 了解我们对一问题的追溯。

后续的版本计划#

我们会持续扩展 icestark 的能力,提升微前端体验。在接下来的版本中,我们会:

  • 提供更优的方式接入 es module 微应用 (rfc)
  • 优化 icestark 的错误提示信息 (issue)
  • 给新手用户提供更简单的接入指导
  • 提供多页签的微应用模板
+ + \ No newline at end of file diff --git a/blog/02-icestark-2-6-0/index.html b/blog/02-icestark-2-6-0/index.html index 9281d076..b00050e2 100644 --- a/blog/02-icestark-2-6-0/index.html +++ b/blog/02-icestark-2-6-0/index.html @@ -11,17 +11,17 @@ icestark 2.6.0 | icestark - - + +
-

icestark 2.6.0

Announcing icestark 2.6.0#

非常高兴地告诉大家,在 2.6.0 这个版本中,我们支持了 ES modules 模块类型的微应用。也就是说,如果您使用 Vite 或者 icejs Vite 模式 开发的应用,可以使用 icestark 来构建您的微前端架构。

本次更新主要包括:

支持 ES modules 类型微应用#

ES modules 模块规范是面向未来的模块规范,诸如 Vitesnowpack 等原生支持 ES modules 的构建工具的产生,以及 现代浏览器 对 ES modules 语法的支持,相信未来会有越来越多的微应用构建在 native ES modules 之上。相比传统的构建工具(比如 webpack),这些新型的构建工具或多或少地带来了以下优势:

  • 由于无需打包的特性,服务器启动时间超快
  • 项目大小不再成为服务器启动速度的限制因素
  • HMR 始终保持快速更新

随着 icejs 支持 Vite 模式,icestark 也为大家带来了 ES modules 类型微应用的支持。

框架应用接入 ES modules 微应用的方式非常简单,配置 loadScriptMode: import 来支持 ES modules 类型微应用的加载。

<AppRouter>
<AppRoute
title="商家平台"
+ loadScriptMode="import"
url={[
'//unpkg.com/icestark-child-seller/build/js/index.js', // 资源 ES modules 入口
'//unpkg.com/icestark-child-seller/build/css/index.css',
]}
/>
<AppRoute
path="/user"
//...
/>
</AppRouter>

对于 Vite 应用的 icestark 适配,可参考 其他框架接入微应用 - Vite 应用 进行配置。后期,我们将提供 Vite plugin 能力,使 Vite 应用的接入非常简单。

对于 ice.js 子应用,只需将 build-plugin-icestark 升级到最新版本,并开启 icejs Vite 模式,即可完成改造工作。

ES modules 微应用的常见问题解答#

框架应用需要是 Vite 应用吗?#

不需要。框架应用可以使用 webpack 等非 ES modules 构建工具,无需对框架应用进行任何构建上的改造。对于框架应用,唯一需要做的是:升级最新的 icestark 版本,并设置 ES modules 微应用的加载方式(loadScriptMode 字段) 设置为 import 即可。

支持沙箱模式吗?#

暂不支持沙箱模式。

为什么要使用 ES modules 微应用#

除了本地开发的优秀体验,由于 ES modules 脚本只执行一次的策略,微应用二次加载的速度基本没有延迟,极高地提升二次加载体验。

浏览器的版本支持#

由于 icestark 内部使用 dynamic import 来支持 ES modules 应用,因此支持 chrome 63 版本以上的浏览器。

tip

有关使用 ES modules 的其他疑问,欢迎通过 issuediscussion 告知我们。

完善 Angular 微应用#

Angular 1.x 应用我们建议通过 entry 方式接入,因为我们没有比较好的方式来定义 生命周期函数。在 Angular 5.x 及以上的版本,我们可以通过 bootstrapModule 获取到 NgModule 实例。因此可以通过下面的方式定义生命周期函数:

import { NgModuleRef } from '@angular/core';
+

icestark 2.6.0

Announcing icestark 2.6.0#

非常高兴地告诉大家,在 2.6.0 这个版本中,我们支持了 ES modules 模块类型的微应用。也就是说,如果您使用 Vite 或者 icejs Vite 模式 开发的应用,可以使用 icestark 来构建您的微前端架构。

本次更新主要包括:

支持 ES modules 类型微应用#

ES modules 模块规范是面向未来的模块规范,诸如 Vitesnowpack 等原生支持 ES modules 的构建工具的产生,以及 现代浏览器 对 ES modules 语法的支持,相信未来会有越来越多的微应用构建在 native ES modules 之上。相比传统的构建工具(比如 webpack),这些新型的构建工具或多或少地带来了以下优势:

  • 由于无需打包的特性,服务器启动时间超快
  • 项目大小不再成为服务器启动速度的限制因素
  • HMR 始终保持快速更新

随着 icejs 支持 Vite 模式,icestark 也为大家带来了 ES modules 类型微应用的支持。

框架应用接入 ES modules 微应用的方式非常简单,配置 loadScriptMode: import 来支持 ES modules 类型微应用的加载。

<AppRouter>
<AppRoute
title="商家平台"
+ loadScriptMode="import"
url={[
'//unpkg.com/icestark-child-seller/build/js/index.js', // 资源 ES modules 入口
'//unpkg.com/icestark-child-seller/build/css/index.css',
]}
/>
<AppRoute
path="/user"
//...
/>
</AppRouter>

对于 Vite 应用的 icestark 适配,可参考 其他框架接入微应用 - Vite 应用 进行配置。后期,我们将提供 Vite plugin 能力,使 Vite 应用的接入非常简单。

对于 ice.js 子应用,只需将 build-plugin-icestark 升级到最新版本,并开启 icejs Vite 模式,即可完成改造工作。

ES modules 微应用的常见问题解答#

框架应用需要是 Vite 应用吗?#

不需要。框架应用可以使用 webpack 等非 ES modules 构建工具,无需对框架应用进行任何构建上的改造。对于框架应用,唯一需要做的是:升级最新的 icestark 版本,并设置 ES modules 微应用的加载方式(loadScriptMode 字段) 设置为 import 即可。

支持沙箱模式吗?#

暂不支持沙箱模式。

为什么要使用 ES modules 微应用#

除了本地开发的优秀体验,由于 ES modules 脚本只执行一次的策略,微应用二次加载的速度基本没有延迟,极高地提升二次加载体验。

浏览器的版本支持#

由于 icestark 内部使用 dynamic import 来支持 ES modules 应用,因此支持 chrome 63 版本以上的浏览器。

tip

有关使用 ES modules 的其他疑问,欢迎通过 issuediscussion 告知我们。

完善 Angular 微应用#

Angular 1.x 应用我们建议通过 entry 方式接入,因为我们没有比较好的方式来定义 生命周期函数。在 Angular 5.x 及以上的版本,我们可以通过 bootstrapModule 获取到 NgModule 实例。因此可以通过下面的方式定义生命周期函数:

import { NgModuleRef } from '@angular/core';
let app: void | NgModuleRef<AppModule>;
if (!isInIcestark()) {
platformBrowserDynamic().bootstrapModule(AppModule)
.catch(err => console.error(err));
}
export async function mount () {
app = await platformBrowserDynamic().bootstrapModule(AppModule)
.catch(err => console.error(err));
}
-
export function unmount () {
// @ts-ignore
app.destroy();
}

同时,为了适配 Angular 应用,icestark 做了如下改动:

  • 兼容 Angular 的 <base /> 元素 - icestark 现在会默认解析 entry 或 entryContent 中的 <base> 元素的 href,并将其作为该微应用最终的 publicPath 的一个基准因子。解析完成后,该 <base > 元素不会出现在 DOM 结构中(否则可能修改其他应用的资源地址)。

修复对 location.hash 赋值 onRouteChange 触发两次的错误#

由于对 window.location.hash 进行赋值,会触发 popstate 和 hashchange 两个事件,导致钩子函数 onRouteChange 触发两次。版本 2.6.0 修复了这个问题。

对项目配置的改造#

由于 icestark 内部使用 dynamic import 来支持 ES modules 应用,因此需要对 icestark 工程进行一些适配,保证 dynamic import 不会被转译。项目 tsconfig.json 的改动如下:

{
"compilerOptions": {
"target": "es5",
- "module": "commonjs",
+ "module": "esnext",
+ "moduleResolution": "Node",
+ "allowSyntheticDefaultImports": true,
+ "esModuleInterop": true
...
}
},
}

也就是说,之前版本的 icestark 的默认产物是 commonjs 规范;2.6.0 的版本的默认产物会是 ES modules 规范。

ice.js 插件支持 ES modules 资源#

ice.js 可通过定制插件 build-plugin-icestark 快速接入 icestark。在该插件的最新版本中,我们支持开启 Vite 模式 的 ice.js 应用快速接入。

首先,需要将 build-plugin-icestark 升级至最新版本:

npm i build-plugin-icestark@latest -D

并将 ice.js 升级至 2.x 版本。详见升级指南

然后,在微应用的构建配置(build.json)中,修改成 Vite 模式:

+ "vite": true,
"plugins": [
["build-plugin-icestark", {
+ "type": "child"
- "umd": true
}],
]

后续的版本计划#

我们会持续扩展 icestark 的能力,提升微前端体验。在接下来的版本中,我们会:

  • 为 Vite 微应用提供对应的改造插件
  • 优化开发者开发体验,dev 下提供关键路径的 log 信息
- - +
export function unmount () {
// @ts-ignore
app.destroy();
}

同时,为了适配 Angular 应用,icestark 做了如下改动:

  • 兼容 Angular 的 <base /> 元素 - icestark 现在会默认解析 entry 或 entryContent 中的 <base> 元素的 href,并将其作为该微应用最终的 publicPath 的一个基准因子。解析完成后,该 <base > 元素不会出现在 DOM 结构中(否则可能修改其他应用的资源地址)。

修复对 location.hash 赋值 onRouteChange 触发两次的错误#

由于对 window.location.hash 进行赋值,会触发 popstate 和 hashchange 两个事件,导致钩子函数 onRouteChange 触发两次。版本 2.6.0 修复了这个问题。

对项目配置的改造#

由于 icestark 内部使用 dynamic import 来支持 ES modules 应用,因此需要对 icestark 工程进行一些适配,保证 dynamic import 不会被转译。项目 tsconfig.json 的改动如下:

{
"compilerOptions": {
"target": "es5",
- "module": "commonjs",
+ "module": "esnext",
+ "moduleResolution": "Node",
+ "allowSyntheticDefaultImports": true,
+ "esModuleInterop": true
...
}
},
}

也就是说,之前版本的 icestark 的默认产物是 commonjs 规范;2.6.0 的版本的默认产物会是 ES modules 规范。

ice.js 插件支持 ES modules 资源#

ice.js 可通过定制插件 build-plugin-icestark 快速接入 icestark。在该插件的最新版本中,我们支持开启 Vite 模式 的 ice.js 应用快速接入。

首先,需要将 build-plugin-icestark 升级至最新版本:

npm i build-plugin-icestark@latest -D

并将 ice.js 升级至 2.x 版本。详见升级指南

然后,在微应用的构建配置(build.json)中,修改成 Vite 模式:

+ "vite": true,
"plugins": [
["build-plugin-icestark", {
+ "type": "child"
- "umd": true
}],
]

后续的版本计划#

我们会持续扩展 icestark 的能力,提升微前端体验。在接下来的版本中,我们会:

  • 为 Vite 微应用提供对应的改造插件
  • 优化开发者开发体验,dev 下提供关键路径的 log 信息
+ + \ No newline at end of file diff --git a/blog/03-icestark-2-7-0/index.html b/blog/03-icestark-2-7-0/index.html index a33d6b7d..ddaef773 100644 --- a/blog/03-icestark-2-7-0/index.html +++ b/blog/03-icestark-2-7-0/index.html @@ -11,14 +11,14 @@ icestark 2.7.0 | icestark - - + +
-

icestark 2.7.0

Announcing icestark 2.7.0#

本次更新为大家带来了应用的细节优化,更新主要包括:

缓存 css 资源#

持续优化微前端应用的加载体验是我们一直追求的事情。在这边版本中,在微应用的以下配置:

我们会默认缓存样式资源,以提升微应用二次加载的体验。对比如下(前图未缓存样式,二次加载有明显加载 Loading;后图为缓存样式):

beforeafter

为了保证用户尽可能地不产生 Break Change,以下场景不会默认缓存 css 资源:

为 Vite 应用的开发者提供便捷的接入插件#

满足使用 Vite 官方应用的用户便捷地接入 icestark,我们提供了 vite-plugin-index-html Vite 插件。该插件提供了类似 webpack-html-plugin 的能力,会将 Vite 生成的虚拟入口,替换成用户指定的入口。

用户可按照我们的 教程 接入。

该插件的简单用法如下:

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
+ import htmlPlugin from 'vite-plugin-index-html';
export default defineConfig({
plugins: [
vue(),
+ htmlPlugin({
+ input: './src/main.ts', // 指定确定的入口文件
+ preserveEntrySignatures: "exports-only", // 确保入口文件导出生命周期函数
+ })
],
})

appHistory 支持传递 state#

为满足用户通过 history state 传参的诉求,appHistory<AppLink /> 可通过第二个参数传递 state。用法如下:

appHistory.push('/home?name=ice', { framework: 'icestark' });
-
<AppLink
to={{
pathname: '/waiter/list',
search: '?name=ice',
state: {
framework: 'icestark'
}
}}
>
使用 AppLink 进行页面跳转
</AppLink>

常规的错误修复#

后续的版本计划#

我们会持续扩展 icestark 的能力,提升微前端体验。在接下来的版本中,我们会:

  • 我们会结合官网,提供详尽的报错指引
  • 提供官方的权限控制实践 (rfcs)
  • 以及样式隔离方案 (rfcs)
- - +
Skip to main content

icestark 2.7.0

Announcing icestark 2.7.0#

本次更新为大家带来了应用的细节优化,更新主要包括:

缓存 css 资源#

持续优化微前端应用的加载体验是我们一直追求的事情。在这边版本中,在微应用的以下配置:

我们会默认缓存样式资源,以提升微应用二次加载的体验。对比如下(前图未缓存样式,二次加载有明显加载 Loading;后图为缓存样式):

beforeafter

为了保证用户尽可能地不产生 Break Change,以下场景不会默认缓存 css 资源:

为 Vite 应用的开发者提供便捷的接入插件#

满足使用 Vite 官方应用的用户便捷地接入 icestark,我们提供了 vite-plugin-index-html Vite 插件。该插件提供了类似 webpack-html-plugin 的能力,会将 Vite 生成的虚拟入口,替换成用户指定的入口。

用户可按照我们的 教程 接入。

该插件的简单用法如下:

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
+ import htmlPlugin from 'vite-plugin-index-html';
export default defineConfig({
plugins: [
vue(),
+ htmlPlugin({
+ input: './src/main.ts', // 指定确定的入口文件
+ preserveEntrySignatures: "exports-only", // 确保入口文件导出生命周期函数
+ })
],
})

appHistory 支持传递 state#

为满足用户通过 history state 传参的诉求,appHistory<AppLink /> 可通过第二个参数传递 state。用法如下:

appHistory.push('/home?name=ice', { framework: 'icestark' });
+
<AppLink
to={{
pathname: '/waiter/list',
search: '?name=ice',
state: {
framework: 'icestark'
}
}}
>
使用 AppLink 进行页面跳转
</AppLink>

常规的错误修复#

后续的版本计划#

我们会持续扩展 icestark 的能力,提升微前端体验。在接下来的版本中,我们会:

  • 我们会结合官网,提供详尽的报错指引
  • 提供官方的权限控制实践 (rfcs)
  • 以及样式隔离方案 (rfcs)
+ + \ No newline at end of file diff --git a/blog/atom.xml b/blog/atom.xml index 928f63ec..1c35a9b7 100644 --- a/blog/atom.xml +++ b/blog/atom.xml @@ -2,7 +2,7 @@ https://micro-frontends.ice.work/blog icestark Blog - 2024-01-18T05:51:10.155Z + 2024-01-18T05:51:25.651Z https://github.com/jpmonette/feed icestark Blog @@ -11,28 +11,28 @@ <![CDATA[icestark 2.4.0]]> icestark 2.4.0 - 2024-01-18T05:51:10.155Z + 2024-01-18T05:51:25.651Z <![CDATA[icestark 2.7.0]]> icestark 2.7.0 - 2024-01-18T05:51:10.155Z + 2024-01-18T05:51:25.651Z <![CDATA[icestark 2.6.0]]> icestark 2.6.0 - 2024-01-18T05:51:10.155Z + 2024-01-18T05:51:25.651Z <![CDATA[icestark 2.5.0]]> icestark 2.5.0 - 2024-01-18T05:51:10.155Z + 2024-01-18T05:51:25.651Z \ No newline at end of file diff --git a/blog/index.html b/blog/index.html index a18a0dc5..90366f03 100644 --- a/blog/index.html +++ b/blog/index.html @@ -11,13 +11,13 @@ Blog | icestark - - + +
-

icestark 2.6.0

Announcing icestark 2.6.0#

非常高兴地告诉大家,在 2.6.0 这个版本中,我们支持了 ES modules 模块类型的微应用。也就是说,如果您使用 Vite 或者 icejs Vite 模式 开发的应用,可以使用 icestark 来构建您的微前端架构。

本次更新主要包括:

- - +
Skip to main content

icestark 2.6.0

Announcing icestark 2.6.0#

非常高兴地告诉大家,在 2.6.0 这个版本中,我们支持了 ES modules 模块类型的微应用。也就是说,如果您使用 Vite 或者 icejs Vite 模式 开发的应用,可以使用 icestark 来构建您的微前端架构。

本次更新主要包括:

+ + \ No newline at end of file diff --git a/blog/rss.xml b/blog/rss.xml index 507b0f03..feb0abbf 100644 --- a/blog/rss.xml +++ b/blog/rss.xml @@ -4,35 +4,35 @@ icestark Blog https://micro-frontends.ice.work/blog icestark Blog - Thu, 18 Jan 2024 05:51:10 GMT + Thu, 18 Jan 2024 05:51:25 GMT https://validator.w3.org/feed/docs/rss2.html https://github.com/jpmonette/feed <![CDATA[icestark 2.4.0]]> https://micro-frontends.ice.work/blog/00-icestark-2-4-0 icestark 2.4.0 - Thu, 18 Jan 2024 05:51:10 GMT + Thu, 18 Jan 2024 05:51:25 GMT <![CDATA[icestark 2.7.0]]> https://micro-frontends.ice.work/blog/03-icestark-2-7-0 icestark 2.7.0 - Thu, 18 Jan 2024 05:51:10 GMT + Thu, 18 Jan 2024 05:51:25 GMT <![CDATA[icestark 2.6.0]]> https://micro-frontends.ice.work/blog/02-icestark-2-6-0 icestark 2.6.0 - Thu, 18 Jan 2024 05:51:10 GMT + Thu, 18 Jan 2024 05:51:25 GMT <![CDATA[icestark 2.5.0]]> https://micro-frontends.ice.work/blog/01-icestark-2-5-0 icestark 2.5.0 - Thu, 18 Jan 2024 05:51:10 GMT + Thu, 18 Jan 2024 05:51:25 GMT diff --git a/blog/tags/announcement/index.html b/blog/tags/announcement/index.html index 5b64596f..dac20859 100644 --- a/blog/tags/announcement/index.html +++ b/blog/tags/announcement/index.html @@ -11,13 +11,13 @@ Posts tagged "announcement" | icestark - - + +
-

4 posts tagged with "announcement"

View All Tags

icestark 2.6.0

Announcing icestark 2.6.0#

非常高兴地告诉大家,在 2.6.0 这个版本中,我们支持了 ES modules 模块类型的微应用。也就是说,如果您使用 Vite 或者 icejs Vite 模式 开发的应用,可以使用 icestark 来构建您的微前端架构。

本次更新主要包括:

- - +
Skip to main content

4 posts tagged with "announcement"

View All Tags

icestark 2.6.0

Announcing icestark 2.6.0#

非常高兴地告诉大家,在 2.6.0 这个版本中,我们支持了 ES modules 模块类型的微应用。也就是说,如果您使用 Vite 或者 icejs Vite 模式 开发的应用,可以使用 icestark 来构建您的微前端架构。

本次更新主要包括:

+ + \ No newline at end of file diff --git a/blog/tags/index.html b/blog/tags/index.html index ae00b5c2..1233330a 100644 --- a/blog/tags/index.html +++ b/blog/tags/index.html @@ -11,13 +11,13 @@ Tags | icestark - - + + - - +
Skip to main content
+ + \ No newline at end of file diff --git a/blog/tags/v-2/index.html b/blog/tags/v-2/index.html index 85da4515..c2f0de7f 100644 --- a/blog/tags/v-2/index.html +++ b/blog/tags/v-2/index.html @@ -11,13 +11,13 @@ Posts tagged "v2" | icestark - - + +
-

4 posts tagged with "v2"

View All Tags

icestark 2.6.0

Announcing icestark 2.6.0#

非常高兴地告诉大家,在 2.6.0 这个版本中,我们支持了 ES modules 模块类型的微应用。也就是说,如果您使用 Vite 或者 icejs Vite 模式 开发的应用,可以使用 icestark 来构建您的微前端架构。

本次更新主要包括:

- - +
Skip to main content

4 posts tagged with "v2"

View All Tags

icestark 2.6.0

Announcing icestark 2.6.0#

非常高兴地告诉大家,在 2.6.0 这个版本中,我们支持了 ES modules 模块类型的微应用。也就是说,如果您使用 Vite 或者 icejs Vite 模式 开发的应用,可以使用 icestark 来构建您的微前端架构。

本次更新主要包括:

+ + \ No newline at end of file diff --git a/docs/api/ice-stark-app/index.html b/docs/api/ice-stark-app/index.html index bd76598b..4b658e82 100644 --- a/docs/api/ice-stark-app/index.html +++ b/docs/api/ice-stark-app/index.html @@ -11,8 +11,8 @@ @ice/stark-app | icestark - - + +
@@ -24,7 +24,7 @@
setLibraryName('microApp');
export function mount(props) {
const { container, customProps } = props;
ReactDOM.render(<App {...customProps} />, container);
}
export function unmount(props) {
const { container } = props;
ReactDOM.unmountComponentAtNode(container);
}
- - + + \ No newline at end of file diff --git a/docs/api/ice-stark/index.html b/docs/api/ice-stark/index.html index cc427ac5..9b2a56e4 100644 --- a/docs/api/ice-stark/index.html +++ b/docs/api/ice-stark/index.html @@ -11,8 +11,8 @@ @ice/stark | icestark - - + +
@@ -28,7 +28,7 @@
start({
onAppEnter: (appConfig) => {
console.log(`${appConfig.name} entered.`)
}
})

createMicroApp
2.0.0+
#

手动加载微应用,类型定义如下:

function createMicroApp(app: string | AppConfig, appLifecyle?: AppLifecylceOptions, configuration?: StartConfiguration): Promise<MicroApp>

使用该 api 的通用场景是无需 icestark 提供的运行时能力,手动指定在某行为下触发渲染微应用。示例如下:

const App = () => {
const container = useRef(null);
useEffect(() => {
createMicroApp({
name: 'microApp',
url: ['//unpkg.com/icestark-child-common/build/js/index.js'],
container: container.current,
})
return () => {
unmountMicroApp('microApp')
}
}, [])
return (
<div ref>
)
}

unmountMicroApp
2.0.0+
#

手动卸载微应用,unmountMicroApp(appName)

unloadMicroApp
2.0.0+
#

手动移除微应用,unloadMicroApp(appName)

同 unmountMicroApp 区别:unmountMicroApp 仅仅执行了微应用的 unmount 方法,从节点上移除微应用,下一次挂载时可以直接执行 mount 重新挂载;而 unloadMicroApp 除了执行 unmount 方法之外,还会将微应用执行结果(mount/unmount)移除,下一次挂载该微应用时,需要重新加载资源执行来获取其生命周期。

- - + + \ No newline at end of file diff --git a/docs/api/stark-module/index.html b/docs/api/stark-module/index.html index 92c5b145..b86bd852 100644 --- a/docs/api/stark-module/index.html +++ b/docs/api/stark-module/index.html @@ -11,8 +11,8 @@ @ice/stark-module | icestark - - + +
@@ -22,7 +22,7 @@
clearModules();

mountModule#

挂载模块,提供自定义组件能力。

import { mountModule, unmoutModule } from '@ice/stark-module';
const moduleInfo = {
name: 'moduleName',
url: 'https://localhost/module.js',
};
const ModuleComponent = () => {
const renderNode = useRef(null);
useEffect(() => {
mountModule(moduleInfo, renderNode, {});
return () => {
unmoutModule(moduleInfo, renderNode);
}
}, []);
return (<div ref={renderNode}></div>);
};

unmoutModule#

卸载模块,提供自定义组件能力,见 mountModule

- - + + \ No newline at end of file diff --git a/docs/faq/index.html b/docs/faq/index.html index a18d8d23..95ef436f 100644 --- a/docs/faq/index.html +++ b/docs/faq/index.html @@ -11,8 +11,8 @@ 常见问题 | icestark - - + +
@@ -39,7 +39,7 @@
console.log(window.fetch);

IE 浏览器环境下支持沙箱吗#

不支持。在不支持 Proxy 语法的浏览器环境(比如 IE 浏览器),icestark 会有如下提示:

proxy sandbox is not support by current browser

并回退到无沙箱模式执行。

tip

此外,我们并不推荐添加诸如 proxy-polyfill 等 polyfill 方法来支持 icestark 沙箱。因为目前实现 Proxy 的 polyfill 都不是完备的(有缺陷的),icestark 沙箱在实现上使用了 has trap,而这个 trap 目前无法在 polyfill 中实现。更多有关 Proxy 的内容,可参考 Proxy

如何解决 Script Error#

“Script error.” 是一个常见错误,但由于该错误不提供完整的报错信息(错误堆栈),问题排查往往无从下手。icestark 的 scriptAttributes 参数支持为加载的 <script /> 资源添加 crossorigin="anonymous" 来解决这个问题。具体可参考 scriptAttributes

tip

想了解更多有关 Script Error 的问题,可以参考 “Script error.”的产生原因和解决办法

页面空白,控制台没有错误#

遇到页面空白,而控制台又没有错误的情况,有可能是因为 微应用已经正常加载,但微应用路由没有匹配成功。因此我们建议微应用增加一个默认路由:

import { renderNotFound, isInIcestark, getBasename } from '@ice/stark-app';
const Routes = () => {
return (
<Router basename={isInIcestark() ? getBasename(): '/'}>
<Route component={Detail} activePath="/detail" exact>
<Route component={isInIcestark() ? () => renderNotFound() : NotFound}>
</Route>
)
}

这样,不会导致微应用正常加载,但微应用路由没有匹配成功时导致的页面空白,而会显示 404 页面。这样,我们能清晰地知道,在 icestark 执行环境下,需要修改微应用的 basename,使得微应用可以与当前路由匹配上。

Vite 微应用支持沙箱吗#

暂不支持沙箱。

接入 Vite 微应用,主应用需要升级为 Vite 应用吗#

不需要。主应用可以使用 webpack 等非 ES modules 构建工具,无需对主应用进行任何构建上的改造。对于主应用,唯一需要做的是:升级最新的 icestark 版本,并设置 ES modules 微应用的加载方式(loadScriptMode 字段) 设置为 import 即可。

切换微应用,主应用样式丢失#

通常情况是主应用开启了 webpack Dynamic Imports 能力,可以通过 shouldAssetsRemove 防止错误地移除主应用的样式资源。

// src/App.jsx
import { AppRouter, AppRoute } from '@ice/stark';
const App = () => {
render() {
return (
<AppRouter
shouldAssetsRemove={(url, element) => {
// 如果请求主应用静态资源,返回 false
if (url.includes('www.framework.com')) {
return false;
}
return true;
}}
>
...
</AppRouter>
);
}
}

主应用路由之间跳转导致重复渲染#

如果主应用需要包含路由页面,在 React 主应用接入 我们推荐将主应用路由作为一个 fallback 微应用来使用。但由于在主应用路由切换时,上层组件状态改变会导致 fallback 应用重复渲染,因此推荐使用 React.memo 防止 React 组件重复渲染。具体示例可参考 主应用中如何包含路由页面

- - + + \ No newline at end of file diff --git a/docs/guide/advanced/communication/index.html b/docs/guide/advanced/communication/index.html index af4bba16..13a2b94a 100644 --- a/docs/guide/advanced/communication/index.html +++ b/docs/guide/advanced/communication/index.html @@ -11,8 +11,8 @@ 应用间通信 | icestark - - + +
@@ -24,7 +24,7 @@
event.on('freshMessage', () => {
// 重新获取消息数
});

在微应用中触发事件:

// 微应用
import { event } from '@ice/stark-data';
event.emit('freshMessage');

props
2.0.0+
#

icestark 还支持通过 props 将主应用数据传递给微应用。

示例#

主应用向微应用统一注入用户信息。

在主应用中通过 props 配置用户信息。

// src/App.jsx
import { AppRouter, AppRoute } from '@ice/stark';
const App = () => {
return (
<AppRouter>
<AppRoute
name="waiter"
activePath="/waiter"
title="商家平台",
props={{
userId: 'xxxxx'
}}
url={[
'https://iceworks.oss-cn-hangzhou.aliyuncs.com/icestark/child-waiter-vue/dist/js/app.js',
'https://iceworks.oss-cn-hangzhou.aliyuncs.com/icestark/child-waiter-vue/dist/css/app.css',
]}
/>
...
</AppRouter>
);
}

微应用可以通过生命周期函数获取到该数据:

export function mount({ container, customProps }) {
ReactDOM.render(<App { ...customProps } />, props.container);
}

其他#

对于主应用和微应用,运行时都共享了当前页面的 location、Cookie、LocalStorage、window 等全局信息,因此应用间的通信,也可以通过这些方案很简单的实现。

- - + + \ No newline at end of file diff --git a/docs/guide/advanced/performance/index.html b/docs/guide/advanced/performance/index.html index 144b95ad..f72b7689 100644 --- a/docs/guide/advanced/performance/index.html +++ b/docs/guide/advanced/performance/index.html @@ -11,8 +11,8 @@ 性能优化 | icestark - - + +
@@ -26,7 +26,7 @@
<body>
<div id="root"></div>
<!-- 在主应用中加载基础依赖 -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.0/cjs/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.0/cjs/react-dom.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/antd/4.17.0-alpha.8/antd.min.js"></script>
<!-- 加载主应用的脚本资源 -->
<script src="//ice.alicdn.com/icestark/layout-app/build/js/index.js"></script>
</body>
</html>

LoadingComponent#

微应用之间切换造成的空白画面让人难以接受,为了降低页面空白造成的视觉冲击,可以在微应用的切换时添加一个 “过渡” 动画:

import Loading from './Loading';
const App = () => {
return (
<AppRouter
LoadingComponent={Loading}
>
...
</AppRouter>
);
}
- - + + \ No newline at end of file diff --git a/docs/guide/advanced/sandbox/index.html b/docs/guide/advanced/sandbox/index.html index 4ef25367..a1de0d11 100644 --- a/docs/guide/advanced/sandbox/index.html +++ b/docs/guide/advanced/sandbox/index.html @@ -11,13 +11,13 @@ 样式和脚本隔离 | icestark - - + +

样式和脚本隔离

icestark 当下的方案里,无论是主应用还是微应用都是直接在页面里执行,本质上不存在隔离机制,针对这个问题我们一方面通过一些规范来保证污染问题,一方面也在尝试更加彻底的沙箱机制,如果你的微应用都是二方接入,那我们推荐直接通过规范约束即可,如果存在三方接入这种不可控的场景,建议还是通过 iframe 的方式嵌入。

样式污染#

页面运行时同时只会存在一个微应用,因此多个微应用不存在样式相互污染的问题,但是主应用和微应用是同时运行的,因此这两者中间可能会存在一些样式相互污染,针对这个问题,我们目前推荐「通过约定避免微应用与主应用的样式相互污染」的方案,同时也在尝试 Shadow DOM 的方案。

规范#

使用 CSS Modules 方案管理样式#

无论是主应用还是微应用,直接通过 CSS Modules 的方案管理自身可控的样式,这样基本杜绝了两者样式冲突的问题。

主应用自定义基础组件 prefix#

除了自身可控的样式,应用中还会有一些全局样式,比较典型的就是类似 next 这种基础组件的样式,如果主应用和微应用使用了不同版本的 next,则很容易造成样式相互污染,这种场景推荐在主应用中将基础组件的前缀统一改掉,比如将 next- 改为 next-icestark-,这个能力已在主应用模板中内置,具体可参考相关代码。

微应用避免产生全局样式#

对于类似 normalize.css 这种全局重置样式,推荐统一通过主应用引入,微应用尽量避免产生全局性质的样式,因为这样在切换微应用时可能会因为全局样式差异产生一些抖动。

Shadow DOM(方案试验中)#

如果将微应用渲染到 Shadow DOM 中,那么微应用产生的所有样式都不会污染到全局,事实上在我们试验的过程中的确是这样的。但是我们遇到一个当下无法解决的问题,大部分类似 Dialog 组件的实现都是在 body 下创建一个容器节点,但是 Shadow DOM 里 Dialog 的样式无法作用到全局,因此展示出来 Dialog 就是无样式的,在这个问题上我们还在尝试,比如类似 Dialog 组件的实现能够进行优化:判断自身是否在 Shadow DOM 里,如果是的话则将容器节点创建到 Shadow DOM 里,否则创建到 body 节点下。

JS 污染#

相对于样式污染,JS 污染的危害性更高,在目前的方案下,如果微应用想要恶意污染的话基本是无法杜绝的,因此针对这种不可控的微应用建议还是通过 iframe 的方式接入。针对可控的二方应用,正常书写代码是不会有问题的,针对一些特殊情况我们也总结了一些规范。

规范#

微应用避免改变全局状态#

比如改变全局变量 window/location 的默认行为,通过 document 操作 Layout 的 DOM,这些本身都是一些不推荐的做法。

主应用通过钩子记录并恢复全局状态#

<AppRouter
onAppEnter={(appConfig) => {
// 按需记录全局状态
}}
onAppLeave={(appConfig) => {
// 按需恢复全局状态
}}
>
// {...}
</AppRouter>

基于 Proxy 的运行沙箱#

通过 with + new Function 的形式,为微应用脚本创建沙箱运行环境,并通过 Proxy 代理阻断沙箱内对 window 全局变量的访问和修改。

icestark 内置了基于 @ice/sandbox 的沙箱隔离,通过 sandbox 属性开启:

<AppRoute
sandbox
activePath="/seller"
title="商家平台"
url={[
'//unpkg.com/icestark-child-seller/build/js/index.js',
'//unpkg.com/icestark-child-seller/build/css/index.css',
]}
/>
- - + + \ No newline at end of file diff --git a/docs/guide/concept/child/index.html b/docs/guide/concept/child/index.html index 4a812fcd..a7b612f4 100644 --- a/docs/guide/concept/child/index.html +++ b/docs/guide/concept/child/index.html @@ -11,8 +11,8 @@ 微应用 | icestark - - + +
@@ -21,7 +21,7 @@
registerAppLeave((props) => {
ReactDOM.unmountComponentAtNode(props.container);
});

2. mount/unmount
1.6.0+
#

import ReactDOM from 'react-dom';
import App from './App';
export function mount(props) {
ReactDOM.render(<App />, props.container);
}
export function unmount(props) {
ReactDOM.unmountComponentAtNode(props.container);
}

入口规范#

icestark 通过微应用入口字段的配置进行应用的渲染,因此这个字段 非常重要。针对不同的场景,icestark 也支持了多种入口配置形式。

1. url#

适用于微应用入口资源比较确定,此时将这些资源地址按顺序拼成数组传给 icestark 即可。

const apps = [{
url: ['https://example.com/a.js', 'https://example.com/a.css'],
activePath: '/foo'
// ...
}]

2. entry
推荐
#

使用场景:

  • 应用依赖的入口资源不确定:比如需要引入 vendor、或者不确定的 externals 资源、资源地址带 hash 等场景
  • 应用默认需要依赖很多 DOM 节点:比如 jQuery/Kissy/Angular 等框架
const apps = [{
entry: 'https://example.com/a.html',
activePath: '/foo'
// ...
}]

entry 对应 html url, icestark 对 entry 的处理包含以下步骤:

  • 通过 window.fetch 获取 entry 属性对应的 html 内容
  • 解析 html 内容,框架将会进行解析处理:提取 js 信息,如果资源路径为相对地址,将根据 entry 地址进行补齐
  • 将处理后的 html 内容插入 icestark 动态创建的节点
  • 依次通过创建 script 标签按顺序引入 js 资源

3. entryContent#

当需要使用 entry 能力但是 html url 不支持前端跨域访问的情况,可以自行将 html 内容拿到,然后通过 entryContent 属性传递给 icestark。

const apps = [{
entryContent: '<!DOCTYPE html><html><body><script src=""></body></html>',
activePath: '/foo'
// ...
}]

4. render/component#

仅使用 React 的主应用支持,具体请参考 主应用接入 - React

- - + + \ No newline at end of file diff --git a/docs/guide/concept/layout/index.html b/docs/guide/concept/layout/index.html index 7a710752..c2c6879e 100644 --- a/docs/guide/concept/layout/index.html +++ b/docs/guide/concept/layout/index.html @@ -11,13 +11,13 @@ 主应用 | icestark - - + +

主应用

又称框架应用或基座应用,一个系统只有一个主应用。主应用应该保持职责明确,一个设计良好的主应用只做两件事情:

  1. 系统整体 Layout 的设计

  2. 所有微应用的配置与注册

主应用尽量避免包含具体页面的 UI 代码,如果主应用做了过多的事情会带来以下问题:

  • 主应用样式代码太多,会增加微应用和主应用样式冲突概率

  • 主应用为微应用提供其他能力比如一些全局 API,会破坏微应用的独立性,增加相互的耦合

  • 主应用本质是一个中心化的部分,变更后原则上需要回归所有微应用,因此需要保证职责的简单,越简单的东西越稳定

- - + + \ No newline at end of file diff --git a/docs/guide/concept/workflow/index.html b/docs/guide/concept/workflow/index.html index f0470bd3..9275174e 100644 --- a/docs/guide/concept/workflow/index.html +++ b/docs/guide/concept/workflow/index.html @@ -11,13 +11,13 @@ 工作流程 | icestark - - + +

工作流程

图示#

icestark 加载图示

加载模式#

icestark 目前支持三种加载模式,分别是 scriptfetchimport,由 loadScriptMode 字段指定。

  1. script

默认加载方式。该模式下,icestark 会通过 HTML <script /> 标签加载微应用脚本资源,再次加载时充分利用浏览器缓存进行加载。

  1. fetch

当指定 loadScriptModefetch,或配置微应用沙箱模式时,会通过 window.fetch用户自定义的 fetch 能力 加载并缓存脚本资源。再次加载时,会充分利用本地内部缓存进行加载。

  1. import

加载 ES modules 类型微应用的主要方式,该模式会通过 Dynamic Import 动态加载脚本资源。

钩子函数#

icestark 在加载微应用的各个阶段提供钩子函数,方便用户监听并执行相应逻辑。详细用法可参考 StartConfiguration

钩子函数触发时机用法示例
onActiveApps微应用开始被激活的回调记录当前 url 匹配的微应用
onLoadingApp微应用加载前的回调在微应用加载过程中渲染加载动画
onFinishLoading微应用加载并执行后的回调在微应用加载过程中结束渲染加载动画、结合 onLoadingApp 记录微应用加载执行的时长
onAppEnter微应用开始渲染前的回调结合 onLoadingApp 记录微应用从加载到渲染的时长,结合 onFinishLoading 记录微应用渲染耗时
onAppLeave微应用卸载前的回调记录用户的停留时长
onError微应用在 icestark 在加载或执行错的回调记录微应用运行的错误信息
- - + + \ No newline at end of file diff --git a/docs/guide/ecosystem/index.html b/docs/guide/ecosystem/index.html index eccf316d..0e33d85d 100644 --- a/docs/guide/ecosystem/index.html +++ b/docs/guide/ecosystem/index.html @@ -11,13 +11,13 @@ 社区生态 | icestark - - + +

社区生态

icestark 生态系统帮助快速创建微前端项目。欢迎参与贡献。

Repos#

这里可以找到一些示例仓库,以及官方提供的脚手架。

文章#

- - + + \ No newline at end of file diff --git a/docs/guide/index.html b/docs/guide/index.html index 9af1c71a..c2abfa11 100644 --- a/docs/guide/index.html +++ b/docs/guide/index.html @@ -11,15 +11,15 @@ 快速起步 | icestark - - + +

快速起步

介绍#

icestark 是一个面向大型系统的微前端解决方案,适用于以下业务场景:

  • 后台比较分散,体验差别大,因为要频繁跳转导致操作效率低,希望能统一收口的一个系统内
  • 单页面应用非常庞大,多人协作成本高,开发/构建时间长,依赖升级回归成本高
  • 系统有二方/三方接入的需求

icestark 在保证一个系统的操作体验基础上,实现各个微应用的独立开发和发版,主应用通过 icestark 管理微应用的注册和渲染,将整个系统彻底解耦。

项目初始化#

icestark 可以通过简单的命令行,生成主应用和微应用模板。无论您是使用 React 还是 Vue,都可以便捷的创建符合 icestark 微前端规范的项目。这些项目均由 icestark 团队官方维护。

tip

如果您想将正在开发中或已开发完成的项目接入 icestark,请移步主应用接入微应用接入。如果您使用的是 create-react-app 、umi 等框架开发的应用,亦可参考其它框架的接入指南

初始化主应用#

# 基于 React 的主应用
$ npm init ice icestark-layout @icedesign/stark-layout-scaffold
# 或者基于 Vue 的主应用
$ npm init ice icestark-layout @vue-materials/icestark-layout-app
$ cd icestark-layout
$ npm install
$ npm start

初始化微应用#

# 基于 React 的微应用
$ npm init ice icestark-child @icedesign/stark-child-scaffold
# 基于 Vue 的微应用
$ npm init ice icestark-child @vue-materials/icestark-child-app
$ cd icestark-child
$ npm install
$ npm run start

兼容性#

  • 现代浏览器和 IE11。
tip

对于 IE 系列浏览器,需要提供相应的 polyfill 支持。详细介绍,请参考常见问题 -> 兼容 IE 浏览器

- - + + \ No newline at end of file diff --git a/docs/guide/micro-module/index.html b/docs/guide/micro-module/index.html index 4b4b92c1..ea5ab322 100644 --- a/docs/guide/micro-module/index.html +++ b/docs/guide/micro-module/index.html @@ -11,8 +11,8 @@ 微模块 | icestark - - + +
@@ -43,7 +43,7 @@
const App = () => {
return (
<div>
<MicroModule moduleName="moduleName" />
</div>
);
}

性能优化#

通常情况下,为了减少模块体积,希望抽离一些公共的三方库。

首先,微模块需要在构建时移除依赖的三方库。

// webpack.config.js
export default {
...
externals: {
react: {
root: 'React',
commonjs2: 'react',
commonjs: 'react',
amd: 'react',
},
'react-dom': {
root: 'ReactDOM',
commonjs2: 'react-dom',
commonjs: 'react-dom',
amd: 'react-dom',
}
}
}

然后,在应用项目中,声明微模块的依赖

import { MicroModule } from '@ice/stark-module';
import ReactDOM from 'react-dom';
const App = () => {
const moduleInfo = {
name: 'moduleName',
url: 'https://localhost/module.js',
// 声明模块 moduleName 所需的三方依赖
runtime: [
{
id: "react@16",
url: [
"https://g.alicdn.com/code/lib/react/16.14.0/umd/react.production.min.js"
]
},
{
id: "react-dom@16",
url: [
"https://g.alicdn.com/code/lib/react-dom/16.14.0/umd/react-dom.production.min.js"
]
},
]
}
return <MicroModule moduleInfo={moduleInfo} />;
}

如果使用官方 build-plugin-stark-module 插件来构建的微模块,只需要在 build.json 中配置:

{
"plugins": [
...
["build-plugin-stark-module", {
"moduleExternals": {
"react": {
"root": "React",
"url": "https://g.alicdn.com/code/lib/react/16.14.0/umd/react.production.min.js",
},
"react-dom": {
"root": "ReactDOM",
"url": "https://g.alicdn.com/code/lib/react-dom/16.14.0/umd/react-dom.production.min.js"
}
}
}],
...
]
}

该插件会将三方依赖从模块中移除,并在产物目录生成一份依赖信息 runtime.json,模块发布时,需要将 runtime.json 一起发布。这样,在应用项目中,可以使用 runtime.json 作为依赖信息。

import { MicroModule } from '@ice/stark-module';
import ReactDOM from 'react-dom';
const App = () => {
const moduleInfo = {
name: 'moduleName',
url: 'https://localhost/module.js',
// 声明模块 moduleName 需要的依赖文件地址
runtime: 'https://xxx.com/moduleName.runtime.json'
}
return <MicroModule moduleInfo={moduleInfo} />;
}

API#

请移步 API -> @ice/stark-module

- - + + \ No newline at end of file diff --git a/docs/guide/upgrade/index.html b/docs/guide/upgrade/index.html index c79567d1..d16fd68e 100644 --- a/docs/guide/upgrade/index.html +++ b/docs/guide/upgrade/index.html @@ -11,13 +11,13 @@ 升级指南 | icestark - - + +

升级指南

1.x -> 2.x#

icestark 2.x 于 2020 年 12 月发布,Changelog

  • 支持以 API 的方式初始化主应用,主应用不再限制 React/Vue/Angular 等不同框架
  • 支持加载 UMD 格式的微应用产物

注意:@ice/stark 2.0.0 完全兼容 1.0.0 版本的 API,因此主应用可以非常低成本的升级 2.0.0 版本。

主应用升级#

2.x 版本完全向前兼容,因此直接将 @ice/stark 依赖升级到 2.x 最新版本即可。

微应用升级#

请确保主应用中 @ice/stark 的版本大于 2.0.0 (或者 1.6.0)

2.x 版本之后,微应用支持两种格式的导出方式:

  1. [原有] 通过 registerAppEnter/registerAppLeave 注册生命周期
  2. [新增] UMD 格式,通过 mount/unmount 注册生命周期,并通过 setLibraryName 配置微应用导出的全局名称

通过 build-plugin-icestark 构建的微应用,只需将 build-plugin-icestark 的版本更新至 2.x,并开启插件选项 umd: true

0.x -> 1.x#

icestark 1.x 于 2019 年 10 月发布,Changelog

  • 将微应用相关 API 拆成独立的包 @ice/stark-app,保证兼容不同框架的微应用
  • 支持 onAppEnter/onAppLeave 相关 API

迁移步骤#

主应用#

@ice/stark 从 0.x 升级到 1.x 即可,API 跟 0.x 兼容。

微应用#

  • 移除 @ice/stark 依赖:npm rm @ice/stark --save
  • 安装 @ice/stark-app 依赖:npm i @ice/stark-app --save
  • 批量替换代码中 @ice/stark@ice/stark-app
- - + + \ No newline at end of file diff --git a/docs/guide/use-child/others/index.html b/docs/guide/use-child/others/index.html index 73949061..37d732c6 100644 --- a/docs/guide/use-child/others/index.html +++ b/docs/guide/use-child/others/index.html @@ -11,8 +11,8 @@ 其他框架接入微应用 | icestark - - + +
@@ -39,7 +39,7 @@
// 导出 mount 生命周期函数
+ export function mount({ container }: { container: Element}) {
+ vue = createApp(App);
+ vue.mount(container);
+ }
// 导出 unmout 生命周期函数
+ export function unmount() {
+ if (vue) {
+ vue.unmount();
+ }
}

修改 Vite 配置文件#

Vite 应用默认使用根目录下的 index.html 作为入口文件,并通过解析 index.html<script /> 标签生成一个 “虚拟的” 脚本资源入口文件,会导致 icestark 无法获取导出的生命周期函数。

我们提供两种方式修改 Vite 配置文件,使得 icestark 可以正确获取生命周期函数。用户可以根据自己的实际情况进行选择。

  1. 使用 Vite Lib 模式

Vite Lib 模式可以完全指定入口文件。这种方式的缺点是 Vite 会只输出应用的样式和脚本文件,如果应用需要单独运行,还需要手动提供 index.html 文件。

配置如下:

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
export default defineConfig({
plugins: [vue()],
+ build: {
+ lib: {
+ entry: './src/main.ts',
+ formats: ['es'],
+ fileName: 'index'
+ },
+ rollupOptions: {
+ preserveEntrySignatures: 'exports-only',
+ }
+ },
})
  1. 使用 vite-plugin-index-html 插件

为了解决 Vite Lib 模式产生的问题,我们提供了 vite-plugin-index-html 插件,这个插件提供了类似 html-webpack-plugin 的能力。

在使用这个插件时,需要指定应用的入口文件(Vue 应用通常是 main.t|js,React 应用通常是 app.t|jsx))。使用如下:

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
+ import htmlPlugin from 'vite-plugin-index-html';
export default defineConfig({
plugins: [
vue(),
+ htmlPlugin({
+ input: './src/main.ts', // 指定确定的入口文件
+ preserveEntrySignatures: "exports-only", // 确保入口文件导出生命周期函数
+ })
],
})

配置基准路由#

对于基准路由的配置,可以依照 React 微应用接入Vue 微应用接入 相同的方式改造接入。

主应用加载 Vite 应用#

主应用需要通过 loadScriptMode: import 来加载 ES modules 类型微应用(Vite 应用)。同时由于 Vite 应用在 dev 下注入 HMR 相关的代码,因此建议通过 entry 方式 接入,保证开发和生产的配置一致性。

export default class App extends React.Component {
render() {
return (
<BasicLayout>
<AppRouter>
<AppRoute
activePath="/seller"
title="商家平台"
+ loadScriptMode="import" // 指定加载 ES modules 类型微应用
+ entry="https://icestark-child-seller"
/>
<AppRoute
activePath="/user"
//...
/>
</AppRouter>
</BasicLayout>
);
}
}

示例 Repo#

- - + + \ No newline at end of file diff --git a/docs/guide/use-child/react/index.html b/docs/guide/use-child/react/index.html index 72f1f1b1..0f1e2c85 100644 --- a/docs/guide/use-child/react/index.html +++ b/docs/guide/use-child/react/index.html @@ -11,8 +11,8 @@ React 微应用接入 | icestark - - + +
@@ -21,7 +21,7 @@
export function unmount(props) {
ReactDOM.unmountComponentAtNode(props.container);
}
// 注意:`setLibraryName` 的入参需要与 webpack 工程配置的 output.library 保持一致
setLibraryName('microApp');
if (!isInIcestark()) {
ReactDOM.render(<App />, document.getElementById('ice-container'));
}

2. 定义基准路由#

正常情况下,注册微应用时会为每个微应用分配一个基准路由比如 /seller,当前微应用的所有路由需要定义在基准路由之下,社区常见的路由库都可以通过参数非常简单的实现该功能。微应用可以通过 getBasename() API 获取自身的基准路由。

React 项目中使用 react-router

import React from 'react';
import { BrowserRouter as Router, Switch, Route, Redirect } from 'react-router-dom';
+import { getBasename } from '@ice/stark-app';
export default () => {
return (
+ <Router basename={getBasename()}>
<Switch>
// ...
</Switch>
</Router>
);
};

3. 构建为 UMD 产物#

入口文件通过导出 mountunmount 等标准生命周期后,需要配置工程上的改造,才能最终导出 UMD 标准的微应用。

以 webpack 工程为例:

module.exports = {
output: {
// 设置模块导出规范为 umd
libraryTarget: 'umd',
// 可选,设置模块在 window 上暴露的名称
library: 'microApp',
}
}
- - + + \ No newline at end of file diff --git a/docs/guide/use-child/vue/index.html b/docs/guide/use-child/vue/index.html index ff08ad2d..f2e164d1 100644 --- a/docs/guide/use-child/vue/index.html +++ b/docs/guide/use-child/vue/index.html @@ -11,8 +11,8 @@ Vue 微应用接入 | icestark - - + +
@@ -29,7 +29,7 @@
if (!isInIcestark()) {
createApp(App).mount('#app');
}

2. 定义基准路由#

Vue 项目中使用 vue-router

import Vue from 'vue';
import Router from 'vue-router';
import getBasename from '@ice/stark-app/lib/getBasename';
import routes from '@/config/routes';
Vue.use(Router);
export default new Router({
routes,
mode: 'history',
base: getBasename(),
});

3. 构建为 UMD 产物#

入口文件通过导出 mount、unmount 等标准生命周期后,需要配置工程上的改造,才能最终导出 UMD 标准的微应用。

以 webpack 工程为例:

module.exports = {
output: {
// 设置模块导出规范为 umd
libraryTarget: 'umd',
// 可选,设置模块在 window 上暴露的名称
library: 'microApp',
}
}
- - + + \ No newline at end of file diff --git a/docs/guide/use-layout/react/index.html b/docs/guide/use-layout/react/index.html index ef28ea4d..74bcecc4 100644 --- a/docs/guide/use-layout/react/index.html +++ b/docs/guide/use-layout/react/index.html @@ -11,8 +11,8 @@ React 主应用接入 | icestark - - + +
@@ -33,7 +33,7 @@
handleRouteChange = (pathname) => {
console.log('route change', pathname);
// 如有需求,可根据 pathname 切换 layout 的形态
this.setState({
pathname,
});
}
render() {
const { pathname } = this.state;
return (
<!-- BasicLayout 可根据 pathname 属性切换展现形式 -->
<BasicLayout pathname={pathname}>
<AppRouter
onRouteChange={this.handleRouteChange}
>
<AppRoute />
</AppRouter>
</BasicLayout>
);
}
}
- - + + \ No newline at end of file diff --git a/docs/guide/use-layout/vue/index.html b/docs/guide/use-layout/vue/index.html index e529c381..ba67f0d1 100644 --- a/docs/guide/use-layout/vue/index.html +++ b/docs/guide/use-layout/vue/index.html @@ -11,8 +11,8 @@ Vue 主应用接入 | icestark - - + +
@@ -22,7 +22,7 @@
const appContainer = document.getElementById('icestarkNode');
registerMicroApps([
{
name: 'app1',
activePath: ['/', '/message', '/about'],
exact: true,
title: '通用页面',
container: appContainer,
url: ['//unpkg.com/icestark-child-common/build/js/index.js'],
},
{
name: 'app2',
activePath: '/seller',
title: '商家平台',
container: appContainer,
url: [
'//unpkg.com/icestark-child-seller/build/js/index.js',
'//unpkg.com/icestark-child-seller/build/css/index.css',
],
},
]);
start();

微应用配置项#

name#

name 字段作为微应用唯一标识,不可重复注册。注册后的微应用均通过 name 标识对微应用进行加载或者卸载。

activePath#

微应用激活的路由规则,可以有四种方式配置:

  • string:配置为字符串类型时,将自动根据设置的路径进行匹配,如设置为 /seller 的情况下,路由只要匹配到 /seller 均会加载改项配置的微应用
  • string[]:数组形式在上述字符串形式的基础上增加了多个路由匹配配置,只要路由匹配到数组中的任意一项均会加载微应用
  • PathData[]:默认情况下设置的路由规则均为通配规则,如果想对路由进行精准匹配或者区分大小写,可以以对象方式配置每一项路由规则,其中可选配置项包括:exactstrictsensitive
  • Function:通过函数方式执行的形式将根据函数执行结果决定微应用是否加载,函数接受参数为当前 url 地址

container#

微应用挂载的 DOM 节点,通常情况下所有微应用的 container 都是同一个。

微应用入口 url/entry/entryContent#

详见 微应用 - 入口规范

- - + + \ No newline at end of file diff --git a/error/index.html b/error/index.html index e68b5596..6c560bf1 100644 --- a/error/index.html +++ b/error/index.html @@ -11,13 +11,13 @@ icestark-面向大型系统的微前端解决方案 | icestark - - + + - - + + \ No newline at end of file diff --git a/index.html b/index.html index ecf3aae3..b7460d1e 100644 --- a/index.html +++ b/index.html @@ -11,13 +11,13 @@ icestark-面向大型系统的微前端解决方案 | icestark - - + +
- - + + \ No newline at end of file diff --git a/search/index.html b/search/index.html index 112bf315..eb537cde 100644 --- a/search/index.html +++ b/search/index.html @@ -11,13 +11,13 @@ Search the documentation | icestark - - + + - - + + \ No newline at end of file