@umijs/server 4.0.6 → 4.0.9
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/dist/index.js +30 -19
- package/dist/routes.js +49 -27
- package/dist/scripts.js +46 -28
- package/dist/server.js +138 -107
- package/dist/ssr.js +99 -77
- package/dist/styles.js +46 -28
- package/dist/types.js +17 -2
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -1,20 +1,31 @@
|
|
|
1
|
-
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
15
8
|
};
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
createServerRoutes: () => import_routes.createServerRoutes
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(src_exports);
|
|
26
|
+
var import_routes = require("./routes");
|
|
27
|
+
__reExport(src_exports, require("./server"), module.exports);
|
|
28
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
29
|
+
0 && (module.exports = {
|
|
30
|
+
createServerRoutes
|
|
31
|
+
});
|
package/dist/routes.js
CHANGED
|
@@ -1,32 +1,54 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// routes.ts
|
|
20
|
+
var routes_exports = {};
|
|
21
|
+
__export(routes_exports, {
|
|
22
|
+
createServerRoute: () => createServerRoute,
|
|
23
|
+
createServerRoutes: () => createServerRoutes
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(routes_exports);
|
|
4
26
|
function createServerRoutes(opts) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const route = createServerRoute({
|
|
10
|
-
route: routesById[id],
|
|
11
|
-
});
|
|
12
|
-
const children = createServerRoutes({
|
|
13
|
-
routesById,
|
|
14
|
-
parentId: route.id,
|
|
15
|
-
});
|
|
16
|
-
if (children.length > 0) {
|
|
17
|
-
// @ts-ignore
|
|
18
|
-
route.children = children;
|
|
19
|
-
}
|
|
20
|
-
return route;
|
|
27
|
+
const { routesById, parentId } = opts;
|
|
28
|
+
return Object.keys(routesById).filter((id) => routesById[id].parentId === parentId).map((id) => {
|
|
29
|
+
const route = createServerRoute({
|
|
30
|
+
route: routesById[id]
|
|
21
31
|
});
|
|
32
|
+
const children = createServerRoutes({
|
|
33
|
+
routesById,
|
|
34
|
+
parentId: route.id
|
|
35
|
+
});
|
|
36
|
+
if (children.length > 0) {
|
|
37
|
+
route.children = children;
|
|
38
|
+
}
|
|
39
|
+
return route;
|
|
40
|
+
});
|
|
22
41
|
}
|
|
23
|
-
exports.createServerRoutes = createServerRoutes;
|
|
24
42
|
function createServerRoute(opts) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
43
|
+
const { route } = opts;
|
|
44
|
+
return {
|
|
45
|
+
id: route.id,
|
|
46
|
+
path: route.path,
|
|
47
|
+
index: route.index
|
|
48
|
+
};
|
|
31
49
|
}
|
|
32
|
-
|
|
50
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
51
|
+
0 && (module.exports = {
|
|
52
|
+
createServerRoute,
|
|
53
|
+
createServerRoutes
|
|
54
|
+
});
|
package/dist/scripts.js
CHANGED
|
@@ -1,33 +1,51 @@
|
|
|
1
|
-
|
|
2
|
-
var
|
|
3
|
-
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
4
10
|
};
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
|
|
20
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
21
|
+
|
|
22
|
+
// scripts.ts
|
|
23
|
+
var scripts_exports = {};
|
|
24
|
+
__export(scripts_exports, {
|
|
25
|
+
normalizeScript: () => normalizeScript,
|
|
26
|
+
normalizeScripts: () => normalizeScripts
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(scripts_exports);
|
|
29
|
+
var import_assert = __toESM(require("assert"));
|
|
8
30
|
function normalizeScripts(scripts) {
|
|
9
|
-
|
|
31
|
+
return scripts.map(normalizeScript);
|
|
10
32
|
}
|
|
11
|
-
|
|
12
|
-
const RE_URL = /^(http:|https:)?\/\//;
|
|
33
|
+
var RE_URL = /^(http:|https:)?\/\//;
|
|
13
34
|
function normalizeScript(script) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
else if (typeof script === 'object') {
|
|
26
|
-
(0, assert_1.default)(typeof script.src === 'string' || typeof script.content === 'string', `Script must have either a "src" or a "content" property.`);
|
|
27
|
-
return script;
|
|
28
|
-
}
|
|
29
|
-
else {
|
|
30
|
-
throw new Error(`Invalid script type: ${typeof script}`);
|
|
31
|
-
}
|
|
35
|
+
if (typeof script === "string") {
|
|
36
|
+
const isUrl = RE_URL.test(script) || script.startsWith("/") && !script.startsWith("/*") || script.startsWith("./") || script.startsWith("../");
|
|
37
|
+
return isUrl ? {
|
|
38
|
+
src: script
|
|
39
|
+
} : { content: script };
|
|
40
|
+
} else if (typeof script === "object") {
|
|
41
|
+
(0, import_assert.default)(typeof script.src === "string" || typeof script.content === "string", `Script must have either a "src" or a "content" property.`);
|
|
42
|
+
return script;
|
|
43
|
+
} else {
|
|
44
|
+
throw new Error(`Invalid script type: ${typeof script}`);
|
|
45
|
+
}
|
|
32
46
|
}
|
|
33
|
-
|
|
47
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
48
|
+
0 && (module.exports = {
|
|
49
|
+
normalizeScript,
|
|
50
|
+
normalizeScripts
|
|
51
|
+
});
|
package/dist/server.js
CHANGED
|
@@ -1,114 +1,145 @@
|
|
|
1
|
-
|
|
2
|
-
var
|
|
3
|
-
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __defProps = Object.defineProperties;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
8
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
9
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
11
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
12
|
+
var __spreadValues = (a, b) => {
|
|
13
|
+
for (var prop in b || (b = {}))
|
|
14
|
+
if (__hasOwnProp.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
if (__getOwnPropSymbols)
|
|
17
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
18
|
+
if (__propIsEnum.call(b, prop))
|
|
19
|
+
__defNormalProp(a, prop, b[prop]);
|
|
20
|
+
}
|
|
21
|
+
return a;
|
|
22
|
+
};
|
|
23
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
24
|
+
var __export = (target, all) => {
|
|
25
|
+
for (var name in all)
|
|
26
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
27
|
+
};
|
|
28
|
+
var __copyProps = (to, from, except, desc) => {
|
|
29
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
30
|
+
for (let key of __getOwnPropNames(from))
|
|
31
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
32
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
33
|
+
}
|
|
34
|
+
return to;
|
|
4
35
|
};
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
36
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
|
|
37
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
38
|
+
|
|
39
|
+
// server.ts
|
|
40
|
+
var server_exports = {};
|
|
41
|
+
__export(server_exports, {
|
|
42
|
+
createRequestHandler: () => createRequestHandler,
|
|
43
|
+
getMarkup: () => getMarkup
|
|
44
|
+
});
|
|
45
|
+
module.exports = __toCommonJS(server_exports);
|
|
46
|
+
var import_react = __toESM(require("react"));
|
|
47
|
+
var import_server = __toESM(require("react-dom/server"));
|
|
48
|
+
var import_scripts = require("./scripts");
|
|
49
|
+
var import_styles = require("./styles");
|
|
13
50
|
async function getMarkup(opts) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
51
|
+
let markup = import_server.default.renderToString(import_react.default.createElement("div", { id: opts.mountElementId || "root" }));
|
|
52
|
+
function propsToString(opts2) {
|
|
53
|
+
return Object.keys(opts2.props).filter((key) => !(opts2.filters || []).includes(key)).map((key) => `${key}=${JSON.stringify(opts2.props[key])}`).join(" ");
|
|
54
|
+
}
|
|
55
|
+
function getScriptContent(script) {
|
|
56
|
+
const attrs = propsToString({
|
|
57
|
+
props: script,
|
|
58
|
+
filters: ["src", "content"]
|
|
59
|
+
});
|
|
60
|
+
return script.src ? `<script${opts.esmScript ? ' type="module"' : ""} ${attrs} src="${script.src}"><\/script>` : `<script${opts.esmScript ? ' type="module"' : ""} ${attrs}>${script.content}<\/script>`;
|
|
61
|
+
}
|
|
62
|
+
function getStyleContent(style) {
|
|
63
|
+
const attrs = propsToString({
|
|
64
|
+
props: style,
|
|
65
|
+
filters: ["src", "content"]
|
|
66
|
+
});
|
|
67
|
+
return style.src ? `<link rel="stylesheet" ${attrs} href="${style.src}" />` : `<style ${attrs}>${style.content}</style>`;
|
|
68
|
+
}
|
|
69
|
+
function getTagContent(opts2) {
|
|
70
|
+
const attrs = propsToString({
|
|
71
|
+
props: opts2.attrs
|
|
72
|
+
});
|
|
73
|
+
return `<${opts2.tagName} ${attrs} />`;
|
|
74
|
+
}
|
|
75
|
+
function withDefaultMetas(metas2 = []) {
|
|
76
|
+
const hasAttr = (key, value) => metas2.some((m) => {
|
|
77
|
+
var _a;
|
|
78
|
+
return value ? ((_a = m[key]) == null ? void 0 : _a.toLowerCase()) === value.toLowerCase() : m[key];
|
|
79
|
+
});
|
|
80
|
+
return [
|
|
81
|
+
!hasAttr("charset") && { charset: "utf-8" },
|
|
82
|
+
!hasAttr("name", "viewport") && {
|
|
83
|
+
name: "viewport",
|
|
84
|
+
content: "width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
|
|
85
|
+
},
|
|
86
|
+
!hasAttr("http-equiv", "X-UA-Compatible") && {
|
|
87
|
+
"http-equiv": "X-UA-Compatible",
|
|
88
|
+
content: "ie=edge"
|
|
89
|
+
},
|
|
90
|
+
...metas2
|
|
91
|
+
].filter(Boolean);
|
|
92
|
+
}
|
|
93
|
+
const favicons = [];
|
|
94
|
+
if (Array.isArray(opts.favicons)) {
|
|
95
|
+
opts.favicons.forEach((e) => {
|
|
96
|
+
favicons.push(`<link rel="shortcut icon" href="${e}">`);
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
const title = opts.title ? `<title>${opts.title}</title>` : "";
|
|
100
|
+
const metas = withDefaultMetas(opts.metas).map((meta) => getTagContent({ attrs: meta, tagName: "meta" }));
|
|
101
|
+
const links = (opts.links || []).map((link) => getTagContent({ attrs: link, tagName: "link" }));
|
|
102
|
+
const styles = (0, import_styles.normalizeStyles)(opts.styles || []).map(getStyleContent);
|
|
103
|
+
const headScripts = (0, import_scripts.normalizeScripts)(opts.headScripts || []).map(getScriptContent);
|
|
104
|
+
const scripts = (0, import_scripts.normalizeScripts)(opts.scripts || []).map(getScriptContent);
|
|
105
|
+
markup = [
|
|
106
|
+
`<!DOCTYPE html>
|
|
60
107
|
<html>
|
|
61
|
-
<head
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
favicons.join('\n'),
|
|
70
|
-
title,
|
|
71
|
-
links.join('\n'),
|
|
72
|
-
styles.join('\n'),
|
|
73
|
-
headScripts.join('\n'),
|
|
74
|
-
`</head>
|
|
108
|
+
<head>`,
|
|
109
|
+
metas.join("\n"),
|
|
110
|
+
favicons.join("\n"),
|
|
111
|
+
title,
|
|
112
|
+
links.join("\n"),
|
|
113
|
+
styles.join("\n"),
|
|
114
|
+
headScripts.join("\n"),
|
|
115
|
+
`</head>
|
|
75
116
|
<body>`,
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
</html
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
}
|
|
86
|
-
return markup;
|
|
117
|
+
markup,
|
|
118
|
+
scripts.join("\n"),
|
|
119
|
+
`</body>
|
|
120
|
+
</html>`
|
|
121
|
+
].filter(Boolean).join("\n");
|
|
122
|
+
if (opts.modifyHTML) {
|
|
123
|
+
markup = await opts.modifyHTML(markup, { path: opts.path });
|
|
124
|
+
}
|
|
125
|
+
return markup;
|
|
87
126
|
}
|
|
88
|
-
exports.getMarkup = getMarkup;
|
|
89
127
|
function createRequestHandler(opts) {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
// });
|
|
103
|
-
// const matches = matchRoutes(routes, req.path, opts.base);
|
|
104
|
-
// 其他接受 HTML 的请求都兜底返回 HTML
|
|
105
|
-
res.set('Content-Type', 'text/html');
|
|
106
|
-
const markup = await getMarkup({ ...opts, path: req.path });
|
|
107
|
-
res.end(markup);
|
|
108
|
-
}
|
|
109
|
-
else {
|
|
110
|
-
next();
|
|
111
|
-
}
|
|
112
|
-
};
|
|
128
|
+
return async (req, res, next) => {
|
|
129
|
+
var _a;
|
|
130
|
+
if (opts.historyType === "browser" && opts.base !== "/" && req.path === "/") {
|
|
131
|
+
res.redirect(opts.base);
|
|
132
|
+
} else if (((_a = req.headers.accept) == null ? void 0 : _a.includes("text/html")) || req.headers.accept === "*/*" || req.path === opts.base) {
|
|
133
|
+
res.set("Content-Type", "text/html");
|
|
134
|
+
const markup = await getMarkup(__spreadProps(__spreadValues({}, opts), { path: req.path }));
|
|
135
|
+
res.end(markup);
|
|
136
|
+
} else {
|
|
137
|
+
next();
|
|
138
|
+
}
|
|
139
|
+
};
|
|
113
140
|
}
|
|
114
|
-
|
|
141
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
142
|
+
0 && (module.exports = {
|
|
143
|
+
createRequestHandler,
|
|
144
|
+
getMarkup
|
|
145
|
+
});
|
package/dist/ssr.js
CHANGED
|
@@ -1,89 +1,111 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// ssr.ts
|
|
20
|
+
var ssr_exports = {};
|
|
21
|
+
__export(ssr_exports, {
|
|
22
|
+
default: () => createRequestHandler
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(ssr_exports);
|
|
25
|
+
var import_server = require("react-dom/server");
|
|
26
|
+
var import_react_router_dom = require("react-router-dom");
|
|
5
27
|
function createRequestHandler(opts) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
bootstrapScripts: [manifest.assets['umi.js'] || '/umi.js'],
|
|
42
|
-
onShellReady() {
|
|
43
|
-
res.setHeader('Content-type', 'text/html');
|
|
44
|
-
stream.pipe(res);
|
|
45
|
-
},
|
|
46
|
-
onError(x) {
|
|
47
|
-
console.error(x);
|
|
48
|
-
},
|
|
49
|
-
});
|
|
28
|
+
return async function(req, res, next) {
|
|
29
|
+
const {
|
|
30
|
+
routesWithServerLoader,
|
|
31
|
+
PluginManager,
|
|
32
|
+
getPlugins,
|
|
33
|
+
getValidKeys,
|
|
34
|
+
getRoutes
|
|
35
|
+
} = opts;
|
|
36
|
+
if (req.url.startsWith("/__serverLoader") && req.query.route) {
|
|
37
|
+
const data = await executeLoader(req.query.route, routesWithServerLoader);
|
|
38
|
+
res.status(200).json(data);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const pluginManager = PluginManager.create({
|
|
42
|
+
plugins: getPlugins(),
|
|
43
|
+
validKeys: getValidKeys()
|
|
44
|
+
});
|
|
45
|
+
const { routes, routeComponents } = await getRoutes(pluginManager);
|
|
46
|
+
const matches = matchRoutesForSSR(req.url, routes);
|
|
47
|
+
if (matches.length === 0) {
|
|
48
|
+
return next();
|
|
49
|
+
}
|
|
50
|
+
const loaderData = {};
|
|
51
|
+
await Promise.all(matches.filter((id) => routes[id].hasServerLoader).map((id) => new Promise(async (resolve) => {
|
|
52
|
+
loaderData[id] = await executeLoader(id, routesWithServerLoader);
|
|
53
|
+
resolve();
|
|
54
|
+
})));
|
|
55
|
+
const manifest = typeof opts.manifest === "function" ? opts.manifest() : opts.manifest;
|
|
56
|
+
const context = {
|
|
57
|
+
routes,
|
|
58
|
+
routeComponents,
|
|
59
|
+
pluginManager,
|
|
60
|
+
location: req.url,
|
|
61
|
+
manifest,
|
|
62
|
+
loaderData
|
|
50
63
|
};
|
|
64
|
+
const jsx = await opts.getClientRootComponent(context);
|
|
65
|
+
const stream = (0, import_server.renderToPipeableStream)(jsx, {
|
|
66
|
+
bootstrapScripts: [manifest.assets["umi.js"] || "/umi.js"],
|
|
67
|
+
onShellReady() {
|
|
68
|
+
res.setHeader("Content-type", "text/html");
|
|
69
|
+
stream.pipe(res);
|
|
70
|
+
},
|
|
71
|
+
onError(x) {
|
|
72
|
+
console.error(x);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
};
|
|
51
76
|
}
|
|
52
|
-
exports.default = createRequestHandler;
|
|
53
77
|
function matchRoutesForSSR(reqUrl, routesById) {
|
|
54
|
-
|
|
55
|
-
|
|
78
|
+
var _a;
|
|
79
|
+
return ((_a = (0, import_react_router_dom.matchRoutes)(createClientRoutes({ routesById }), reqUrl)) == null ? void 0 : _a.map((route) => route.route.id)) || [];
|
|
56
80
|
}
|
|
57
81
|
function createClientRoutes(opts) {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
routesById,
|
|
65
|
-
parentId: route.id,
|
|
66
|
-
});
|
|
67
|
-
if (children.length > 0) {
|
|
68
|
-
// @ts-ignore
|
|
69
|
-
route.children = children;
|
|
70
|
-
}
|
|
71
|
-
return route;
|
|
82
|
+
const { routesById, parentId } = opts;
|
|
83
|
+
return Object.keys(routesById).filter((id) => routesById[id].parentId === parentId).map((id) => {
|
|
84
|
+
const route = createClientRoute(routesById[id]);
|
|
85
|
+
const children = createClientRoutes({
|
|
86
|
+
routesById,
|
|
87
|
+
parentId: route.id
|
|
72
88
|
});
|
|
89
|
+
if (children.length > 0) {
|
|
90
|
+
route.children = children;
|
|
91
|
+
}
|
|
92
|
+
return route;
|
|
93
|
+
});
|
|
73
94
|
}
|
|
74
95
|
function createClientRoute(route) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
96
|
+
const { id, path, index } = route;
|
|
97
|
+
return {
|
|
98
|
+
id,
|
|
99
|
+
path,
|
|
100
|
+
index
|
|
101
|
+
};
|
|
81
102
|
}
|
|
82
103
|
async function executeLoader(routeKey, routesWithServerLoader) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
return await mod.serverLoader();
|
|
104
|
+
const mod = await routesWithServerLoader[routeKey]();
|
|
105
|
+
if (!mod.serverLoader || typeof mod.serverLoader !== "function") {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
return await mod.serverLoader();
|
|
89
109
|
}
|
|
110
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
111
|
+
0 && (module.exports = {});
|
package/dist/styles.js
CHANGED
|
@@ -1,33 +1,51 @@
|
|
|
1
|
-
|
|
2
|
-
var
|
|
3
|
-
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
4
10
|
};
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
|
|
20
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
21
|
+
|
|
22
|
+
// styles.ts
|
|
23
|
+
var styles_exports = {};
|
|
24
|
+
__export(styles_exports, {
|
|
25
|
+
normalizeStyle: () => normalizeStyle,
|
|
26
|
+
normalizeStyles: () => normalizeStyles
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(styles_exports);
|
|
29
|
+
var import_assert = __toESM(require("assert"));
|
|
8
30
|
function normalizeStyles(scripts) {
|
|
9
|
-
|
|
31
|
+
return scripts.map(normalizeStyle);
|
|
10
32
|
}
|
|
11
|
-
|
|
12
|
-
const RE_URL = /^(http:|https:)?\/\//;
|
|
33
|
+
var RE_URL = /^(http:|https:)?\/\//;
|
|
13
34
|
function normalizeStyle(style) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
else if (typeof style === 'object') {
|
|
26
|
-
(0, assert_1.default)(typeof style.src === 'string' || typeof style.content === 'string', `Style must have either a "src" or a "content" property.`);
|
|
27
|
-
return style;
|
|
28
|
-
}
|
|
29
|
-
else {
|
|
30
|
-
throw new Error(`Invalid style type: ${typeof style}`);
|
|
31
|
-
}
|
|
35
|
+
if (typeof style === "string") {
|
|
36
|
+
const isUrl = RE_URL.test(style) || style.startsWith("/") && !style.startsWith("/*") || style.startsWith("./") || style.startsWith("../");
|
|
37
|
+
return isUrl ? {
|
|
38
|
+
src: style
|
|
39
|
+
} : { content: style };
|
|
40
|
+
} else if (typeof style === "object") {
|
|
41
|
+
(0, import_assert.default)(typeof style.src === "string" || typeof style.content === "string", `Style must have either a "src" or a "content" property.`);
|
|
42
|
+
return style;
|
|
43
|
+
} else {
|
|
44
|
+
throw new Error(`Invalid style type: ${typeof style}`);
|
|
45
|
+
}
|
|
32
46
|
}
|
|
33
|
-
|
|
47
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
48
|
+
0 && (module.exports = {
|
|
49
|
+
normalizeStyle,
|
|
50
|
+
normalizeStyles
|
|
51
|
+
});
|
package/dist/types.js
CHANGED
|
@@ -1,2 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __copyProps = (to, from, except, desc) => {
|
|
6
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
7
|
+
for (let key of __getOwnPropNames(from))
|
|
8
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
9
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
10
|
+
}
|
|
11
|
+
return to;
|
|
12
|
+
};
|
|
13
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
14
|
+
|
|
15
|
+
// types.ts
|
|
16
|
+
var types_exports = {};
|
|
17
|
+
module.exports = __toCommonJS(types_exports);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umijs/server",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.9",
|
|
4
4
|
"description": "@umijs/server",
|
|
5
5
|
"homepage": "https://github.com/umijs/umi/tree/master/packages/server#readme",
|
|
6
6
|
"bugs": "https://github.com/umijs/umi/issues",
|
|
@@ -15,12 +15,12 @@
|
|
|
15
15
|
"dist"
|
|
16
16
|
],
|
|
17
17
|
"scripts": {
|
|
18
|
-
"build": "
|
|
18
|
+
"build": "umi-scripts father build",
|
|
19
19
|
"build:deps": "umi-scripts bundleDeps",
|
|
20
|
-
"dev": "
|
|
20
|
+
"dev": "umi-scripts father dev"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@umijs/bundler-utils": "4.0.
|
|
23
|
+
"@umijs/bundler-utils": "4.0.9",
|
|
24
24
|
"history": "5.3.0",
|
|
25
25
|
"react": "18.1.0",
|
|
26
26
|
"react-dom": "18.1.0",
|