@umijs/renderer-react 3.5.20 → 4.0.0-beta.4

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.js CHANGED
@@ -1,700 +1,2 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var runtime = require('@umijs/runtime');
6
- var React = require('react');
7
- var reactDom = require('react-dom');
8
- var reactRouterConfig = require('react-router-config');
9
-
10
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
11
-
12
- var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
13
-
14
- function ownKeys(object, enumerableOnly) {
15
- var keys = Object.keys(object);
16
-
17
- if (Object.getOwnPropertySymbols) {
18
- var symbols = Object.getOwnPropertySymbols(object);
19
-
20
- if (enumerableOnly) {
21
- symbols = symbols.filter(function (sym) {
22
- return Object.getOwnPropertyDescriptor(object, sym).enumerable;
23
- });
24
- }
25
-
26
- keys.push.apply(keys, symbols);
27
- }
28
-
29
- return keys;
30
- }
31
-
32
- function _objectSpread2(target) {
33
- for (var i = 1; i < arguments.length; i++) {
34
- var source = arguments[i] != null ? arguments[i] : {};
35
-
36
- if (i % 2) {
37
- ownKeys(Object(source), true).forEach(function (key) {
38
- _defineProperty(target, key, source[key]);
39
- });
40
- } else if (Object.getOwnPropertyDescriptors) {
41
- Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
42
- } else {
43
- ownKeys(Object(source)).forEach(function (key) {
44
- Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
45
- });
46
- }
47
- }
48
-
49
- return target;
50
- }
51
-
52
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
53
- try {
54
- var info = gen[key](arg);
55
- var value = info.value;
56
- } catch (error) {
57
- reject(error);
58
- return;
59
- }
60
-
61
- if (info.done) {
62
- resolve(value);
63
- } else {
64
- Promise.resolve(value).then(_next, _throw);
65
- }
66
- }
67
-
68
- function _asyncToGenerator(fn) {
69
- return function () {
70
- var self = this,
71
- args = arguments;
72
- return new Promise(function (resolve, reject) {
73
- var gen = fn.apply(self, args);
74
-
75
- function _next(value) {
76
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
77
- }
78
-
79
- function _throw(err) {
80
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
81
- }
82
-
83
- _next(undefined);
84
- });
85
- };
86
- }
87
-
88
- function _defineProperty(obj, key, value) {
89
- if (key in obj) {
90
- Object.defineProperty(obj, key, {
91
- value: value,
92
- enumerable: true,
93
- configurable: true,
94
- writable: true
95
- });
96
- } else {
97
- obj[key] = value;
98
- }
99
-
100
- return obj;
101
- }
102
-
103
- function _extends() {
104
- _extends = Object.assign || function (target) {
105
- for (var i = 1; i < arguments.length; i++) {
106
- var source = arguments[i];
107
-
108
- for (var key in source) {
109
- if (Object.prototype.hasOwnProperty.call(source, key)) {
110
- target[key] = source[key];
111
- }
112
- }
113
- }
114
-
115
- return target;
116
- };
117
-
118
- return _extends.apply(this, arguments);
119
- }
120
-
121
- function _objectWithoutPropertiesLoose(source, excluded) {
122
- if (source == null) return {};
123
- var target = {};
124
- var sourceKeys = Object.keys(source);
125
- var key, i;
126
-
127
- for (i = 0; i < sourceKeys.length; i++) {
128
- key = sourceKeys[i];
129
- if (excluded.indexOf(key) >= 0) continue;
130
- target[key] = source[key];
131
- }
132
-
133
- return target;
134
- }
135
-
136
- function _objectWithoutProperties(source, excluded) {
137
- if (source == null) return {};
138
-
139
- var target = _objectWithoutPropertiesLoose(source, excluded);
140
-
141
- var key, i;
142
-
143
- if (Object.getOwnPropertySymbols) {
144
- var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
145
-
146
- for (i = 0; i < sourceSymbolKeys.length; i++) {
147
- key = sourceSymbolKeys[i];
148
- if (excluded.indexOf(key) >= 0) continue;
149
- if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
150
- target[key] = source[key];
151
- }
152
- }
153
-
154
- return target;
155
- }
156
-
157
- function _slicedToArray(arr, i) {
158
- return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
159
- }
160
-
161
- function _arrayWithHoles(arr) {
162
- if (Array.isArray(arr)) return arr;
163
- }
164
-
165
- function _iterableToArrayLimit(arr, i) {
166
- var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
167
-
168
- if (_i == null) return;
169
- var _arr = [];
170
- var _n = true;
171
- var _d = false;
172
-
173
- var _s, _e;
174
-
175
- try {
176
- for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) {
177
- _arr.push(_s.value);
178
-
179
- if (i && _arr.length === i) break;
180
- }
181
- } catch (err) {
182
- _d = true;
183
- _e = err;
184
- } finally {
185
- try {
186
- if (!_n && _i["return"] != null) _i["return"]();
187
- } finally {
188
- if (_d) throw _e;
189
- }
190
- }
191
-
192
- return _arr;
193
- }
194
-
195
- function _unsupportedIterableToArray(o, minLen) {
196
- if (!o) return;
197
- if (typeof o === "string") return _arrayLikeToArray(o, minLen);
198
- var n = Object.prototype.toString.call(o).slice(8, -1);
199
- if (n === "Object" && o.constructor) n = o.constructor.name;
200
- if (n === "Map" || n === "Set") return Array.from(o);
201
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
202
- }
203
-
204
- function _arrayLikeToArray(arr, len) {
205
- if (len == null || len > arr.length) len = arr.length;
206
-
207
- for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
208
-
209
- return arr2;
210
- }
211
-
212
- function _nonIterableRest() {
213
- throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
214
- }
215
-
216
- function _createForOfIteratorHelper(o, allowArrayLike) {
217
- var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
218
-
219
- if (!it) {
220
- if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
221
- if (it) o = it;
222
- var i = 0;
223
-
224
- var F = function () {};
225
-
226
- return {
227
- s: F,
228
- n: function () {
229
- if (i >= o.length) return {
230
- done: true
231
- };
232
- return {
233
- done: false,
234
- value: o[i++]
235
- };
236
- },
237
- e: function (e) {
238
- throw e;
239
- },
240
- f: F
241
- };
242
- }
243
-
244
- throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
245
- }
246
-
247
- var normalCompletion = true,
248
- didErr = false,
249
- err;
250
- return {
251
- s: function () {
252
- it = it.call(o);
253
- },
254
- n: function () {
255
- var step = it.next();
256
- normalCompletion = step.done;
257
- return step;
258
- },
259
- e: function (e) {
260
- didErr = true;
261
- err = e;
262
- },
263
- f: function () {
264
- try {
265
- if (!normalCompletion && it.return != null) it.return();
266
- } finally {
267
- if (didErr) throw err;
268
- }
269
- }
270
- };
271
- }
272
-
273
- function Route(props) {
274
- return /*#__PURE__*/React__default['default'].createElement(runtime.__RouterContext.Consumer, null, function (context) {
275
- var location = props.location || context.location;
276
- var match = props.computedMatch;
277
-
278
- var newProps = _objectSpread2(_objectSpread2({}, context), {}, {
279
- location: location,
280
- match: match
281
- });
282
-
283
- var render = props.render;
284
- return /*#__PURE__*/React__default['default'].createElement(runtime.__RouterContext.Provider, {
285
- value: newProps
286
- }, newProps.match ? render(_objectSpread2(_objectSpread2({}, props.layoutProps), newProps)) : null);
287
- });
288
- }
289
-
290
- var _excluded = ["children"];
291
- function Switch(props) {
292
- return /*#__PURE__*/React__default['default'].createElement(runtime.__RouterContext.Consumer, null, function (context) {
293
- var children = props.children,
294
- extraProps = _objectWithoutProperties(props, _excluded);
295
-
296
- var location = props.location || context.location;
297
- var element,
298
- match = null;
299
- React.Children.forEach(children, function (child) {
300
- if (match === null && /*#__PURE__*/React.isValidElement(child)) {
301
- element = child;
302
- var path = child.props.path || child.props.from;
303
- match = path ? runtime.matchPath(location.pathname, _objectSpread2(_objectSpread2({}, child.props), {}, {
304
- path: path
305
- })) : context.match;
306
- }
307
- });
308
- return match ? /*#__PURE__*/React.cloneElement(element, {
309
- location: location,
310
- computedMatch: match,
311
- layoutProps: extraProps
312
- }) : null;
313
- });
314
- }
315
-
316
- var _excluded$1 = ["component"];
317
-
318
- function wrapInitialPropsFetch(route, opts) {
319
- var component = route.component,
320
- restRouteParams = _objectWithoutProperties(route, _excluded$1);
321
-
322
- var Component = route.component;
323
-
324
- function ComponentWithInitialPropsFetch(props) {
325
- var _useState = React.useState(function () {
326
- return window.g_initialProps;
327
- }),
328
- _useState2 = _slicedToArray(_useState, 2),
329
- initialProps = _useState2[0],
330
- setInitialProps = _useState2[1];
331
-
332
- React.useEffect(function () {
333
- /**
334
- * 1. 首次渲染时,此时 window.g_initialProps 变量存在,不需要再走一次 getInitialProps,这样一次 SSR 就走了 2 次 getInitialProps
335
- * 2. 但是路由切换时,window.getInitialProps 会被赋为 null,这时候就走 getInitialProps 逻辑
336
- * 3. 如果任何时候都走 2 次,配置 forceInitial: true,这个场景用于静态站点的首屏加载希望走最新数据
337
- * 4. 开启动态加载后,会在执行 getInitialProps 前预加载下
338
- */
339
- var handleGetInitialProps = /*#__PURE__*/function () {
340
- var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
341
- var _preloadComponent;
342
-
343
- var preloadComponent, defaultCtx, ctx, _initialProps;
344
-
345
- return regeneratorRuntime.wrap(function _callee$(_context) {
346
- while (1) {
347
- switch (_context.prev = _context.next) {
348
- case 0:
349
- // preload when enalbe dynamicImport
350
- preloadComponent = Component;
351
-
352
- if (!Component.preload) {
353
- _context.next = 6;
354
- break;
355
- }
356
-
357
- _context.next = 4;
358
- return Component.preload();
359
-
360
- case 4:
361
- preloadComponent = _context.sent;
362
- // for test case, really use .default
363
- preloadComponent = preloadComponent.default || preloadComponent;
364
-
365
- case 6:
366
- defaultCtx = _objectSpread2(_objectSpread2({
367
- isServer: false,
368
- match: props === null || props === void 0 ? void 0 : props.match,
369
- history: props === null || props === void 0 ? void 0 : props.history,
370
- route: route
371
- }, opts.getInitialPropsCtx || {}), restRouteParams);
372
-
373
- if (!((_preloadComponent = preloadComponent) === null || _preloadComponent === void 0 ? void 0 : _preloadComponent.getInitialProps)) {
374
- _context.next = 15;
375
- break;
376
- }
377
-
378
- _context.next = 10;
379
- return opts.plugin.applyPlugins({
380
- key: 'ssr.modifyGetInitialPropsCtx',
381
- type: runtime.ApplyPluginsType.modify,
382
- initialValue: defaultCtx,
383
- async: true
384
- });
385
-
386
- case 10:
387
- ctx = _context.sent;
388
- _context.next = 13;
389
- return preloadComponent.getInitialProps(ctx || defaultCtx);
390
-
391
- case 13:
392
- _initialProps = _context.sent;
393
- setInitialProps(_initialProps);
394
-
395
- case 15:
396
- case "end":
397
- return _context.stop();
398
- }
399
- }
400
- }, _callee);
401
- }));
402
-
403
- return function handleGetInitialProps() {
404
- return _ref.apply(this, arguments);
405
- };
406
- }(); // null 时,一定会触发 getInitialProps 执行
407
-
408
-
409
- if (!window.g_initialProps) {
410
- handleGetInitialProps();
411
- }
412
- }, [window.location.pathname, window.location.search]);
413
- return /*#__PURE__*/React__default['default'].createElement(Component, _extends({}, props, initialProps));
414
- } // flag for having wrappered
415
-
416
-
417
- ComponentWithInitialPropsFetch.wrapInitialPropsLoaded = true;
418
- ComponentWithInitialPropsFetch.displayName = 'ComponentWithInitialPropsFetch';
419
- return ComponentWithInitialPropsFetch;
420
- }
421
-
422
- function _render(_ref2) {
423
- var route = _ref2.route,
424
- opts = _ref2.opts,
425
- props = _ref2.props;
426
- var routes = renderRoutes(_objectSpread2(_objectSpread2({}, opts), {}, {
427
- routes: route.routes || [],
428
- rootRoutes: opts.rootRoutes
429
- }), {
430
- location: props.location
431
- });
432
- var Component = route.component,
433
- wrappers = route.wrappers;
434
-
435
- if (Component) {
436
- var defaultPageInitialProps = opts.isServer ? {} : window.g_initialProps;
437
-
438
- var newProps = _objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2({}, props), opts.extraProps), opts.pageInitialProps || defaultPageInitialProps), {}, {
439
- route: route,
440
- routes: opts.rootRoutes
441
- }); // @ts-ignore
442
-
443
-
444
- var ret = /*#__PURE__*/React__default['default'].createElement(Component, newProps, routes); // route.wrappers
445
-
446
- if (wrappers) {
447
- var len = wrappers.length - 1;
448
-
449
- while (len >= 0) {
450
- ret = /*#__PURE__*/React.createElement(wrappers[len], newProps, ret);
451
- len -= 1;
452
- }
453
- }
454
-
455
- return ret;
456
- } else {
457
- return routes;
458
- }
459
- }
460
-
461
- function getRouteElement(_ref3) {
462
- var route = _ref3.route,
463
- index = _ref3.index,
464
- opts = _ref3.opts;
465
- var routeProps = {
466
- key: route.key || index,
467
- exact: route.exact,
468
- strict: route.strict,
469
- sensitive: route.sensitive,
470
- path: route.path
471
- };
472
-
473
- if (route.redirect) {
474
- return /*#__PURE__*/React__default['default'].createElement(runtime.Redirect, _extends({}, routeProps, {
475
- from: route.path,
476
- to: route.redirect
477
- }));
478
- } else {
479
- var _route$component, _route$component2, _route$component3;
480
-
481
- // avoid mount and unmount with url hash change
482
- if ( // only when SSR config enable
483
- opts.ssrProps && !opts.isServer && // make sure loaded once
484
- !((_route$component = route.component) === null || _route$component === void 0 ? void 0 : _route$component.wrapInitialPropsLoaded) && ( // TODO need a type
485
- ((_route$component2 = route.component) === null || _route$component2 === void 0 ? void 0 : _route$component2.getInitialProps) || ((_route$component3 = route.component) === null || _route$component3 === void 0 ? void 0 : _route$component3.preload))) {
486
- // client Render for enable ssr, but not sure SSR success
487
- route.component = wrapInitialPropsFetch(route, opts);
488
- }
489
-
490
- return /*#__PURE__*/React__default['default'].createElement(Route, _extends({}, routeProps, {
491
- render: function render(props) {
492
- return _render({
493
- route: route,
494
- opts: opts,
495
- props: props
496
- });
497
- }
498
- }));
499
- }
500
- }
501
-
502
- function renderRoutes(opts) {
503
- var switchProps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
504
- return opts.routes ? /*#__PURE__*/React__default['default'].createElement(Switch, switchProps, opts.routes.map(function (route, index) {
505
- return getRouteElement({
506
- route: route,
507
- index: index,
508
- opts: _objectSpread2(_objectSpread2({}, opts), {}, {
509
- rootRoutes: opts.rootRoutes || opts.routes
510
- })
511
- });
512
- })) : null;
513
- }
514
-
515
- var _excluded$2 = ["history"];
516
-
517
- function RouterComponent(props) {
518
- var history = props.history,
519
- renderRoutesProps = _objectWithoutProperties(props, _excluded$2);
520
-
521
- React.useEffect(function () {
522
- // first time using window.g_initialProps
523
- // switch route fetching data, if exact route reset window.getInitialProps
524
- if (window.g_useSSR) {
525
- window.g_initialProps = null;
526
- }
527
-
528
- function routeChangeHandler(location, action) {
529
- var matchedRoutes = reactRouterConfig.matchRoutes(props.routes, location.pathname); // Set title
530
-
531
- if (typeof document !== 'undefined' && renderRoutesProps.defaultTitle !== undefined) {
532
- document.title = matchedRoutes.length && // @ts-ignore
533
- matchedRoutes[matchedRoutes.length - 1].route.title || renderRoutesProps.defaultTitle || '';
534
- }
535
-
536
- props.plugin.applyPlugins({
537
- key: 'onRouteChange',
538
- type: runtime.ApplyPluginsType.event,
539
- args: {
540
- routes: props.routes,
541
- matchedRoutes: matchedRoutes,
542
- location: location,
543
- action: action
544
- }
545
- });
546
- }
547
-
548
- routeChangeHandler(history.location, 'POP');
549
- return history.listen(routeChangeHandler);
550
- }, [history]);
551
- return /*#__PURE__*/React__default['default'].createElement(runtime.Router, {
552
- history: history
553
- }, renderRoutes(renderRoutesProps));
554
- }
555
- /**
556
- * preload for SSR in dynamicImport
557
- * exec preload Promise function before ReactDOM.hydrate
558
- * @param Routes
559
- */
560
-
561
-
562
- function preloadComponent(_x) {
563
- return _preloadComponent.apply(this, arguments);
564
- }
565
-
566
- function _preloadComponent() {
567
- _preloadComponent = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(readyRoutes) {
568
- var pathname,
569
- matchedRoutes,
570
- _iterator,
571
- _step,
572
- _route$component,
573
- matchRoute,
574
- route,
575
- _preloadComponent2,
576
- _args = arguments;
577
-
578
- return regeneratorRuntime.wrap(function _callee$(_context) {
579
- while (1) {
580
- switch (_context.prev = _context.next) {
581
- case 0:
582
- pathname = _args.length > 1 && _args[1] !== undefined ? _args[1] : window.location.pathname;
583
- // using matched routes not load all routes
584
- matchedRoutes = reactRouterConfig.matchRoutes(readyRoutes, pathname);
585
- _iterator = _createForOfIteratorHelper(matchedRoutes);
586
- _context.prev = 3;
587
-
588
- _iterator.s();
589
-
590
- case 5:
591
- if ((_step = _iterator.n()).done) {
592
- _context.next = 19;
593
- break;
594
- }
595
-
596
- matchRoute = _step.value;
597
- route = matchRoute.route; // load all preload function, because of only a chance to load
598
-
599
- if (!(typeof route.component !== 'string' && ((_route$component = route.component) === null || _route$component === void 0 ? void 0 : _route$component.preload))) {
600
- _context.next = 13;
601
- break;
602
- }
603
-
604
- _context.next = 11;
605
- return route.component.preload();
606
-
607
- case 11:
608
- _preloadComponent2 = _context.sent;
609
- route.component = _preloadComponent2.default || _preloadComponent2;
610
-
611
- case 13:
612
- if (!route.routes) {
613
- _context.next = 17;
614
- break;
615
- }
616
-
617
- _context.next = 16;
618
- return preloadComponent(route.routes, pathname);
619
-
620
- case 16:
621
- route.routes = _context.sent;
622
-
623
- case 17:
624
- _context.next = 5;
625
- break;
626
-
627
- case 19:
628
- _context.next = 24;
629
- break;
630
-
631
- case 21:
632
- _context.prev = 21;
633
- _context.t0 = _context["catch"](3);
634
-
635
- _iterator.e(_context.t0);
636
-
637
- case 24:
638
- _context.prev = 24;
639
-
640
- _iterator.f();
641
-
642
- return _context.finish(24);
643
-
644
- case 27:
645
- return _context.abrupt("return", readyRoutes);
646
-
647
- case 28:
648
- case "end":
649
- return _context.stop();
650
- }
651
- }
652
- }, _callee, null, [[3, 21, 24, 27]]);
653
- }));
654
- return _preloadComponent.apply(this, arguments);
655
- }
656
-
657
- function renderClient(opts) {
658
- var rootContainer = opts.plugin.applyPlugins({
659
- type: runtime.ApplyPluginsType.modify,
660
- key: 'rootContainer',
661
- initialValue: /*#__PURE__*/React__default['default'].createElement(RouterComponent, {
662
- history: opts.history,
663
- routes: opts.routes,
664
- plugin: opts.plugin,
665
- ssrProps: opts.ssrProps,
666
- defaultTitle: opts.defaultTitle
667
- }),
668
- args: {
669
- history: opts.history,
670
- routes: opts.routes,
671
- plugin: opts.plugin
672
- }
673
- });
674
-
675
- if (opts.rootElement) {
676
- var rootElement = typeof opts.rootElement === 'string' ? document.getElementById(opts.rootElement) : opts.rootElement;
677
-
678
- var callback = opts.callback || function () {}; // flag showing SSR successed
679
-
680
-
681
- if (window.g_useSSR) {
682
- if (opts.dynamicImport) {
683
- // dynamicImport should preload current route component
684
- // first loades);
685
- preloadComponent(opts.routes).then(function () {
686
- reactDom.hydrate(rootContainer, rootElement, callback);
687
- });
688
- } else {
689
- reactDom.hydrate(rootContainer, rootElement, callback);
690
- }
691
- } else {
692
- reactDom.render(rootContainer, rootElement, callback);
693
- }
694
- } else {
695
- return rootContainer;
696
- }
697
- }
698
-
699
- exports.renderClient = renderClient;
700
- exports.renderRoutes = renderRoutes;
1
+ export { Link, Outlet } from 'react-router-dom';
2
+ export * from './browser';
@@ -0,0 +1,21 @@
1
+ /// <reference types="react" />
2
+ import { IRoute, IRoutesById } from './types';
3
+ export declare function createClientRoutes(opts: {
4
+ routesById: IRoutesById;
5
+ parentId?: string;
6
+ Component: any;
7
+ }): {
8
+ id: string;
9
+ path: string | undefined;
10
+ index: boolean | undefined;
11
+ element: JSX.Element;
12
+ }[];
13
+ export declare function createClientRoute(opts: {
14
+ route: IRoute;
15
+ Component: any;
16
+ }): {
17
+ id: string;
18
+ path: string | undefined;
19
+ index: boolean | undefined;
20
+ element: JSX.Element;
21
+ };