Skip to content

Commit 1cb1cb7

Browse files
Merge branch 'feature/react-18-stream'
2 parents fac71d1 + 64af0d7 commit 1cb1cb7

62 files changed

Lines changed: 2919 additions & 3962 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.env

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1+
# dev env
12
DEV_HOST=localhost
23
DEV_PORT=9000
34
WDS_PORT=9001
5+
PUBLIC_DEV_API_HOST=localhost:9000
6+
# prod env
47
PROD_PORT=5000
58
PROD_HOST=localhost
6-
PUBLIC_API_HOST=localhost:9000
9+
PUBLIC_PROD_API_HOST=localhost:5000
10+
# feature
711
CRYPTO_KEY=ad$cr3efW89ypg
812
SSR=true
9-
UI=chakra
1013
MIDDLEWARE=false
1114
ANIMATE_ROUTER=false
15+
# entry
1216
SERVER_ENTRY=./src/server/entry.ts
1317
CLIENT_ENTRY=./src/client/entry.tsx

.swcrc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"jsc": {
3+
"parser": {
4+
"syntax": "typescript",
5+
"tsx": true,
6+
"decorators": true
7+
},
8+
"transform": {
9+
"react": {
10+
"runtime": "automatic",
11+
"pragma": "React.createElement",
12+
"pragmaFrag": "React.Fragment",
13+
"throwIfNamespace": true,
14+
"useBuiltins": true
15+
}
16+
}
17+
},
18+
"sourceMaps": true
19+
}

