@umijs/server 4.0.0-beta.9 → 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 CHANGED
@@ -1 +1,2 @@
1
+ export { createServerRoutes } from './routes';
1
2
  export * from './server';
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.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
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
- const react_router_dom_1 = require("react-router-dom");
19
- const routes_1 = require("./routes");
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
- 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: '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} src="${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,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
- 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* () {
97
- // 匹配路由,不匹配走 next()
98
- // TODO: cache
99
- const routes = (0, routes_1.createServerRoutes)({
100
- routesById: opts.routes,
101
- });
102
- const matches = (0, react_router_dom_1.matchRoutes)(routes, req.path);
103
- if (matches) {
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 = yield getMarkup(Object.assign(Object.assign({}, opts), { path: req.path }));
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,6 +1,6 @@
1
1
  {
2
2
  "name": "@umijs/server",
3
- "version": "4.0.0-beta.9",
3
+ "version": "4.0.0-canary.202200505.1",
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,17 +16,15 @@
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
- "history": "5.0.1",
24
- "react": "18.0.0-alpha-4298ddbc5-20211023",
25
- "react-dom": "18.0.0-alpha-4298ddbc5-20211023",
26
- "react-router-dom": "6.0.0-beta.8"
27
- },
28
- "devDependencies": {
29
- "@types/express": "4.17.13"
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"
30
28
  },
31
29
  "publishConfig": {
32
30
  "access": "public"