已有应用迁移

icestark 完全面向 react 设计,因此已有 react 应用的迁移成本很低。

安装 icestark

  1. $ npm i @ice/stark --save

在框架应用中使用

框架应用主要是对各个子应用的资源 url、渲染位置、path 的管理注意各个子应用的 path 配置不能重复,规则类似 Routepath 配置

  1. // src/index.js
  2. import React from 'react';
  3. import ReactDOM from 'react-dom';
  4. import { AppRouter, AppRoute } from '@ice/stark';
  5. class Layout extends React.Component {
  6. onRouteChange = (pathname, query) => {
  7. console.log(pathname, query);
  8. }
  9. render() {
  10. return (
  11. <div>
  12. <div>this is common header</div>
  13. <AppRouter
  14. onRouteChange={this.onRouteChange}
  15. ErrorComponent={<div>js bundle loaded error</div>}
  16. NotFoundComponent={<div>NotFound</div>}
  17. >
  18. <AppRoute
  19. path={['/', '/list', '/detail']}
  20. basename="/"
  21. exact
  22. title="商家平台"
  23. url={[
  24. '//g.alicdn.com/icestark-demo/child/0.2.1/js/index.js',
  25. '//g.alicdn.com/icestark-demo/child/0.2.1/css/index.css',
  26. ]}
  27. />
  28. <AppRoute
  29. path="/waiter"
  30. basename="/waiter"
  31. title="小二平台"
  32. url={[
  33. '//g.alicdn.com/icestark-demo/child2/0.2.1/js/index.js',
  34. '//g.alicdn.com/icestark-demo/child2/0.2.1/css/index.css',
  35. ]}
  36. />
  37. </AppRouter>
  38. <div>this is common footer</div>
  39. </div>
  40. );
  41. }
  42. }
  • AppRouter 定位子应用渲染节点

  • AppRoute 设置子应用相关配置,path 配置有效路由信息、basename 配置统一的路由前缀,url 配置静态资源路径

  • icestark 会按照 react-router 的路由解析规则,判断当前生效 path,加载对应子应用的静态资源 url,渲染在 AppRouter

在子应用中使用

icestark 对子应用的代码侵入性极少,只有三方面:

ReactDOM 渲染节点

  1. // src/index.js
  2. import React from 'react';
  3. import ReactDOM from 'react-dom';
  4. import { getMountNode } from '@ice/stark';
  5. import App from './App';
  6. ReactDOM.render(<App />, getMountNode());
  • getMountNode 方法会判断当前应用的运行环境,如果以子应用方式运行会使用 icestark 内部创建的 dom 节点 / shadowDOM 节点,否则会默认采用<div id="ice-container"></div>节点。同时,该方法支持传 DOM 节点作为默认节点。

子应用间跳转

  1. import React from 'react';
  2. import { AppLink } from '@ice/stark';
  3. class App extends React.Component {
  4. // ...
  5. render() {
  6. return (
  7. <div>
  8. <AppLink to="/waiter/list">子应用间跳转使用 AppLink </AppLink>
  9. </div>
  10. );
  11. }
  12. }
  • 跨应用跳转的情况下使用 AppLink,表示本次跳转需要重新加载静态资源;子应用内部跳转,仍使用 Link

Router 注入 basename、渲染全局 404

  1. import React from 'react';
  2. import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
  3. import { renderNotFound, getBasename } from '@ice/stark';
  4. function List() {
  5. return <div>List</div>;
  6. }
  7. function Detail() {
  8. return <div>Detail</div>;
  9. }
  10. export default class App extends React.Component {
  11. render() {
  12. return (
  13. // 通过 getBasename 将在框架应用中注册的 basename 注入到子应用中
  14. <Router basename={getBasename()}>
  15. <Switch>
  16. <Route exact path="/" component={List} />
  17. <Route path="/detail" component={Detail} />
  18. <Route
  19. component={() => {
  20. // 通知框架应用渲染全局 404
  21. return renderNotFound();
  22. }}
  23. />
  24. </Switch>
  25. </Router>
  26. );
  27. }
  28. }
  • 子应用通过 getBasename 方法获取框架应用中配置的 basename,通过 renderNotFound 方法触发框架应用渲染 404