Skip to content

Commit 0e85fbb

Browse files
committed
add LazyComponent
1 parent 90070aa commit 0e85fbb

File tree

9 files changed

+143
-38
lines changed

9 files changed

+143
-38
lines changed

admin-ui/rsbuild.config.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ export default defineConfig({
5252
template: './public/index.html',
5353
},
5454
performance: {
55-
chunkSplit: {
56-
strategy: 'split-by-size',
57-
minSize: 10000,
58-
maxSize: 30000,
59-
},
55+
// chunkSplit: {
56+
// strategy: 'split-by-size',
57+
// minSize: 10000,
58+
// maxSize: 30000,
59+
// },
6060
},
6161
tools: {
6262
rspack: (config) => {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import React, {lazy, Suspense} from 'react';
2+
import {Spin} from "antd";
3+
4+
5+
interface LazyComponentProps {
6+
lazy:()=> Promise<{default: React.ComponentType<any>}>;
7+
}
8+
9+
const LazyComponent:React.FC<LazyComponentProps> = (props) => {
10+
11+
const PageComponent = lazy(() => {
12+
return props.lazy();
13+
});
14+
15+
const fallback = (
16+
<Spin size="large" tip="Loading">
17+
<div style={{minHeight: 100}}/>
18+
</Spin>
19+
);
20+
21+
return (
22+
<Suspense fallback={fallback}>
23+
<PageComponent/>
24+
</Suspense>
25+
);
26+
};
27+
28+
export default LazyComponent;
29+

admin-ui/src/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import store from "@/store/Redux";
77
import {ConfigProvider} from "antd";
88
import zhCN from 'antd/es/locale/zh_CN';
99
import '@/styles/index.scss';
10-
import "@/config/register.component";
10+
import("@/config/register.component").then(()=>{
11+
console.log('register.component loaded');
12+
});
1113
import {CSSUtils, ThemeConfig, ThemeProvider} from "@codingapi/ui-framework";
1214

1315
const root = ReactDOM.createRoot(

admin-ui/src/pages/welcome/index.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ import React from 'react';
22
import {PageContainer} from "@ant-design/pro-components";
33
import './index.scss';
44

5-
65
const WelcomePage = () => {
76

87
return (
98
<PageContainer>
10-
119
<ul>Admin-UI 支持的功能有</ul>
1210
<ul>1. 自定义流程</ul>
1311
<ul>2. 表单渲染</ul>

mobile-ui/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
"cross-env": "^7.0.3",
1616
"dayjs": "^1.11.13",
1717
"jszip": "^3.10.1",
18-
"lodash": "^4.17.21",
1918
"moment": "^2.30.1",
2019
"react": "^18.3.1",
2120
"react-dom": "^18.3.1",
@@ -75,6 +74,7 @@
7574
"identity-obj-proxy": "^3.0.0",
7675
"jest": "^29.7.0",
7776
"jest-environment-jsdom": "^29.7.0",
77+
"lodash": "^4.17.21",
7878
"mockjs": "^1.1.0",
7979
"sass": "^1.89.2",
8080
"sass-loader": "^16.0.1",

mobile-ui/rsbuild.config.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ export default defineConfig({
5050
template: './public/index.html',
5151
},
5252
performance: {
53-
chunkSplit: {
54-
strategy: 'split-by-size',
55-
minSize: 10000,
56-
maxSize: 30000,
57-
},
53+
// chunkSplit: {
54+
// strategy: 'split-by-size',
55+
// minSize: 10000,
56+
// maxSize: 30000,
57+
// },
5858
},
5959
tools: {
6060
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React, {lazy, Suspense} from 'react';
2+
import {Loading} from "antd-mobile";
3+
4+
5+
interface LazyComponentProps {
6+
lazy:()=> Promise<{default: React.ComponentType<any>}>;
7+
}
8+
9+
const LazyComponent:React.FC<LazyComponentProps> = (props) => {
10+
11+
const PageComponent = lazy(() => {
12+
return props.lazy();
13+
});
14+
15+
return (
16+
<Suspense fallback={<Loading />}>
17+
<PageComponent/>
18+
</Suspense>
19+
);
20+
};
21+
22+
export default LazyComponent;
23+

mobile-ui/src/config/route.tsx

Lines changed: 71 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,109 @@
1-
import Login from "@/pages/login";
21
import NotFound from "@/layout/NotFound";
3-
import Layout from "@/layout";
4-
import HomePage from "@/pages/home";
52
import {Route} from "react-router";
63
import {RouteObject} from "react-router/dist/lib/context";
74
import React from "react";
8-
import LeaveListPage from "@/pages/leave";
9-
import LeaveCreatePage from "@/pages/leave/create";
10-
import FlowListPage from "@/pages/flow";
11-
import FlowDetailPage from "@/pages/flow/detail";
12-
import LeaveDetailPage from "@/pages/leave/detail";
13-
import FormPage from "@/pages/form";
14-
import MircoPage from "@/pages/mirco";
15-
5+
import LazyComponent from "@/components/LazyComponent";
166

177
export const routes: RouteObject[] = [
188
{
199
path: "/login",
20-
element: <Login/>,
10+
element: (
11+
<LazyComponent
12+
lazy={() => {
13+
return import('@/pages/login');
14+
}}
15+
/>
16+
),
2117
},
2218
{
2319
path: '/',
24-
element: <Layout/>,
20+
element: (
21+
<LazyComponent
22+
lazy={() => {
23+
return import('@/layout/index');
24+
}}
25+
/>
26+
),
2527
children: [
2628
{
2729
path: "/",
28-
element: <HomePage/>,
30+
element: (
31+
<LazyComponent
32+
lazy={() => {
33+
return import('@/pages/home');
34+
}}
35+
/>
36+
),
2937
},
3038
{
3139
path: "/form",
32-
element: <FormPage/>,
40+
element: (
41+
<LazyComponent
42+
lazy={() => {
43+
return import('@/pages/form');
44+
}}
45+
/>
46+
),
3347
},
3448
{
3549
path: "/mirco",
36-
element: <MircoPage/>,
50+
element: (
51+
<LazyComponent
52+
lazy={() => {
53+
return import('@/pages/mirco');
54+
}}
55+
/>
56+
),
3757
},
3858
{
3959
path: "/leave/index",
40-
element: <LeaveListPage/>,
60+
element: (
61+
<LazyComponent
62+
lazy={() => {
63+
return import('@/pages/leave');
64+
}}
65+
/>
66+
),
4167
},
4268
{
4369
path: "/leave/create",
44-
element: <LeaveCreatePage/>,
70+
element: (
71+
<LazyComponent
72+
lazy={() => {
73+
return import('@/pages/leave/create');
74+
}}
75+
/>
76+
),
4577
},
4678
{
4779
path: "/leave/detail",
48-
element: <LeaveDetailPage/>,
80+
element: (
81+
<LazyComponent
82+
lazy={() => {
83+
return import('@/pages/leave/detail');
84+
}}
85+
/>
86+
),
4987
},
5088
{
5189
path: "/flow/list",
52-
element: <FlowListPage/>,
90+
element: (
91+
<LazyComponent
92+
lazy={() => {
93+
return import('@/pages/flow');
94+
}}
95+
/>
96+
),
5397
},
5498
{
5599
path: "/flow/detail",
56-
element: <FlowDetailPage/>,
100+
element: (
101+
<LazyComponent
102+
lazy={() => {
103+
return import('@/pages/flow/detail');
104+
}}
105+
/>
106+
),
57107
},
58108
{
59109
path: '/*',

mobile-ui/src/index.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import React, {createContext} from 'react';
22
import ReactDOM from 'react-dom/client';
3-
import reportWebVitals from './reportWebVitals';
3+
// import reportWebVitals from './reportWebVitals';
44
import '@/styles/index.scss';
55
import {createHashRouter, RouterProvider} from "react-router-dom";
66
import {routes} from "@/config/route";
77
import zhCN from "antd-mobile/es/locales/zh-CN";
88
import {ConfigProvider} from "antd-mobile";
9-
import "@/config/register.component";
9+
10+
import("@/config/register.component").then(() => {
11+
console.log('register.component loaded');
12+
});
1013

1114
const root = ReactDOM.createRoot(
1215
document.getElementById('root') as HTMLElement
@@ -26,4 +29,4 @@ root.render(
2629

2730
// If you want to start measuring performance in your app, pass a function
2831
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
29-
reportWebVitals();
32+
// reportWebVitals();

0 commit comments

Comments
 (0)