babel.config.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ function isWebpack(caller) {
99
module.exports = (api) => {
1010
const web = api.caller(isWebTarget);
1111
const webpack = api.caller(isWebpack);
12-
1312

1413
api.cache.using(() => process.env.NODE_ENV === "production");
1514

@@ -39,7 +38,6 @@ module.exports = (api) => {
3938
// plugins.push("@babel/plugin-syntax-dynamic-import");
4039
plugins.push("@babel/plugin-transform-runtime");
4140
plugins.push("@babel/plugin-proposal-export-default-from");
42-
plugins.push("@loadable/babel-plugin");
4341

4442
return {
4543
presets,

global.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ declare global {
3636
PROD_PORT: string;
3737
SSR: string;
3838
UI: "antd" | "material" | "chakra";
39-
PUBLIC_API_HOST: string;
39+
PUBLIC_DEV_API_HOST: string;
40+
PUBLIC_PROD_API_HOST: string;
4041
CRYPTO_KEY: string;
4142
SERVER_ENTRY: string;
4243
CLIENT_ENTRY: string;

package.json

Lines changed: 47 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-ssr",
3-
"version": "1.3.0",
3+
"version": "2.0.0",
44
"author": "mrwang",
55
"license": "MIT",
66
"scripts": {
@@ -15,14 +15,10 @@
1515
"test": "jest"
1616
},
1717
"dependencies": {
18-
"@chakra-ui/react": "^1.8.8",
19-
"@emotion/react": "^11.9.0",
18+
"@chakra-ui/react": "^2.2.1",
19+
"@emotion/react": "^11.9.3",
2020
"@emotion/server": "^11.4.0",
21-
"@emotion/styled": "^11.8.1",
22-
"@loadable/component": "^5.15.2",
23-
"@loadable/server": "^5.15.2",
24-
"@mui/material": "^5.8.0",
25-
"antd": "^4.20.4",
21+
"@emotion/styled": "^11.9.3",
2622
"axios": "^0.27.2",
2723
"chalk": "4",
2824
"compression": "^1.7.4",
@@ -31,100 +27,98 @@
3127
"dotenv": "^16.0.0",
3228
"express": "^4.18.1",
3329
"express-session": "^1.17.3",
34-
"framer-motion": "6.3.3",
35-
"immer": "^9.0.13",
30+
"framer-motion": "^6.3.11",
31+
"immer": "^9.0.15",
3632
"js-cookie": "^3.0.1",
3733
"lodash": "^4.17.21",
3834
"multer": "^1.4.4",
3935
"pretty-error": "^4.0.0",
40-
"react": "^17.0.2",
41-
"react-dom": "^17.0.2",
42-
"react-helmet-async": "1.2.3",
43-
"react-intl": "5.24.8",
44-
"react-redux": "^8.0.1",
45-
"react-router": "6.3.0",
46-
"react-router-dom": "6.3.0",
36+
"react": "^18.2.0",
37+
"react-dom": "^18.2.0",
38+
"react-helmet-async": "^1.3.0",
39+
"react-intl": "^6.0.4",
40+
"react-redux": "^8.0.2",
41+
"react-router": "^6.3.0",
42+
"react-router-dom": "^6.3.0",
4743
"redux": "^4.2.0",
4844
"redux-saga": "^1.1.3",
4945
"redux-thunk": "^2.4.1",
5046
"spark-md5": "^3.0.1",
51-
"webpack": "^5.72.1",
52-
"zustand": "^3.7.1"
47+
"webpack": "^5.73.0",
48+
"zustand": "^3.7.2"
5349
},
5450
"devDependencies": {
5551
"@babel/cli": "^7.17.10",
56-
"@babel/core": "^7.18.0",
52+
"@babel/core": "^7.18.5",
5753
"@babel/plugin-proposal-class-properties": "^7.17.12",
58-
"@babel/plugin-proposal-decorators": "^7.17.12",
54+
"@babel/plugin-proposal-decorators": "^7.18.2",
5955
"@babel/plugin-proposal-export-default-from": "^7.17.12",
6056
"@babel/plugin-proposal-object-rest-spread": "^7.18.0",
6157
"@babel/plugin-proposal-optional-chaining": "^7.17.12",
6258
"@babel/plugin-proposal-private-methods": "^7.17.12",
6359
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
64-
"@babel/plugin-transform-modules-commonjs": "^7.18.0",
65-
"@babel/plugin-transform-runtime": "^7.18.0",
66-
"@babel/preset-env": "^7.18.0",
60+
"@babel/plugin-transform-modules-commonjs": "^7.18.2",
61+
"@babel/plugin-transform-runtime": "^7.18.5",
62+
"@babel/preset-env": "^7.18.2",
6763
"@babel/preset-react": "^7.17.12",
6864
"@babel/preset-typescript": "^7.17.12",
69-
"@loadable/babel-plugin": "^5.13.2",
70-
"@loadable/webpack-plugin": "^5.15.2",
71-
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.6",
65+
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.7",
66+
"@swc/core": "^1.2.203",
7267
"@types/compression": "^1.7.2",
73-
"@types/cookie-parser": "^1.4.2",
68+
"@types/cookie-parser": "^1.4.3",
7469
"@types/cors": "^2.8.12",
7570
"@types/express": "^4.17.13",
7671
"@types/express-session": "^1.17.4",
77-
"@types/jest": "^27.4.1",
78-
"@types/js-cookie": "^3.0.1",
79-
"@types/loadable__component": "^5.13.4",
80-
"@types/loadable__server": "^5.12.6",
81-
"@types/lodash": "^4.14.180",
72+
"@types/jest": "^28.1.1",
73+
"@types/js-cookie": "^3.0.2",
74+
"@types/lodash": "^4.14.182",
8275
"@types/multer": "^1.4.7",
83-
"@types/node": "^17.0.23",
84-
"@types/react": "^17.0.42",
85-
"@types/react-dom": "^17.0.14",
76+
"@types/node": "^18.0.0",
77+
"@types/react": "^18.0.12",
78+
"@types/react-dom": "^18.0.5",
8679
"@types/webpack": "^5.28.0",
8780
"@types/webpack-env": "^1.17.0",
8881
"@types/webpack-hot-middleware": "^2.25.6",
89-
"@typescript-eslint/eslint-plugin": "^5.25.0",
90-
"@typescript-eslint/parser": "^5.25.0",
82+
"@typescript-eslint/eslint-plugin": "^5.28.0",
83+
"@typescript-eslint/parser": "^5.28.0",
9184
"autoprefixer": "^10.4.7",
92-
"babel-jest": "^27.5.1",
85+
"babel-jest": "^28.1.1",
9386
"babel-loader": "^8.2.5",
9487
"babel-plugin-import": "^1.13.5",
9588
"clean-webpack-plugin": "^4.0.0",
96-
"core-js": "^3.22.5",
89+
"core-js": "^3.23.1",
9790
"cross-env": "^7.0.3",
9891
"css-loader": "^6.7.1",
99-
"css-minimizer-webpack-plugin": "^3.4.1",
100-
"eslint": "^8.15.0",
92+
"css-minimizer-webpack-plugin": "^4.0.0",
93+
"eslint": "^8.17.0",
10194
"eslint-config-prettier": "^8.5.0",
10295
"eslint-import-resolver-typescript": "^2.7.1",
10396
"eslint-plugin-import": "^2.26.0",
10497
"eslint-plugin-jsx-a11y": "^6.5.1",
10598
"eslint-plugin-prettier": "^4.0.0",
10699
"eslint-plugin-react": "^7.30.0",
107-
"eslint-plugin-react-hooks": "^4.5.0",
100+
"eslint-plugin-react-hooks": "^4.6.0",
108101
"eslint-webpack-plugin": "^3.1.1",
109102
"file-loader": "^6.2.0",
110103
"fork-ts-checker-webpack-plugin": "^7.2.11",
111104
"identity-obj-proxy": "^3.0.0",
112-
"jest": "^27.5.1",
113-
"mini-css-extract-plugin": "^2.6.0",
105+
"jest": "^28.1.1",
106+
"mini-css-extract-plugin": "^2.6.1",
114107
"nodemon": "^2.0.15",
115108
"postcss": "^8.4.14",
116-
"postcss-loader": "^6.2.1",
117-
"prettier": "^2.6.2",
118-
"react-refresh": "^0.13.0",
119-
"sass": "^1.52.0",
120-
"sass-loader": "^12.6.0",
109+
"postcss-loader": "^7.0.0",
110+
"prettier": "^2.7.0",
111+
"react-refresh": "^0.14.0",
112+
"sass": "^1.52.3",
113+
"sass-loader": "^13.0.0",
121114
"style-loader": "^3.3.1",
115+
"swc-loader": "^0.2.3",
122116
"thread-loader": "^3.0.4",
123-
"typescript": "^4.5.5",
117+
"typescript": "^4.7.3",
124118
"webpack-bundle-analyzer": "^4.5.0",
125-
"webpack-cli": "^4.9.2",
119+
"webpack-cli": "^4.10.0",
126120
"webpack-dev-middleware": "^5.3.3",
127-
"webpack-dev-server": "^4.9.0",
121+
"webpack-dev-server": "^4.9.2",
128122
"webpack-hot-middleware": "^2.25.1",
129123
"webpack-manifest-plugin": "^5.0.0",
130124
"webpack-merge": "^5.8.0",

readme.md

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,10 @@ yarn run start
6868

6969
## animate router 路由切换动画
7070

71-
## 尝试 file router 开发自动更新 存在其他问题。。。 (middleware 模式 下)
71+
## file router 开发自动更新 (middleware 模式 下)
7272

7373
## 全新版本 升级优化修复重构 ^\_^
7474

7575
## react router 6
7676

77-
## 开箱既支持多种 UI 组件库(CSR / SSR) 通过环境变量 UI 切换(antd,material,chakra-ui,...)
78-
7977
## jest
80-
81-
## new stream SSR
82-
83-
## delete loadable component dependence
84-
85-
## TODO support swc-loader

script/dynamic.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ class DynamicRouter {
1313
getRouterConfig = (prePath, dirName) => {
1414
return new Promise((resolve) => {
1515
const routes = [];
16+
let indexPath = 0;
1617
let dynamicPath = 0;
18+
let fallbackPath = 0;
1719
fs.promises
1820
.readdir(dirName, { withFileTypes: true })
1921
.then((files) =>
@@ -32,6 +34,21 @@ class DynamicRouter {
3234
} else {
3335
throw new Error(`file router dynamicPath duplicate`);
3436
}
37+
} else if (fileName.toLowerCase() === "index") {
38+
// 默认路由, 同一级只应该有一个
39+
if (indexPath === 0) {
40+
indexPath++;
41+
config.path = `${prePath}`;
42+
} else {
43+
throw new Error("file router default path duplicate");
44+
}
45+
} else if (fileName === "404") {
46+
if (prePath === "/") {
47+
fallbackPath++;
48+
config.path = `${prePath}*`;
49+
} else {
50+
throw new Error(`can not add 404 page on the ${prePath}`);
51+
}
3552
} else {
3653
config.path = `${prePath}${fileName}`;
3754
}
@@ -54,6 +71,12 @@ class DynamicRouter {
5471
// 如果存在动态路由 进行排序放在当前层级最后面
5572
routes.sort((_, t) => (/^\[(.*)\]$/.test(t.path) ? -1 : 0));
5673
}
74+
if (indexPath === 1) {
75+
routes.sort((_, t) => (t.path.endsWith("/") ? 1 : 0));
76+
}
77+
if (fallbackPath === 1) {
78+
routes.sort((_, b) => (b.path === "/*" ? -1 : 0));
79+
}
5780
})
5881
.then(() => resolve(routes))
5982
.catch((e) => console.log(chalk.red(`file router error, ${e.toString()}`)));

script/start-prod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ const withPromise = async () => {
2323
} catch (e) {
2424
console.error(e.message);
2525
}
26-
console.log("compiler done. before start this app, pls set PUBLIC_API_HOST in the env file!");
2726
};
2827

2928
withPromise();

src/client/antDesignEntry.tsx

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/client/chakraEntry.tsx

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,30 @@
1-
import { ColorModeScript, ChakraProvider, cookieStorageManager } from "@chakra-ui/react";
2-
import { CacheProvider } from "@emotion/react";
1+
import { ChakraProvider, createCookieStorageManager } from "@chakra-ui/react";
32
import { StrictMode } from "react";
43
import { HelmetProvider } from "react-helmet-async";
54
import { Provider } from "react-redux";
65
import { BrowserRouter as Router } from "react-router-dom";
76

87
import { App } from "components/App";
9-
import { createEmotionCache } from "config/createEmotionCache";
108
import { theme } from "theme";
119

1210
import type { createUniversalStore } from "store";
1311

14-
const cache = createEmotionCache();
15-
1612
const Root = ({ store }: { store: ReturnType<typeof createUniversalStore> }) => {
1713
console.warn("you are using chakra UI component library!");
1814

19-
const cookieStore = cookieStorageManager(store.getState().server.cookie.data);
15+
const cookieStore = createCookieStorageManager("chakra-ui-color-mode", store.getState().server.cookie.data);
2016

2117
return (
2218
<StrictMode>
23-
<CacheProvider value={cache}>
24-
<ChakraProvider resetCSS theme={theme} colorModeManager={cookieStore}>
25-
<Provider store={store}>
26-
<Router>
27-
<HelmetProvider>
28-
<ColorModeScript initialColorMode={cookieStore.get(theme.config.initialColorMode)} />
29-
<App />
30-
</HelmetProvider>
31-
</Router>
32-
</Provider>
33-
</ChakraProvider>
34-
</CacheProvider>
19+
<ChakraProvider resetCSS theme={theme} colorModeManager={cookieStore}>
20+
<Provider store={store}>
21+
<Router>
22+
<HelmetProvider>
23+
<App />
24+
</HelmetProvider>
25+
</Router>
26+
</Provider>
27+
</ChakraProvider>
3528
</StrictMode>
3629
);
3730
};

0 commit comments

Comments
 (0)