render.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. "use strict";
  2. var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
  3. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  4. Object.defineProperty(exports, "__esModule", {
  5. value: true
  6. });
  7. exports._r = _r;
  8. exports._u = _u;
  9. exports.render = render;
  10. exports.unmount = unmount;
  11. var _regeneratorRuntime2 = _interopRequireDefault(require("@babel/runtime/helpers/regeneratorRuntime"));
  12. var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
  13. var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
  14. var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
  15. var ReactDOM = _interopRequireWildcard(require("react-dom"));
  16. // Let compiler not to search module usage
  17. var fullClone = (0, _objectSpread2.default)({}, ReactDOM);
  18. var version = fullClone.version,
  19. reactRender = fullClone.render,
  20. unmountComponentAtNode = fullClone.unmountComponentAtNode;
  21. var createRoot;
  22. try {
  23. var mainVersion = Number((version || '').split('.')[0]);
  24. if (mainVersion >= 18) {
  25. createRoot = fullClone.createRoot;
  26. }
  27. } catch (e) {
  28. // Do nothing;
  29. }
  30. function toggleWarning(skip) {
  31. var __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED = fullClone.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
  32. if (__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED && (0, _typeof2.default)(__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED) === 'object') {
  33. __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.usingClientEntryPoint = skip;
  34. }
  35. }
  36. var MARK = '__rc_react_root__';
  37. // ========================== Render ==========================
  38. function modernRender(node, container) {
  39. toggleWarning(true);
  40. var root = container[MARK] || createRoot(container);
  41. toggleWarning(false);
  42. root.render(node);
  43. container[MARK] = root;
  44. }
  45. function legacyRender(node, container) {
  46. reactRender === null || reactRender === void 0 || reactRender(node, container);
  47. }
  48. /** @private Test usage. Not work in prod */
  49. function _r(node, container) {
  50. if (process.env.NODE_ENV !== 'production') {
  51. return legacyRender(node, container);
  52. }
  53. }
  54. function render(node, container) {
  55. if (createRoot) {
  56. modernRender(node, container);
  57. return;
  58. }
  59. legacyRender(node, container);
  60. }
  61. // ========================= Unmount ==========================
  62. function modernUnmount(_x) {
  63. return _modernUnmount.apply(this, arguments);
  64. }
  65. function _modernUnmount() {
  66. _modernUnmount = (0, _asyncToGenerator2.default)( /*#__PURE__*/(0, _regeneratorRuntime2.default)().mark(function _callee(container) {
  67. return (0, _regeneratorRuntime2.default)().wrap(function _callee$(_context) {
  68. while (1) switch (_context.prev = _context.next) {
  69. case 0:
  70. return _context.abrupt("return", Promise.resolve().then(function () {
  71. var _container$MARK;
  72. (_container$MARK = container[MARK]) === null || _container$MARK === void 0 || _container$MARK.unmount();
  73. delete container[MARK];
  74. }));
  75. case 1:
  76. case "end":
  77. return _context.stop();
  78. }
  79. }, _callee);
  80. }));
  81. return _modernUnmount.apply(this, arguments);
  82. }
  83. function legacyUnmount(container) {
  84. unmountComponentAtNode(container);
  85. }
  86. /** @private Test usage. Not work in prod */
  87. function _u(container) {
  88. if (process.env.NODE_ENV !== 'production') {
  89. return legacyUnmount(container);
  90. }
  91. }
  92. function unmount(_x2) {
  93. return _unmount.apply(this, arguments);
  94. }
  95. function _unmount() {
  96. _unmount = (0, _asyncToGenerator2.default)( /*#__PURE__*/(0, _regeneratorRuntime2.default)().mark(function _callee2(container) {
  97. return (0, _regeneratorRuntime2.default)().wrap(function _callee2$(_context2) {
  98. while (1) switch (_context2.prev = _context2.next) {
  99. case 0:
  100. if (!(createRoot !== undefined)) {
  101. _context2.next = 2;
  102. break;
  103. }
  104. return _context2.abrupt("return", modernUnmount(container));
  105. case 2:
  106. legacyUnmount(container);
  107. case 3:
  108. case "end":
  109. return _context2.stop();
  110. }
  111. }, _callee2);
  112. }));
  113. return _unmount.apply(this, arguments);
  114. }