@umijs/server 4.0.0-beta.7 → 4.0.0-canary.202200505.1
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.d.ts +1 -0
- package/dist/index.js +8 -1
- package/dist/scripts.js +1 -1
- package/dist/server.d.ts +6 -3
- package/dist/server.js +80 -82
- package/dist/styles.js +1 -1
- package/package.json +18 -20
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
5
9
|
}) : (function(o, m, k, k2) {
|
|
6
10
|
if (k2 === undefined) k2 = k;
|
|
7
11
|
o[k2] = m[k];
|
|
@@ -10,4 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
10
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
15
|
};
|
|
12
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.createServerRoutes = void 0;
|
|
18
|
+
var routes_1 = require("./routes");
|
|
19
|
+
Object.defineProperty(exports, "createServerRoutes", { enumerable: true, get: function () { return routes_1.createServerRoutes; } });
|
|
13
20
|
__exportStar(require("./server"), exports);
|
package/dist/scripts.js
CHANGED
|
@@ -13,7 +13,7 @@ const RE_URL = /^(http:|https:)?\/\//;
|
|
|
13
13
|
function normalizeScript(script) {
|
|
14
14
|
if (typeof script === 'string') {
|
|
15
15
|
const isUrl = RE_URL.test(script) ||
|
|
16
|
-
script.startsWith('/') ||
|
|
16
|
+
(script.startsWith('/') && !script.startsWith('/*')) ||
|
|
17
17
|
script.startsWith('./') ||
|
|
18
18
|
script.startsWith('../');
|
|
19
19
|
return isUrl
|
package/dist/server.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import type { RequestHandler } from 'express';
|
|
2
|
-
interface IOpts {
|
|
1
|
+
import type { RequestHandler } from '@umijs/bundler-utils/compiled/express';
|
|
2
|
+
export interface IOpts {
|
|
3
|
+
base: string;
|
|
3
4
|
routes: Record<string, {
|
|
4
5
|
path: string;
|
|
5
6
|
file: string;
|
|
@@ -10,15 +11,17 @@ interface IOpts {
|
|
|
10
11
|
metas?: Record<string, string>[];
|
|
11
12
|
styles?: (Record<string, string> | string)[];
|
|
12
13
|
favicon?: string;
|
|
14
|
+
title?: string;
|
|
13
15
|
headScripts?: (Record<string, string> | string)[];
|
|
14
16
|
scripts?: (Record<string, string> | string)[];
|
|
17
|
+
mountElementId?: string;
|
|
15
18
|
esmScript?: boolean;
|
|
16
19
|
modifyHTML?: (html: string, args: {
|
|
17
20
|
path?: string;
|
|
18
21
|
}) => Promise<string>;
|
|
22
|
+
historyType?: 'hash' | 'browser';
|
|
19
23
|
}
|
|
20
24
|
export declare function getMarkup(opts: Omit<IOpts, 'routes'> & {
|
|
21
25
|
path?: string;
|
|
22
26
|
}): Promise<string>;
|
|
23
27
|
export declare function createRequestHandler(opts: IOpts): RequestHandler;
|
|
24
|
-
export {};
|
package/dist/server.js
CHANGED
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
@@ -15,54 +6,54 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
15
6
|
exports.createRequestHandler = exports.getMarkup = void 0;
|
|
16
7
|
const react_1 = __importDefault(require("react"));
|
|
17
8
|
const server_1 = __importDefault(require("react-dom/server"));
|
|
18
|
-
|
|
19
|
-
|
|
9
|
+
// import { matchRoutes } from 'react-router-dom';
|
|
10
|
+
// import { createServerRoutes } from './routes';
|
|
20
11
|
const scripts_1 = require("./scripts");
|
|
21
12
|
const styles_1 = require("./styles");
|
|
22
|
-
function getMarkup(opts) {
|
|
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
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
13
|
+
async function getMarkup(opts) {
|
|
14
|
+
// TODO: use real component
|
|
15
|
+
let markup = server_1.default.renderToString(react_1.default.createElement('div', { id: opts.mountElementId || 'root' }));
|
|
16
|
+
function propsToString(opts) {
|
|
17
|
+
return Object.keys(opts.props)
|
|
18
|
+
.filter((key) => !(opts.filters || []).includes(key))
|
|
19
|
+
.map((key) => `${key}=${JSON.stringify(opts.props[key])}`)
|
|
20
|
+
.join(' ');
|
|
21
|
+
}
|
|
22
|
+
function getScriptContent(script) {
|
|
23
|
+
const attrs = propsToString({
|
|
24
|
+
props: script,
|
|
25
|
+
filters: ['src', 'content'],
|
|
26
|
+
});
|
|
27
|
+
return script.src
|
|
28
|
+
? `<script${opts.esmScript ? ' type="module"' : ''} ${attrs} src="${script.src}"></script>`
|
|
29
|
+
: `<script${opts.esmScript ? ' type="module"' : ''} ${attrs}>${script.content}</script>`;
|
|
30
|
+
}
|
|
31
|
+
function getStyleContent(style) {
|
|
32
|
+
const attrs = propsToString({
|
|
33
|
+
props: style,
|
|
34
|
+
filters: ['src', 'content'],
|
|
35
|
+
});
|
|
36
|
+
return style.src
|
|
37
|
+
? `<link rel="stylesheet" ${attrs} href="${style.src}" />`
|
|
38
|
+
: `<style ${attrs}>${style.content}</style>`;
|
|
39
|
+
}
|
|
40
|
+
function getTagContent(opts) {
|
|
41
|
+
const attrs = propsToString({
|
|
42
|
+
props: opts.attrs,
|
|
43
|
+
});
|
|
44
|
+
return `<${opts.tagName} ${attrs} />`;
|
|
45
|
+
}
|
|
46
|
+
const favicon = opts.favicon
|
|
47
|
+
? `<link rel="shortcut icon" href="${opts.favicon}">`
|
|
48
|
+
: '';
|
|
49
|
+
const title = opts.title ? `<title>${opts.title}</title>` : '';
|
|
50
|
+
const metas = (opts.metas || []).map((meta) => getTagContent({ attrs: meta, tagName: 'meta' }));
|
|
51
|
+
const links = (opts.links || []).map((link) => getTagContent({ attrs: link, tagName: 'link' }));
|
|
52
|
+
const styles = (0, styles_1.normalizeStyles)(opts.styles || []).map(getStyleContent);
|
|
53
|
+
const headScripts = (0, scripts_1.normalizeScripts)(opts.headScripts || []).map(getScriptContent);
|
|
54
|
+
const scripts = (0, scripts_1.normalizeScripts)(opts.scripts || []).map(getScriptContent);
|
|
55
|
+
markup = [
|
|
56
|
+
`<!DOCTYPE html>
|
|
66
57
|
<html>
|
|
67
58
|
<head>
|
|
68
59
|
<meta charset="UTF-8" />
|
|
@@ -71,43 +62,50 @@ function getMarkup(opts) {
|
|
|
71
62
|
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
|
|
72
63
|
/>
|
|
73
64
|
<meta http-equiv="X-UA-Compatible" content="ie=edge" />`,
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
65
|
+
metas.join('\n'),
|
|
66
|
+
favicon,
|
|
67
|
+
title,
|
|
68
|
+
links.join('\n'),
|
|
69
|
+
styles.join('\n'),
|
|
70
|
+
headScripts.join('\n'),
|
|
71
|
+
`</head>
|
|
80
72
|
<body>`,
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
73
|
+
markup,
|
|
74
|
+
scripts.join('\n'),
|
|
75
|
+
`</body>
|
|
84
76
|
</html>`,
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
});
|
|
77
|
+
]
|
|
78
|
+
.filter(Boolean)
|
|
79
|
+
.join('\n');
|
|
80
|
+
if (opts.modifyHTML) {
|
|
81
|
+
markup = await opts.modifyHTML(markup, { path: opts.path });
|
|
82
|
+
}
|
|
83
|
+
return markup;
|
|
93
84
|
}
|
|
94
85
|
exports.getMarkup = getMarkup;
|
|
95
86
|
function createRequestHandler(opts) {
|
|
96
|
-
return (req, res, next) =>
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
87
|
+
return async (req, res, next) => {
|
|
88
|
+
var _a;
|
|
89
|
+
if (opts.historyType === 'browser' &&
|
|
90
|
+
opts.base !== '/' &&
|
|
91
|
+
req.path === '/') {
|
|
92
|
+
// 如果是 browser,并且配置了非 / base,访问 / 时 redirect 到 base 路径
|
|
93
|
+
res.redirect(opts.base);
|
|
94
|
+
}
|
|
95
|
+
else if ((_a = req.headers.accept) === null || _a === void 0 ? void 0 : _a.includes('text/html')) {
|
|
96
|
+
// 匹配路由,不匹配走 next()
|
|
97
|
+
// const routes = createServerRoutes({
|
|
98
|
+
// routesById: opts.routes,
|
|
99
|
+
// });
|
|
100
|
+
// const matches = matchRoutes(routes, req.path, opts.base);
|
|
101
|
+
// 其他接受 HTML 的请求都兜底返回 HTML
|
|
104
102
|
res.set('Content-Type', 'text/html');
|
|
105
|
-
const markup =
|
|
103
|
+
const markup = await getMarkup({ ...opts, path: req.path });
|
|
106
104
|
res.end(markup);
|
|
107
105
|
}
|
|
108
106
|
else {
|
|
109
107
|
next();
|
|
110
108
|
}
|
|
111
|
-
}
|
|
109
|
+
};
|
|
112
110
|
}
|
|
113
111
|
exports.createRequestHandler = createRequestHandler;
|
package/dist/styles.js
CHANGED
|
@@ -13,7 +13,7 @@ const RE_URL = /^(http:|https:)?\/\//;
|
|
|
13
13
|
function normalizeStyle(style) {
|
|
14
14
|
if (typeof style === 'string') {
|
|
15
15
|
const isUrl = RE_URL.test(style) ||
|
|
16
|
-
style.startsWith('/') ||
|
|
16
|
+
(style.startsWith('/') && !style.startsWith('/*')) ||
|
|
17
17
|
style.startsWith('./') ||
|
|
18
18
|
style.startsWith('../');
|
|
19
19
|
return isUrl
|
package/package.json
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umijs/server",
|
|
3
|
-
"version": "4.0.0-
|
|
3
|
+
"version": "4.0.0-canary.202200505.1",
|
|
4
4
|
"description": "@umijs/server",
|
|
5
|
+
"homepage": "https://github.com/umijs/umi-next/tree/master/packages/server#readme",
|
|
6
|
+
"bugs": "https://github.com/umijs/umi-next/issues",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/umijs/umi-next"
|
|
10
|
+
},
|
|
11
|
+
"license": "MIT",
|
|
5
12
|
"main": "dist/index.js",
|
|
6
13
|
"types": "dist/index.d.ts",
|
|
7
14
|
"files": [
|
|
@@ -9,29 +16,20 @@
|
|
|
9
16
|
],
|
|
10
17
|
"scripts": {
|
|
11
18
|
"build": "pnpm tsc",
|
|
12
|
-
"build:deps": "
|
|
19
|
+
"build:deps": "umi-scripts bundleDeps",
|
|
13
20
|
"dev": "pnpm build -- --watch"
|
|
14
21
|
},
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
"
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@umijs/bundler-utils": "4.0.0-canary.202200505.1",
|
|
24
|
+
"history": "5.3.0",
|
|
25
|
+
"react": "18.1.0",
|
|
26
|
+
"react-dom": "18.1.0",
|
|
27
|
+
"react-router-dom": "6.3.0"
|
|
18
28
|
},
|
|
19
|
-
"authors": [
|
|
20
|
-
"chencheng <sorrycc@gmail.com> (https://github.com/sorrycc)"
|
|
21
|
-
],
|
|
22
|
-
"license": "MIT",
|
|
23
|
-
"bugs": "https://github.com/umijs/umi-next/issues",
|
|
24
|
-
"homepage": "https://github.com/umijs/umi-next/tree/master/packages/server#readme",
|
|
25
29
|
"publishConfig": {
|
|
26
30
|
"access": "public"
|
|
27
31
|
},
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
|
|
31
|
-
"react-dom": "18.0.0-alpha-4298ddbc5-20211023",
|
|
32
|
-
"react-router-dom": "6.0.0-beta.8"
|
|
33
|
-
},
|
|
34
|
-
"devDependencies": {
|
|
35
|
-
"@types/express": "4.17.13"
|
|
36
|
-
}
|
|
32
|
+
"authors": [
|
|
33
|
+
"chencheng <sorrycc@gmail.com> (https://github.com/sorrycc)"
|
|
34
|
+
]
|
|
37
35
|
}
|