@umijs/server 4.0.0-rc.14 → 4.0.0-rc.17

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.
Files changed (2) hide show
  1. package/dist/server.js +64 -75
  2. package/package.json +2 -2
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,51 +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 title = opts.title ? `<title>${opts.title}</title>` : '';
60
- const metas = (opts.metas || []).map((meta) => getTagContent({ attrs: meta, tagName: 'meta' }));
61
- const links = (opts.links || []).map((link) => getTagContent({ attrs: link, tagName: 'link' }));
62
- const styles = (0, styles_1.normalizeStyles)(opts.styles || []).map(getStyleContent);
63
- const headScripts = (0, scripts_1.normalizeScripts)(opts.headScripts || []).map(getScriptContent);
64
- const scripts = (0, scripts_1.normalizeScripts)(opts.scripts || []).map(getScriptContent);
65
- markup = [
66
- `<!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>
67
57
  <html>
68
58
  <head>
69
59
  <meta charset="UTF-8" />
@@ -72,30 +62,29 @@ function getMarkup(opts) {
72
62
  content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
73
63
  />
74
64
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />`,
75
- metas.join('\n'),
76
- favicon,
77
- title,
78
- links.join('\n'),
79
- styles.join('\n'),
80
- headScripts.join('\n'),
81
- `</head>
65
+ metas.join('\n'),
66
+ favicon,
67
+ title,
68
+ links.join('\n'),
69
+ styles.join('\n'),
70
+ headScripts.join('\n'),
71
+ `</head>
82
72
  <body>`,
83
- markup,
84
- scripts.join('\n'),
85
- `</body>
73
+ markup,
74
+ scripts.join('\n'),
75
+ `</body>
86
76
  </html>`,
87
- ]
88
- .filter(Boolean)
89
- .join('\n');
90
- if (opts.modifyHTML) {
91
- markup = yield opts.modifyHTML(markup, { path: opts.path });
92
- }
93
- return markup;
94
- });
77
+ ]
78
+ .filter(Boolean)
79
+ .join('\n');
80
+ if (opts.modifyHTML) {
81
+ markup = await opts.modifyHTML(markup, { path: opts.path });
82
+ }
83
+ return markup;
95
84
  }
96
85
  exports.getMarkup = getMarkup;
97
86
  function createRequestHandler(opts) {
98
- return (req, res, next) => __awaiter(this, void 0, void 0, function* () {
87
+ return async (req, res, next) => {
99
88
  var _a;
100
89
  if (opts.historyType === 'browser' &&
101
90
  opts.base !== '/' &&
@@ -111,12 +100,12 @@ function createRequestHandler(opts) {
111
100
  // const matches = matchRoutes(routes, req.path, opts.base);
112
101
  // 其他接受 HTML 的请求都兜底返回 HTML
113
102
  res.set('Content-Type', 'text/html');
114
- const markup = yield getMarkup(Object.assign(Object.assign({}, opts), { path: req.path }));
103
+ const markup = await getMarkup({ ...opts, path: req.path });
115
104
  res.end(markup);
116
105
  }
117
106
  else {
118
107
  next();
119
108
  }
120
- });
109
+ };
121
110
  }
122
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.14",
3
+ "version": "4.0.0-rc.17",
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",
@@ -20,7 +20,7 @@
20
20
  "dev": "pnpm build -- --watch"
21
21
  },
22
22
  "dependencies": {
23
- "@umijs/bundler-utils": "4.0.0-rc.14",
23
+ "@umijs/bundler-utils": "4.0.0-rc.17",
24
24
  "history": "5.3.0",
25
25
  "react": "18.1.0",
26
26
  "react-dom": "18.1.0",