@umijs/renderer-react 4.0.0-canary.20240321.1 → 4.0.0-canary.20240419.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/browser.d.ts CHANGED
@@ -18,11 +18,21 @@ export declare type RenderClientOpts = {
18
18
  * @doc https://umijs.org/docs/api/config#runtimepublicpath
19
19
  */
20
20
  runtimePublicPath?: boolean;
21
+ /**
22
+ * react dom 渲染的的目标节点 id
23
+ * @doc 一般不需要改,微前端的时候会变化
24
+ */
25
+ mountElementId?: string;
21
26
  /**
22
27
  * react dom 渲染的的目标 dom
23
28
  * @doc 一般不需要改,微前端的时候会变化
24
29
  */
25
30
  rootElement?: HTMLElement;
31
+ /**
32
+ * ssr 是否从 app root 根节点开始 render
33
+ * @doc 默认 false, 从 app root 开始 render,为 true 时从 html 开始
34
+ */
35
+ renderFromRoot?: boolean;
26
36
  /**
27
37
  * 当前的路由配置
28
38
  */
package/dist/browser.js CHANGED
@@ -7,6 +7,7 @@ import ReactDOM from 'react-dom/client';
7
7
  import { matchRoutes, Router, useRoutes } from 'react-router-dom';
8
8
  import { AppContext, useAppData } from "./appContext";
9
9
  import { fetchServerLoader } from "./dataFetcher";
10
+ import { Html } from "./html";
10
11
  import { createClientRoutes } from "./routes";
11
12
  var root = null;
12
13
 
@@ -247,7 +248,16 @@ export function renderClient(opts) {
247
248
  // 为了测试,直接返回组件
248
249
  if (opts.components) return Browser;
249
250
  if (opts.hydrate) {
250
- ReactDOM.hydrateRoot(rootElement, /*#__PURE__*/React.createElement(Browser, null));
251
+ // @ts-ignore
252
+ var loaderData = window.__UMI_LOADER_DATA__ || {};
253
+ // @ts-ignore
254
+ var metadata = window.__UMI_METADATA_LOADER_DATA__ || {};
255
+ var hydtateHtmloptions = {
256
+ metadata: metadata,
257
+ loaderData: loaderData,
258
+ mountElementId: opts.mountElementId
259
+ };
260
+ ReactDOM.hydrateRoot(opts.renderFromRoot ? rootElement : document, /*#__PURE__*/React.createElement(Html, hydtateHtmloptions, /*#__PURE__*/React.createElement(Browser, null)));
251
261
  return;
252
262
  }
253
263
  if (ReactDOM.createRoot) {
@@ -7,7 +7,8 @@ export function fetchServerLoader(_ref) {
7
7
  url: window.location.href
8
8
  }).toString();
9
9
  // 在有basename的情况下__serverLoader的请求路径需要加上basename
10
- var url = "".concat(withEndSlash(basename), "__serverLoader?").concat(query);
10
+ // FIXME: 先临时解自定义 serverLoader 请求路径的问题,后续改造 serverLoader 时再提取成类似 runtimeServerLoader 的配置项
11
+ var url = "".concat(withEndSlash(window.umiServerLoaderPath || basename), "__serverLoader?").concat(query);
11
12
  fetch(url, {
12
13
  credentials: 'include'
13
14
  }).then(function (d) {
package/dist/html.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import React from 'react';
2
+ import { IHtmlProps } from './types';
3
+ export declare function Html({ children, loaderData, manifest, htmlPageOpts, renderFromRoot, mountElementId, }: React.PropsWithChildren<IHtmlProps>): JSX.Element;
package/dist/html.js ADDED
@@ -0,0 +1,142 @@
1
+ import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
2
+ import _extends from "@babel/runtime/helpers/esm/extends";
3
+ import _typeof from "@babel/runtime/helpers/esm/typeof";
4
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
5
+ var _excluded = ["content"],
6
+ _excluded2 = ["content"];
7
+ import React from 'react';
8
+ var RE_URL = /^(http:|https:)?\/\//;
9
+ function isUrl(str) {
10
+ return RE_URL.test(str) || str.startsWith('/') && !str.startsWith('/*') || str.startsWith('./') || str.startsWith('../');
11
+ }
12
+ function normalizeScripts(script) {
13
+ var extraProps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
14
+ if (typeof script === 'string') {
15
+ return isUrl(script) ? _objectSpread({
16
+ src: script
17
+ }, extraProps) : {
18
+ content: script
19
+ };
20
+ } else if (_typeof(script) === 'object') {
21
+ return _objectSpread(_objectSpread({}, script), extraProps);
22
+ } else {
23
+ throw new Error("Invalid script type: ".concat(_typeof(script)));
24
+ }
25
+ }
26
+ function generatorStyle(style) {
27
+ return isUrl(style) ? {
28
+ type: 'link',
29
+ href: style
30
+ } : {
31
+ type: 'style',
32
+ content: style
33
+ };
34
+ }
35
+ var HydrateMetadata = function HydrateMetadata(props) {
36
+ var _htmlPageOpts$favicon, _htmlPageOpts$keyword, _htmlPageOpts$metas, _htmlPageOpts$links, _htmlPageOpts$styles, _htmlPageOpts$headScr;
37
+ var htmlPageOpts = props.htmlPageOpts;
38
+ return /*#__PURE__*/React.createElement(React.Fragment, null, (htmlPageOpts === null || htmlPageOpts === void 0 ? void 0 : htmlPageOpts.title) && /*#__PURE__*/React.createElement("title", null, htmlPageOpts.title), htmlPageOpts === null || htmlPageOpts === void 0 || (_htmlPageOpts$favicon = htmlPageOpts.favicons) === null || _htmlPageOpts$favicon === void 0 ? void 0 : _htmlPageOpts$favicon.map(function (favicon, key) {
39
+ return /*#__PURE__*/React.createElement("link", {
40
+ key: key,
41
+ rel: "shortcut icon",
42
+ href: favicon
43
+ });
44
+ }), (htmlPageOpts === null || htmlPageOpts === void 0 ? void 0 : htmlPageOpts.description) && /*#__PURE__*/React.createElement("meta", {
45
+ name: "description",
46
+ content: htmlPageOpts.description
47
+ }), (htmlPageOpts === null || htmlPageOpts === void 0 || (_htmlPageOpts$keyword = htmlPageOpts.keywords) === null || _htmlPageOpts$keyword === void 0 ? void 0 : _htmlPageOpts$keyword.length) && /*#__PURE__*/React.createElement("meta", {
48
+ name: "keywords",
49
+ content: htmlPageOpts.keywords.join(',')
50
+ }), htmlPageOpts === null || htmlPageOpts === void 0 || (_htmlPageOpts$metas = htmlPageOpts.metas) === null || _htmlPageOpts$metas === void 0 ? void 0 : _htmlPageOpts$metas.map(function (em) {
51
+ return /*#__PURE__*/React.createElement("meta", {
52
+ key: em.name,
53
+ name: em.name,
54
+ content: em.content
55
+ });
56
+ }), htmlPageOpts === null || htmlPageOpts === void 0 || (_htmlPageOpts$links = htmlPageOpts.links) === null || _htmlPageOpts$links === void 0 ? void 0 : _htmlPageOpts$links.map(function (link, key) {
57
+ return /*#__PURE__*/React.createElement("link", _extends({
58
+ key: key
59
+ }, link));
60
+ }), htmlPageOpts === null || htmlPageOpts === void 0 || (_htmlPageOpts$styles = htmlPageOpts.styles) === null || _htmlPageOpts$styles === void 0 ? void 0 : _htmlPageOpts$styles.map(function (style, key) {
61
+ var _generatorStyle = generatorStyle(style),
62
+ type = _generatorStyle.type,
63
+ href = _generatorStyle.href,
64
+ content = _generatorStyle.content;
65
+ if (type === 'link') {
66
+ return /*#__PURE__*/React.createElement("link", {
67
+ key: key,
68
+ rel: "stylesheet",
69
+ href: href
70
+ });
71
+ } else if (type === 'style') {
72
+ return /*#__PURE__*/React.createElement("style", {
73
+ key: key
74
+ }, content);
75
+ }
76
+ }), htmlPageOpts === null || htmlPageOpts === void 0 || (_htmlPageOpts$headScr = htmlPageOpts.headScripts) === null || _htmlPageOpts$headScr === void 0 ? void 0 : _htmlPageOpts$headScr.map(function (script, key) {
77
+ var _normalizeScripts = normalizeScripts(script),
78
+ content = _normalizeScripts.content,
79
+ rest = _objectWithoutProperties(_normalizeScripts, _excluded);
80
+ return /*#__PURE__*/React.createElement("script", _extends({
81
+ key: key
82
+ }, rest), content);
83
+ }));
84
+ };
85
+ export function Html(_ref) {
86
+ var _htmlPageOpts$scripts;
87
+ var children = _ref.children,
88
+ loaderData = _ref.loaderData,
89
+ manifest = _ref.manifest,
90
+ htmlPageOpts = _ref.htmlPageOpts,
91
+ renderFromRoot = _ref.renderFromRoot,
92
+ mountElementId = _ref.mountElementId;
93
+ // TODO: 处理 head 标签,比如 favicon.ico 的一致性
94
+ // TODO: root 支持配置
95
+ if (renderFromRoot) {
96
+ return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(HydrateMetadata, {
97
+ htmlPageOpts: htmlPageOpts
98
+ }), /*#__PURE__*/React.createElement("div", {
99
+ id: mountElementId
100
+ }, children));
101
+ }
102
+ var serverBuildManifest = typeof window === 'undefined' ? manifest :
103
+ // @ts-ignore
104
+ window.__UMI_BUILD_MANIFEST_DATA__;
105
+ return (
106
+ /*#__PURE__*/
107
+ // FIXME: Resolve the hydrate warning for suppressHydrationWarning(3)
108
+ React.createElement("html", {
109
+ suppressHydrationWarning: true,
110
+ lang: (htmlPageOpts === null || htmlPageOpts === void 0 ? void 0 : htmlPageOpts.lang) || 'en'
111
+ }, /*#__PURE__*/React.createElement("head", null, /*#__PURE__*/React.createElement("meta", {
112
+ charSet: "utf-8"
113
+ }), /*#__PURE__*/React.createElement("meta", {
114
+ name: "viewport",
115
+ content: "width=device-width, initial-scale=1"
116
+ }), (serverBuildManifest === null || serverBuildManifest === void 0 ? void 0 : serverBuildManifest.assets['umi.css']) && /*#__PURE__*/React.createElement("link", {
117
+ suppressHydrationWarning: true,
118
+ rel: "stylesheet",
119
+ href: manifest === null || manifest === void 0 ? void 0 : manifest.assets['umi.css']
120
+ }), /*#__PURE__*/React.createElement(HydrateMetadata, {
121
+ htmlPageOpts: htmlPageOpts
122
+ })), /*#__PURE__*/React.createElement("body", null, /*#__PURE__*/React.createElement("noscript", {
123
+ dangerouslySetInnerHTML: {
124
+ __html: "<b>Enable JavaScript to run this app.</b>"
125
+ }
126
+ }), /*#__PURE__*/React.createElement("div", {
127
+ id: mountElementId
128
+ }, children), /*#__PURE__*/React.createElement("script", {
129
+ suppressHydrationWarning: true,
130
+ dangerouslySetInnerHTML: {
131
+ __html: "window.__UMI_LOADER_DATA__ = ".concat(JSON.stringify(loaderData || {}), "; window.__UMI_METADATA_LOADER_DATA__ = ").concat(JSON.stringify(htmlPageOpts || {}), "; window.__UMI_BUILD_MANIFEST_DATA__ = ").concat(JSON.stringify(manifest) || {})
132
+ }
133
+ }), htmlPageOpts === null || htmlPageOpts === void 0 || (_htmlPageOpts$scripts = htmlPageOpts.scripts) === null || _htmlPageOpts$scripts === void 0 ? void 0 : _htmlPageOpts$scripts.map(function (script, key) {
134
+ var _normalizeScripts2 = normalizeScripts(script),
135
+ content = _normalizeScripts2.content,
136
+ rest = _objectWithoutProperties(_normalizeScripts2, _excluded2);
137
+ return /*#__PURE__*/React.createElement("script", _extends({
138
+ key: key
139
+ }, rest), content);
140
+ })))
141
+ );
142
+ }
package/dist/server.d.ts CHANGED
@@ -1,15 +1,2 @@
1
- import type { IMetadata } from '@umijs/server/dist/types';
2
- import { IRouteComponents, IRoutesById } from './types';
3
- interface IHtmlProps {
4
- routes: IRoutesById;
5
- routeComponents: IRouteComponents;
6
- pluginManager: any;
7
- location: string;
8
- loaderData: {
9
- [routeKey: string]: any;
10
- };
11
- manifest: any;
12
- metadata?: IMetadata;
13
- }
14
- export declare function getClientRootComponent(opts: IHtmlProps): Promise<JSX.Element>;
15
- export {};
1
+ import { IRootComponentOptions } from './types';
2
+ export declare function getClientRootComponent(opts: IRootComponentOptions): Promise<JSX.Element>;
package/dist/server.js CHANGED
@@ -5,6 +5,7 @@ import React from 'react';
5
5
  import { StaticRouter } from 'react-router-dom/server';
6
6
  import { AppContext } from "./appContext";
7
7
  import { Routes } from "./browser";
8
+ import { Html } from "./html";
8
9
  import { createClientRoutes } from "./routes";
9
10
  // Get the root React component for ReactDOMServer.renderToString
10
11
  export function getClientRootComponent(_x) {
@@ -17,11 +18,18 @@ function _getClientRootComponent() {
17
18
  while (1) switch (_context.prev = _context.next) {
18
19
  case 0:
19
20
  basename = '/';
20
- components = _objectSpread({}, opts.routeComponents);
21
+ components = _objectSpread({}, opts.routeComponents); // todo 参数对齐
21
22
  clientRoutes = createClientRoutes({
22
23
  routesById: opts.routes,
23
24
  routeComponents: components
24
25
  });
26
+ opts.pluginManager.applyPlugins({
27
+ key: 'patchClientRoutes',
28
+ type: 'event',
29
+ args: {
30
+ routes: clientRoutes
31
+ }
32
+ });
25
33
  rootContainer = /*#__PURE__*/React.createElement(StaticRouter, {
26
34
  basename: basename,
27
35
  location: opts.location
@@ -49,54 +57,11 @@ function _getClientRootComponent() {
49
57
  }
50
58
  }, rootContainer);
51
59
  return _context.abrupt("return", /*#__PURE__*/React.createElement(Html, opts, app));
52
- case 7:
60
+ case 8:
53
61
  case "end":
54
62
  return _context.stop();
55
63
  }
56
64
  }, _callee);
57
65
  }));
