Skip to content

Commit 6a4e54d

Browse files
committed
[migrate] replace React 19 with Preact 10 to reduce Bundle Size
[optimize] simplify i18n, Router & Model details
1 parent 461a224 commit 6a4e54d

9 files changed

Lines changed: 398 additions & 394 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,6 @@ pnpm build
8282
[18]: https://github.com/new?template_name=React-MobX-Ant-Design-ts&template_owner=idea2app
8383
[19]: https://github.com/idea2app/React-MobX-Ant-Design-ts/blob/ae6204a04c108eddff7ff5265341676b55918509/.github/workflows/main.yml#L10-L12
8484
[20]: https://github.com/idea2app/React-MobX-Ant-Design-ts/settings/secrets/actions
85-
[21]: https://github.com/kaiyuanshe/kaiyuanshe.github.io/blob/bb4675a56bf1d6b207231313da5ed0af7cf0ebd6/.github/workflows/pull-request.yml#L32-L56
85+
[21]: https://github.com/idea2app/Lark-Next-Bootstrap-ts/blob/5c58fd8c6faf85a89a19a0358c315dc8909098e7/.github/workflows/main.yml#L31-L60
8686
[22]: https://github.com/idea2app/React-MobX-Ant-Design-ts/issues/new/choose
8787
[23]: https://github.com/idea2app/React-MobX-Ant-Design-ts/projects

package.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"@ant-design/icons": "^6.1.0",
1616
"antd": "^6.0.1",
1717
"browser-unhandled-rejection": "^1.0.2",
18+
"idb-keyval": "^6.2.2",
1819
"koajax": "^3.1.2",
1920
"lodash": "^4.17.21",
2021
"mobx": "^6.15.0",
@@ -24,8 +25,9 @@
2425
"mobx-react-helper": "^0.5.1",
2526
"mobx-restful": "^2.1.4",
2627
"mobx-restful-table": "^2.6.2",
27-
"react": "^19.2.0",
28-
"react-dom": "^19.2.0",
28+
"preact": "^10.28.0",
29+
"react": "npm:@preact/compat",
30+
"react-dom": "npm:@preact/compat",
2931
"react-router": "^7.10.0",
3032
"react-router-class-tools": "^0.2.1",
3133
"react-router-dom": "^7.10.0",
@@ -56,6 +58,11 @@
5658
"msgpackr-extract"
5759
]
5860
},
61+
"alias": {
62+
"react": "preact/compat",
63+
"react-dom": "preact/compat",
64+
"react/jsx-runtime": "preact/jsx-runtime"
65+
},
5966
"@parcel/resolver-default": {
6067
"packageExports": true
6168
},

pnpm-lock.yaml

Lines changed: 371 additions & 372 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/component/RestTable.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ export class RestTable<T extends DataObject> extends ObservedComponent<
156156
this.props;
157157

158158
const checkable = deletable || typeof onCheck === 'function',
159-
{ t } = this.props.translator,
159+
{ t } = props.translator,
160160
{ downloading, pageSize, pageIndex, totalCount = 0, currentPage } = store;
161161

162162
return (

src/model/Translation.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export const i18n = new TranslationModel({
88
'en-US': () => import('../translation/en-US')
99
});
1010

11+
export const { t } = i18n;
12+
1113
export const LanguageName: Record<(typeof i18n)['currentLanguage'], string> = {
1214
'zh-CN': '简体中文',
1315
'zh-TW': '繁體中文',

src/model/User.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { clear } from 'idb-keyval';
12
import { observable } from 'mobx';
3+
import { persist, restore } from 'mobx-restful';
24

35
export interface Passport {
46
account: string;
@@ -9,27 +11,26 @@ export interface User extends Pick<Passport, 'account'> {
911
avatar?: string;
1012
}
1113

12-
const { localStorage: store } = globalThis;
13-
1414
export class UserModel {
15+
@persist()
1516
@observable
16-
accessor session: User | undefined =
17-
store['session'] && JSON.parse(store['session']);
17+
accessor session: User | undefined;
18+
19+
restored = restore(this, 'User');
1820

1921
async signIn({ account, key }: Passport) {
2022
if (account !== 'admin' || key !== '19890604')
2123
throw new URIError("The key can't be forgotten!");
2224

23-
this.session = {
25+
return (this.session = {
2426
account,
2527
avatar: 'https://github.com/idea2app.png'
26-
};
27-
store['session'] = JSON.stringify(this.session);
28+
});
2829
}
2930

3031
signOut() {
3132
this.session = undefined;
32-
store.clear();
33+
return clear();
3334
}
3435
}
3536

src/page/Home.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ import { Component } from 'react';
44
import { RouteComponentProps, withRouter } from 'react-router-class-tools';
55

66
import project from '../model/Project';
7-
import { i18n } from '../model/Translation';
8-
9-
const { t } = i18n;
7+
import { t } from '../model/Translation';
108

9+
@withRouter
1110
@observer
12-
class HomePage extends Component<
11+
export class HomePage extends Component<
1312
RouteComponentProps<{}, {}, { guest: string }>
1413
> {
1514
componentDidMount() {
@@ -52,5 +51,3 @@ class HomePage extends Component<
5251
);
5352
}
5453
}
55-
56-
export default withRouter(HomePage);

src/page/Pagination.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import { Component } from 'react';
66
import { Badge } from '../component/Badge';
77
import { Column, RestTable } from '../component/RestTable';
88
import { repositoryStore } from '../model/service';
9-
import { i18n } from '../model/Translation';
10-
11-
const { t } = i18n;
9+
import { i18n, t } from '../model/Translation';
1210

1311
@observer
1412
export class PaginationPage extends Component {

src/page/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { HashRouter, Route, Routes } from 'react-router-dom';
22

33
import { PageBox } from '../component/PageBox';
4-
import HomePage from './Home';
4+
import { HomePage } from './Home';
55
import { PaginationPage } from './Pagination';
66
import { ScrollListPage } from './ScrollList';
77

0 commit comments

Comments
 (0)