@umijs/server 4.0.0-rc.12 → 4.0.0-rc.15

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 CHANGED
@@ -1 +1,2 @@
1
+ export { createServerRoutes } from './routes';
1
2
  export * from './server';
package/dist/index.js CHANGED
@@ -14,4 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
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; } });
17
20
  __exportStar(require("./server"), exports);
package/dist/server.d.ts CHANGED
@@ -11,6 +11,7 @@ export interface IOpts {
11
11
  metas?: Record<string, string>[];
12
12
  styles?: (Record<string, string> | string)[];
13
13
  favicon?: string;
14
+ title?: string;
14
15
  headScripts?: (Record<string, string> | string)[];
15
16
  scripts?: (Record<string, string> | string)[];
16
17
  mountElementId?: string;
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
  };
@@ -19,50 +10,50 @@ const server_1 = __importDefault(require("react-dom/server"));
19
10
  // import { createServerRoutes } from './routes';
20
11
  const scripts_1 = require("./scripts");
21
12
  const styles_1 = require("./styles");
22
- function getMarkup(opts) {
23
- return __awaiter(this, void 0, void 0, function* () {
24
- // TODO: use real component
25
- let markup = server_1.default.renderToString(react_1.default.createElement('div', { id: opts.mountElementId || 'root' }));
26
- function propsToString(opts) {
27
- return Object.keys(opts.props)
28
- .filter((key) => !(opts.filters || []).includes(key))
29
- .map((key) => `${key}=${JSON.stringify(opts.props[key])}`)
30
- .join(' ');
31
- }
32
- function getScriptContent(script) {
33
- const attrs = propsToString({
34
- props: script,
35
- filters: ['src', 'content'],
36
- });
37
- return script.src
38
- ? `<script${opts.esmScript ? ' type="module"' : ''} ${attrs} src="${script.src}"></script>`
39
- : `<script${opts.esmScript ? ' type="module"' : ''} ${attrs}>${script.content}</script>`;
40
- }
41
- function getStyleContent(style) {
42
- const attrs = propsToString({
43
- props: style,
44
- filters: ['src', 'content'],
45
- });
46
- return style.src
47
- ? `<link rel="stylesheet" ${attrs} href="${style.src}" />`
48
- : `<style ${attrs}>${style.content}</style>`;
49
- }
50
- function getTagContent(opts) {
51
- const attrs = propsToString({
52
- props: opts.attrs,
53
- });
54
- return `<${opts.tagName} ${attrs} />`;
55
- }
56
- const favicon = opts.favicon
57
- ? `<link rel="shortcut icon" href="${opts.favicon}">`
58
- : '';
59
- const metas = (opts.metas || []).map((meta) => getTagContent({ attrs: meta, tagName: 'meta' }));
60
- const links = (opts.links || []).map((link) => getTagContent({ attrs: link, tagName: 'link' }));
61
- const styles = (0, styles_1.normalizeStyles)(opts.styles || []).map(getStyleContent);
62
- const headScripts = (0, scripts_1.normalizeScripts)(opts.headScripts || []).map(getScriptContent);
63
- const scripts = (0, scripts_1.normalizeScripts)(opts.scripts || []).map(getScriptContent);
64
- markup = [
65
- `<!DOCTYPE html>
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,29 +62,29 @@ 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
- metas.join('\n'),
75
- favicon,
76
- links.join('\n'),
77
- styles.join('\n'),
78
- headScripts.join('\n'),
79
- `</head>
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
- markup,
82
- scripts.join('\n'),
83
- `</body>
73
+ markup,
74
+ scripts.join('\n'),
75
+ `</body>
84
76
  </html>`,
85
- ]
86
- .filter(Boolean)
87
- .join('\n');
88
- if (opts.modifyHTML) {
89
- markup = yield opts.modifyHTML(markup, { path: opts.path });
90
- }
91
- return markup;
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) => __awaiter(this, void 0, void 0, function* () {
87
+ return async (req, res, next) => {
97
88
  var _a;
98
89
  if (opts.historyType === 'browser' &&
99
90
  opts.base !== '/' &&
@@ -109,12 +100,12 @@ function createRequestHandler(opts) {
109
100
  // const matches = matchRoutes(routes, req.path, opts.base);
110
101
  // 其他接受 HTML 的请求都兜底返回 HTML
111
102
  res.set('Content-Type', 'text/html');
112
- const markup = yield getMarkup(Object.assign(Object.assign({}, opts), { path: req.path }));
103
+ const markup = await getMarkup({ ...opts, path: req.path });
113
104
  res.end(markup);
114
105
  }
115
106
  else {
116
107
  next();
117
108
  }
118
- });
109
+ };
119
110
  }
120
111
  exports.createRequestHandler = createRequestHandler;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umijs/server",
3
- "version": "4.0.0-rc.12",
3
+ "version": "4.0.0-rc.15",
4
4
  "description": "@umijs/server",
5
5
  "homepage": "https://github.com/umijs/umi-next/tree/master/packages/server#readme",
6
6
  "bugs": "https://github.com/umijs/umi-next/issues",
@@ -16,14 +16,14 @@
16
16
  ],
17
17
  "scripts": {
18
18
  "build": "pnpm tsc",
19
- "build:deps": "pnpm esno ../../scripts/bundleDeps.ts",
19
+ "build:deps": "umi-scripts bundleDeps",
20
20
  "dev": "pnpm build -- --watch"
21
21
  },
22
22
  "dependencies": {
23
- "@umijs/bundler-utils": "4.0.0-rc.12",
23
+ "@umijs/bundler-utils": "4.0.0-rc.15",
24
24
  "history": "5.3.0",
25
- "react": "17.0.2",
26
- "react-dom": "17.0.2",
25
+ "react": "18.1.0",
26
+ "react-dom": "18.1.0",
27
27
  "react-router-dom": "6.3.0"
28
28
  },
29
29
  "publishConfig": {