render.js 3.6 KB

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