@appigram/react-code-split-ssr 1.2.0-beta.6 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/generate-routes.js +8 -12
- package/package.json +4 -4
- package/src/generate-routes.tsx +26 -31
package/lib/generate-routes.js
CHANGED
|
@@ -11,7 +11,7 @@ import { jsx as _jsx } from "react/jsx-runtime";
|
|
|
11
11
|
import { matchPath, Route, Routes } from "react-router-dom";
|
|
12
12
|
const generateRoutes = (options = {
|
|
13
13
|
pathname: "/",
|
|
14
|
-
routes: []
|
|
14
|
+
routes: [],
|
|
15
15
|
}) => __awaiter(void 0, void 0, void 0, function* () {
|
|
16
16
|
if (!Array.isArray(options.routes) || options.routes.length === 0) {
|
|
17
17
|
throw new Error("options.routes must be an non-empty array");
|
|
@@ -20,19 +20,15 @@ const generateRoutes = (options = {
|
|
|
20
20
|
const preloadedComp = preload === undefined
|
|
21
21
|
? yield options.notFoundComp().props.mod
|
|
22
22
|
: yield preload.element().props.mod;
|
|
23
|
-
const
|
|
23
|
+
const renderElement = (path, bundle) => {
|
|
24
24
|
if (!preloadedComp)
|
|
25
25
|
return bundle;
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
return () => {
|
|
30
|
-
const routes = options.routes.map((props, i) => {
|
|
31
|
-
const element = renderComp(props.path, props.element);
|
|
32
|
-
return (_jsx(Route, Object.assign({}, props, { element: element }), i));
|
|
33
|
-
});
|
|
34
|
-
routes.push(_jsx(Route, { element: renderComp(null, options.notFoundComp) }, void 0));
|
|
35
|
-
return (_jsx(Routes, { children: routes }, void 0));
|
|
26
|
+
const isRouteMatched = (preload && preload.path === path) || (!preload && !path);
|
|
27
|
+
const Element = isRouteMatched ? preloadedComp.default : bundle;
|
|
28
|
+
return isRouteMatched ? _jsx(Element, {}, void 0) : Element;
|
|
36
29
|
};
|
|
30
|
+
return (_jsx(Routes, { children: options.routes.map((props, i) => {
|
|
31
|
+
return (_jsx(Route, { path: props.path, element: renderElement(props.path, props.element) }, i));
|
|
32
|
+
}) }, void 0));
|
|
37
33
|
});
|
|
38
34
|
export default generateRoutes;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@appigram/react-code-split-ssr",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.2",
|
|
4
4
|
"description": "React code splitting with SSR",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -22,15 +22,15 @@
|
|
|
22
22
|
"author": "Eugene Sysmanov",
|
|
23
23
|
"license": "MIT",
|
|
24
24
|
"devDependencies": {
|
|
25
|
-
"@types/react": "^17.0.
|
|
25
|
+
"@types/react": "^17.0.37",
|
|
26
26
|
"@types/react-router-dom": "^5.3.2",
|
|
27
27
|
"tslint": "^6.1.3",
|
|
28
28
|
"tslint-react": "^5.0.0",
|
|
29
|
-
"typescript": "^4.
|
|
29
|
+
"typescript": "^4.5.4"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"react": "^17.0.2",
|
|
33
33
|
"react-dom": "^17.0.2",
|
|
34
|
-
"react-router-dom": "^6.
|
|
34
|
+
"react-router-dom": "^6.1.1"
|
|
35
35
|
}
|
|
36
36
|
}
|
package/src/generate-routes.tsx
CHANGED
|
@@ -1,17 +1,16 @@
|
|
|
1
|
-
import { ReactElement
|
|
1
|
+
import { ReactElement } from "react";
|
|
2
2
|
import { matchPath, Route, Routes } from "react-router-dom";
|
|
3
|
-
// import Bundle from './bundle'
|
|
4
3
|
|
|
5
4
|
export interface IJSXModule {
|
|
6
|
-
default:
|
|
5
|
+
default: React.FC | React.ComponentClass;
|
|
7
6
|
}
|
|
8
7
|
|
|
9
8
|
export interface ISSRRoute {
|
|
10
9
|
caseSensitive?: boolean;
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
children?: React.ReactNode;
|
|
11
|
+
element?: any; // () => React.FunctionComponentElement<{ mod: Promise<IJSXModule> }>;
|
|
12
|
+
index?: boolean;
|
|
13
|
+
path?: string;
|
|
15
14
|
}
|
|
16
15
|
|
|
17
16
|
export interface IOptions {
|
|
@@ -23,16 +22,15 @@ export interface IOptions {
|
|
|
23
22
|
const generateRoutes = async (
|
|
24
23
|
options: IOptions = {
|
|
25
24
|
pathname: "/",
|
|
26
|
-
routes: []
|
|
25
|
+
routes: [],
|
|
27
26
|
}
|
|
28
|
-
)
|
|
27
|
+
) => {
|
|
29
28
|
if (!Array.isArray(options.routes) || options.routes.length === 0) {
|
|
30
29
|
throw new Error("options.routes must be an non-empty array");
|
|
31
30
|
}
|
|
32
31
|
|
|
33
32
|
const preload = options.routes.find(
|
|
34
|
-
(route) =>
|
|
35
|
-
!!matchPath(route.path, options.pathname)
|
|
33
|
+
(route) => !!matchPath(route.path, options.pathname)
|
|
36
34
|
);
|
|
37
35
|
|
|
38
36
|
const preloadedComp: any =
|
|
@@ -40,29 +38,26 @@ const generateRoutes = async (
|
|
|
40
38
|
? await options.notFoundComp().props.mod
|
|
41
39
|
: await preload.element().props.mod;
|
|
42
40
|
|
|
43
|
-
const
|
|
41
|
+
const renderElement = (path: string, bundle: ReactElement) => {
|
|
44
42
|
if (!preloadedComp) return bundle;
|
|
45
|
-
const
|
|
46
|
-
|
|
43
|
+
const isRouteMatched = (preload && preload.path === path) || (!preload && !path);
|
|
44
|
+
const Element = isRouteMatched ? preloadedComp.default : bundle;
|
|
45
|
+
return isRouteMatched ? <Element /> : Element
|
|
47
46
|
};
|
|
48
47
|
|
|
49
|
-
return (
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
{routes}
|
|
63
|
-
</Routes>
|
|
64
|
-
);
|
|
65
|
-
};
|
|
48
|
+
return (
|
|
49
|
+
<Routes>
|
|
50
|
+
{options.routes.map((props, i) => {
|
|
51
|
+
return (
|
|
52
|
+
<Route
|
|
53
|
+
key={i}
|
|
54
|
+
path={props.path}
|
|
55
|
+
element={renderElement(props.path, props.element)}
|
|
56
|
+
/>
|
|
57
|
+
);
|
|
58
|
+
})}
|
|
59
|
+
</Routes>
|
|
60
|
+
);
|
|
66
61
|
};
|
|
67
62
|
|
|
68
63
|
export default generateRoutes;
|