Skip to content

Commit

Permalink
pref: optimize performance (#3542)
Browse files Browse the repository at this point in the history
* pref: optimize performance

* pref: use less img

* pref: use less img
  • Loading branch information
chenshuai2144 authored Feb 18, 2019
1 parent 9f84663 commit 10ef3ea
Show file tree
Hide file tree
Showing 14 changed files with 105 additions and 51 deletions.
6 changes: 2 additions & 4 deletions config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const plugins = [
dynamicImport: {
loadingComponent: './components/PageLoading/index',
webpackChunkName: true,
level: 3,
},
pwa: pwa
? {
Expand Down Expand Up @@ -76,10 +77,7 @@ export default {
},
externals: {
'@antv/data-set': 'DataSet',
// if is production externals react react-dom and bizcharts
...(NODE_ENV === 'production'
? { react: 'React', 'react-dom': 'ReactDOM', bizcharts: 'BizCharts' }
: {}),
bizcharts: 'BizCharts',
},
// proxy: {
// '/server/api/': {
Expand Down
2 changes: 1 addition & 1 deletion mock/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const avatars = [
];

const avatars2 = [
'https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png',
'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
'https://gw.alipayobjects.com/zos/rmsportal/cnrhVkzwxjPwAaCfPbdc.png',
'https://gw.alipayobjects.com/zos/rmsportal/gaOngJwsRYRaVAuXXcmB.png',
'https://gw.alipayobjects.com/zos/rmsportal/ubnKSIfAJTxIgXOKlciN.png',
Expand Down
2 changes: 1 addition & 1 deletion mock/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export default {
// 支持值为 Object 和 Array
'GET /api/currentUser': {
name: 'Serati Ma',
avatar: 'https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png',
avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
userid: '00000001',
email: 'antdesign@alipay.com',
signature: '海纳百川,有容乃大',
Expand Down
1 change: 0 additions & 1 deletion src/components/ActiveChart/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { Component } from 'react';
import { MiniArea } from '../Charts';
import NumberInfo from '../NumberInfo';

import styles from './index.less';

function fixedZero(val) {
Expand Down
42 changes: 42 additions & 0 deletions src/components/Charts/AsyncLoadBizCharts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import React from 'react';
import PageLoading from '../PageLoading';
import { importCDN } from '@/utils/utils';

let isLoaderBizChart = false;
const loadBizCharts = async () => {
if (isLoaderBizChart) {
return Promise.resolve(true);
}
await Promise.all([
importCDN('//gw.alipayobjects.com/os/lib/bizcharts/3.4.3/umd/BizCharts.min.js'),
importCDN('//gw.alipayobjects.com/os/lib/antv/data-set/0.10.1/dist/data-set.min.js'),
]);
// eslint-disable-next-line no-console
console.log('bizCharts load success');
isLoaderBizChart = true;
return Promise.resolve(true);
};

class AsyncLoadBizCharts extends React.Component {
state = {
loading: !isLoaderBizChart,
};

async componentDidMount() {
await loadBizCharts();
this.setState({
loading: false,
});
}

render() {
const { children } = this.props;
const { loading } = this.state;
if (!loading) {
return children;
}
return <PageLoading />;
}
}

export { loadBizCharts, AsyncLoadBizCharts };
15 changes: 0 additions & 15 deletions src/components/Charts/g2.js

This file was deleted.

35 changes: 23 additions & 12 deletions src/components/Charts/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
import React, { Suspense } from 'react';
import numeral from 'numeral';
import './g2';
import ChartCard from './ChartCard';
import Bar from './Bar';
import Pie from './Pie';
import Radar from './Radar';
import Gauge from './Gauge';
import MiniArea from './MiniArea';
import MiniBar from './MiniBar';
import MiniProgress from './MiniProgress';
import Field from './Field';
import WaterWave from './WaterWave';
import TagCloud from './TagCloud';
import TimelineChart from './TimelineChart';

const getComponent = Component => {
return props => {
return (
<Suspense fallback={null}>
<Component {...props} />
</Suspense>
);
};
};

const Bar = getComponent(React.lazy(() => import('./Bar')));
const Pie = getComponent(React.lazy(() => import('./Pie')));
const Radar = getComponent(React.lazy(() => import('./Radar')));
const Gauge = getComponent(React.lazy(() => import('./Gauge')));
const MiniArea = getComponent(React.lazy(() => import('./MiniArea')));
const MiniBar = getComponent(React.lazy(() => import('./MiniBar')));
const MiniProgress = getComponent(React.lazy(() => import('./MiniProgress')));
const Field = getComponent(React.lazy(() => import('./Field')));
const WaterWave = getComponent(React.lazy(() => import('./WaterWave')));
const TagCloud = getComponent(React.lazy(() => import('./TagCloud')));
const TimelineChart = getComponent(React.lazy(() => import('./TimelineChart')));

const yuan = val => ${numeral(val).format('0,0')}`;

Expand Down
2 changes: 2 additions & 0 deletions src/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { notification, Button, message } from 'antd';
import { formatMessage } from 'umi/locale';
import defaultSettings from './defaultSettings';

window.React = React;

const { pwa } = defaultSettings;
// if pwa is true
if (pwa) {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Account/Settings/BaseView.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class BaseView extends Component {
if (currentUser.avatar) {
return currentUser.avatar;
}
const url = 'https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png';
const url = 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png';
return url;
}

Expand Down
10 changes: 7 additions & 3 deletions src/pages/Dashboard/Analysis.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import React, { Component, Suspense } from 'react';
import { connect } from 'dva';
import { Row, Col, Icon, Menu, Dropdown } from 'antd';

import GridContent from '@/components/PageHeaderWrapper/GridContent';
import { getTimeDistance } from '@/utils/utils';

import styles from './Analysis.less';
import PageLoading from '@/components/PageLoading';
import { AsyncLoadBizCharts } from '@/components/Charts/AsyncLoadBizCharts';

const IntroduceRow = React.lazy(() => import('./IntroduceRow'));
const SalesCard = React.lazy(() => import('./SalesCard'));
Expand All @@ -26,6 +25,7 @@ class Analysis extends Component {
};

componentDidMount() {
console.log('run');
const { dispatch } = this.props;
this.reqRef = requestAnimationFrame(() => {
dispatch({
Expand Down Expand Up @@ -184,4 +184,8 @@ class Analysis extends Component {
}
}

export default Analysis;
export default props => (
<AsyncLoadBizCharts>
<Analysis {...props} />
</AsyncLoadBizCharts>
);
14 changes: 9 additions & 5 deletions src/pages/Dashboard/Monitor.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { PureComponent } from 'react';
import React, { Component } from 'react';
import { AsyncLoadBizCharts } from '@/components/Charts/AsyncLoadBizCharts';
import { connect } from 'dva';
import { formatMessage, FormattedMessage } from 'umi/locale';
import { Row, Col, Card, Tooltip } from 'antd';
Expand All @@ -8,7 +9,6 @@ import CountDown from '@/components/CountDown';
import ActiveChart from '@/components/ActiveChart';
import numeral from 'numeral';
import GridContent from '@/components/PageHeaderWrapper/GridContent';

import Authorized from '@/utils/Authorized';
import styles from './Monitor.less';

Expand All @@ -27,7 +27,7 @@ const havePermissionAsync = new Promise(resolve => {
monitor,
loading: loading.models.monitor,
}))
class Monitor extends PureComponent {
class Monitor extends Component {
componentDidMount() {
const { dispatch } = this.props;
dispatch({
Expand Down Expand Up @@ -110,7 +110,7 @@ class Monitor extends PureComponent {
}
>
<img
src="https://gw.alipayobjects.com/zos/rmsportal/HBWnDEUXCnGnGrRfrpKa.png"
src="https://gw.alipayobjects.com/zos/antfincdn/h%24wFbzuuzz/HBWnDEUXCnGnGrRfrpKa.png"
alt="map"
/>
</Tooltip>
Expand Down Expand Up @@ -242,4 +242,8 @@ class Monitor extends PureComponent {
}
}

export default Monitor;
export default props => (
<AsyncLoadBizCharts>
<Monitor {...props} />
</AsyncLoadBizCharts>
);
8 changes: 6 additions & 2 deletions src/pages/Dashboard/Workplace.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import moment from 'moment';
import { connect } from 'dva';
import Link from 'umi/link';
import { Row, Col, Card, List, Avatar } from 'antd';

import { AsyncLoadBizCharts } from '@/components/Charts/AsyncLoadBizCharts';
import { Radar } from '@/components/Charts';
import EditableLinkGroup from '@/components/EditableLinkGroup';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
Expand Down Expand Up @@ -253,4 +253,8 @@ class Workplace extends PureComponent {
}
}

export default Workplace;
export default props => (
<AsyncLoadBizCharts>
<Workplace {...props} />
</AsyncLoadBizCharts>
);
6 changes: 0 additions & 6 deletions src/pages/document.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@
/>
<title>Ant Design Pro</title>
<link rel="icon" href="/favicon.png" type="image/x-icon" />
<% if(context.env === 'production') { %>
<script src="//gw.alipayobjects.com/os/lib/react/16.8.1/umd/react.production.min.js"></script>
<script src="//gw.alipayobjects.com/os/lib/react-dom/16.8.1/umd/react-dom.production.min.js"></script>
<script src="//gw.alipayobjects.com/os/lib/bizcharts/3.4.3/umd/BizCharts.min.js"></script>
<% }%>
<script src="https://gw.alipayobjects.com/os/lib/antv/data-set/0.10.1/dist/data-set.min.js"></script>
</head>
<body>
<noscript>Sorry, we need js to run correctly!</noscript>
Expand Down
11 changes: 11 additions & 0 deletions src/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,14 @@ export function formatWan(val) {
export function isAntdPro() {
return window.location.hostname === 'preview.pro.ant.design';
}

export const importCDN = (url, name) =>
new Promise(resolve => {
const dom = document.createElement('script');
dom.src = url;
dom.type = 'text/javascript';
dom.onload = () => {
resolve(window[name]);
};
document.head.appendChild(dom);
});

1 comment on commit 10ef3ea

@tsuijie
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个提交的特性是breaking change,会导致其他自定义模块非异步加载的BizCharts都报错。

Please sign in to comment.