58
66
  return _getClientRootComponent.apply(this, arguments);
59
- }
60
- function Html(_ref) {
61
- var _metadata$keywords, _metadata$metas;
62
- var children = _ref.children,
63
- loaderData = _ref.loaderData,
64
- manifest = _ref.manifest,
65
- metadata = _ref.metadata;
66
- // TODO: 处理 head 标签,比如 favicon.ico 的一致性
67
- // TODO: root 支持配置
68
-
69
- return /*#__PURE__*/React.createElement("html", {
70
- lang: (metadata === null || metadata === void 0 ? void 0 : metadata.lang) || 'en'
71
- }, /*#__PURE__*/React.createElement("head", null, /*#__PURE__*/React.createElement("meta", {
72
- charSet: "utf-8"
73
- }), /*#__PURE__*/React.createElement("meta", {
74
- name: "viewport",
75
- content: "width=device-width, initial-scale=1"
76
- }), (metadata === null || metadata === void 0 ? void 0 : metadata.title) && /*#__PURE__*/React.createElement("title", null, metadata.title), (metadata === null || metadata === void 0 ? void 0 : metadata.description) && /*#__PURE__*/React.createElement("meta", {
77
- name: "description",
78
- content: metadata.description
79
- }), (metadata === null || metadata === void 0 || (_metadata$keywords = metadata.keywords) === null || _metadata$keywords === void 0 ? void 0 : _metadata$keywords.length) && /*#__PURE__*/React.createElement("meta", {
80
- name: "keywords",
81
- content: metadata.keywords.join(',')
82
- }), metadata === null || metadata === void 0 || (_metadata$metas = metadata.metas) === null || _metadata$metas === void 0 ? void 0 : _metadata$metas.map(function (em) {
83
- return /*#__PURE__*/React.createElement("meta", {
84
- key: em.name,
85
- name: em.name,
86
- content: em.content
87
- });
88
- }), manifest.assets['umi.css'] && /*#__PURE__*/React.createElement("link", {
89
- rel: "stylesheet",
90
- href: manifest.assets['umi.css']
91
- })), /*#__PURE__*/React.createElement("body", null, /*#__PURE__*/React.createElement("noscript", {
92
- dangerouslySetInnerHTML: {
93
- __html: "<b>Enable JavaScript to run this app.</b>"
94
- }
95
- }), /*#__PURE__*/React.createElement("div", {
96
- id: "root"
97
- }, children), /*#__PURE__*/React.createElement("script", {
98
- dangerouslySetInnerHTML: {
99
- __html: "window.__UMI_LOADER_DATA__ = ".concat(JSON.stringify(loaderData))
100
- }
101
- })));
102
67
  }
