Commit fba1af02 authored by Ethan_Wan's avatar Ethan_Wan Committed by ι™ˆεΈ…

feat: instead of dva/dynamic using react-loadable

parent 67f686a2
import { createElement } from 'react'; import React, { createElement } from 'react';
import dynamic from 'dva/dynamic'; import { Spin } from 'antd';
import pathToRegexp from 'path-to-regexp'; import pathToRegexp from 'path-to-regexp';
import Loadable from 'react-loadable';
import { getMenuData } from './menu'; import { getMenuData } from './menu';
let routerDataCache; let routerDataCache;
...@@ -13,15 +14,17 @@ const modelNotExisted = (app, model) => ...@@ -13,15 +14,17 @@ const modelNotExisted = (app, model) =>
// wrapper of dynamic // wrapper of dynamic
const dynamicWrapper = (app, models, component) => { const dynamicWrapper = (app, models, component) => {
// () => require('module') // register models
// transformed by babel-plugin-dynamic-import-node-sync
if (component.toString().indexOf('.then(') < 0) {
models.forEach(model => { models.forEach(model => {
if (modelNotExisted(app, model)) { if (modelNotExisted(app, model)) {
// eslint-disable-next-line // eslint-disable-next-line
app.model(require(`../models/${model}`).default); app.model(require(`../models/${model}`).default);
} }
}); });
// () => require('module')
// transformed by babel-plugin-dynamic-import-node-sync
if (component.toString().indexOf('.then(') < 0) {
return props => { return props => {
if (!routerDataCache) { if (!routerDataCache) {
routerDataCache = getRouterData(app); routerDataCache = getRouterData(app);
...@@ -33,12 +36,8 @@ const dynamicWrapper = (app, models, component) => { ...@@ -33,12 +36,8 @@ const dynamicWrapper = (app, models, component) => {
}; };
} }
// () => import('module') // () => import('module')
return dynamic({ return Loadable({
app, loader: () => {
models: () =>
models.filter(model => modelNotExisted(app, model)).map(m => import(`../models/${m}.js`)),
// add routerData prop
component: () => {
if (!routerDataCache) { if (!routerDataCache) {
routerDataCache = getRouterData(app); routerDataCache = getRouterData(app);
} }
...@@ -51,6 +50,9 @@ const dynamicWrapper = (app, models, component) => { ...@@ -51,6 +50,9 @@ const dynamicWrapper = (app, models, component) => {
}); });
}); });
}, },
loading: () => {
return <Spin size="large" className="global-spin" />;
},
}); });
}; };
......
...@@ -18,14 +18,14 @@ body { ...@@ -18,14 +18,14 @@ body {
-moz-osx-font-smoothing: grayscale; -moz-osx-font-smoothing: grayscale;
} }
.globalSpin {
width: 100%;
margin: 40px 0 !important;
}
// temp fix for https://github.com/ant-design/ant-design/commit/a1fafb5b727b62cb0be29ce6e9eca8f579d4f8b7 // temp fix for https://github.com/ant-design/ant-design/commit/a1fafb5b727b62cb0be29ce6e9eca8f579d4f8b7
:global { :global {
.ant-spin-container { .ant-spin-container {
overflow: visible !important; overflow: visible !important;
} }
.global-spin {
width: 100%;
margin: 40px 0 !important;
}
} }
import React from 'react'; import React from 'react';
import { routerRedux, Route, Switch } from 'dva/router'; import { routerRedux, Route, Switch } from 'dva/router';
import { LocaleProvider, Spin } from 'antd'; import { LocaleProvider } from 'antd';
import zhCN from 'antd/lib/locale-provider/zh_CN'; import zhCN from 'antd/lib/locale-provider/zh_CN';
import dynamic from 'dva/dynamic';
import { getRouterData } from './common/router'; import { getRouterData } from './common/router';
import Authorized from './utils/Authorized'; import Authorized from './utils/Authorized';
import { getQueryPath } from './utils/utils'; import { getQueryPath } from './utils/utils';
import styles from './index.less';
const { ConnectedRouter } = routerRedux; const { ConnectedRouter } = routerRedux;
const { AuthorizedRoute } = Authorized; const { AuthorizedRoute } = Authorized;
dynamic.setDefaultLoadingComponent(() => {
return <Spin size="large" className={styles.globalSpin} />;
});
function RouterConfig({ history, app }) { function RouterConfig({ history, app }) {
const routerData = getRouterData(app); const routerData = getRouterData(app);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment