@umijs/server 4.0.0-beta.1 → 4.0.0-beta.5

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/server.d.ts CHANGED
@@ -6,15 +6,19 @@ interface IOpts {
6
6
  id: string;
7
7
  parentId?: string;
8
8
  }>;
9
- config: Record<string, any>;
10
- scripts: string[];
9
+ links?: Record<string, string>[];
10
+ metas?: Record<string, string>[];
11
+ styles?: (Record<string, string> | string)[];
12
+ favicon?: string;
13
+ headScripts?: (Record<string, string> | string)[];
14
+ scripts?: (Record<string, string> | string)[];
11
15
  esmScript?: boolean;
12
16
  modifyHTML?: (opts: {
13
17
  html: string;
14
18
  path?: string;
15
19
  }) => Promise<string>;
16
20
  }
17
- export declare function getMarkup(opts: Pick<IOpts, 'scripts' | 'esmScript' | 'modifyHTML' | 'config'> & {
21
+ export declare function getMarkup(opts: Omit<IOpts, 'routes'> & {
18
22
  path?: string;
19
23
  }): Promise<string>;
20
24
  export declare function createRequestHandler(opts: IOpts): RequestHandler;
package/dist/server.js CHANGED
@@ -18,20 +18,49 @@ const server_1 = __importDefault(require("react-dom/server"));
18
18
  const react_router_dom_1 = require("react-router-dom");
19
19
  const routes_1 = require("./routes");
20
20
  const scripts_1 = require("./scripts");
21
+ const styles_1 = require("./styles");
21
22
  function getMarkup(opts) {
22
23
  return __awaiter(this, void 0, void 0, function* () {
23
24
  // TODO: use real component
24
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
+ }
25
32
  function getScriptContent(script) {
33
+ const attrs = propsToString({
34
+ props: script,
35
+ filters: ['src', 'content'],
36
+ });
26
37
  return script.src
27
- ? `<script${opts.esmScript ? ' type="module"' : ''} src='${script.src}'></script>`
28
- : `<script${opts.esmScript ? ' type="module"' : ''}>${script.content}</script>`;
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} />`;
29
55
  }
30
- const favicon = opts.config.favicon
31
- ? `<link rel="shortcut icon" href="${opts.config.favicon}">`
56
+ const favicon = opts.favicon
57
+ ? `<link rel="shortcut icon" href="${opts.favicon}">`
32
58
  : '';
33
- const headScripts = (0, scripts_1.normalizeScripts)(opts.config.headScripts || []).map(getScriptContent);
34
- const scripts = (0, scripts_1.normalizeScripts)(opts.scripts.concat(opts.config.scripts || [])).map(getScriptContent);
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);
35
64
  markup = [
36
65
  `<!DOCTYPE html>
37
66
  <html>
@@ -42,7 +71,10 @@ function getMarkup(opts) {
42
71
  content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
43
72
  />
44
73
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />`,
74
+ metas.join('\n'),
45
75
  favicon,
76
+ links.join('\n'),
77
+ styles.join('\n'),
46
78
  headScripts.join('\n'),
47
79
  `</head>
48
80
  <body>`,
@@ -0,0 +1,12 @@
1
+ export declare type IStyle = string | {
2
+ src?: string;
3
+ content?: string;
4
+ };
5
+ export declare function normalizeStyles(scripts: IStyle[]): {
6
+ src?: string | undefined;
7
+ content?: string | undefined;
8
+ }[];
9
+ export declare function normalizeStyle(style: IStyle): {
10
+ src?: string | undefined;
11
+ content?: string | undefined;
12
+ };
package/dist/styles.js ADDED
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.normalizeStyle = exports.normalizeStyles = void 0;
7
+ const assert_1 = __importDefault(require("assert"));
8
+ function normalizeStyles(scripts) {
9
+ return scripts.map(normalizeStyle);
10
+ }
11
+ exports.normalizeStyles = normalizeStyles;
12
+ const RE_URL = /^(http:|https:)?\/\//;
13
+ function normalizeStyle(style) {
14
+ if (typeof style === 'string') {
15
+ const isUrl = RE_URL.test(style) ||
16
+ style.startsWith('/') ||
17
+ style.startsWith('./') ||
18
+ style.startsWith('../');
19
+ return isUrl
20
+ ? {
21
+ src: style,
22
+ }
23
+ : { content: style };
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
+ }
32
+ }
33
+ exports.normalizeStyle = normalizeStyle;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umijs/server",
3
- "version": "4.0.0-beta.1",
3
+ "version": "4.0.0-beta.5",
4
4
  "description": "@umijs/server",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",