package/dist/types.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  /// <reference types="react" />
2
+ import type { IhtmlPageOpts } from '@umijs/server/dist/types';
2
3
  import type { RouteMatch, RouteObject } from 'react-router-dom';
3
4
  export interface IRouteSSRProps {
4
5
  clientLoader?: () => Promise<any>;
@@ -33,3 +34,35 @@ export interface IRouteComponents {
33
34
  export interface ILoaderData {
34
35
  [routeKey: string]: any;
35
36
  }
37
+ interface IHtmlHydrateOptions {
38
+ htmlPageOpts?: IhtmlPageOpts;
39
+ renderFromRoot?: boolean;
40
+ mountElementId?: string;
41
+ }
42
+ export interface IRootComponentOptions extends IHtmlHydrateOptions {
43
+ routes: IRoutesById;
44
+ routeComponents: IRouteComponents;
45
+ pluginManager: any;
46
+ location: string;
47
+ loaderData: {
48
+ [routeKey: string]: any;
49
+ };
50
+ manifest: any;
51
+ }
52
+ export interface IHtmlProps extends IHtmlHydrateOptions {
53
+ children?: React.ReactNode;
54
+ loaderData?: {
55
+ [routeKey: string]: any;
56
+ };
57
+ manifest?: any;
58
+ }
59
+ export declare type IScript = Partial<{
60
+ async: boolean;
61
+ charset: string;
62
+ content: string;
63
+ crossOrigin: string | null;
64
+ defer: boolean;
65
+ src: string;
66
+ type: string;
67
+ }> | string;
68
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umijs/renderer-react",
3
- "version": "4.0.0-canary.20240321.1",
3
+ "version": "4.0.0-canary.20240419.1",
4
4
  "description": "@umijs/renderer-react",
5
5
  "homepage": "https://github.com/umijs/umi/tree/master/packages/renderer-react#readme",
6
6
  "bugs": "https://github.com/umijs/umi/issues",