ssr-window.esm.mjs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * SSR Window 4.0.2
  3. * Better handling for window object in SSR environment
  4. * https://github.com/nolimits4web/ssr-window
  5. *
  6. * Copyright 2021, Vladimir Kharlampidi
  7. *
  8. * Licensed under MIT
  9. *
  10. * Released on: December 13, 2021
  11. */
  12. /* eslint-disable no-param-reassign */
  13. function isObject(obj) {
  14. return obj !== null && typeof obj === 'object' && 'constructor' in obj && obj.constructor === Object;
  15. }
  16. function extend(target, src) {
  17. if (target === void 0) {
  18. target = {};
  19. }
  20. if (src === void 0) {
  21. src = {};
  22. }
  23. Object.keys(src).forEach(key => {
  24. if (typeof target[key] === 'undefined') target[key] = src[key];else if (isObject(src[key]) && isObject(target[key]) && Object.keys(src[key]).length > 0) {
  25. extend(target[key], src[key]);
  26. }
  27. });
  28. }
  29. const ssrDocument = {
  30. body: {},
  31. addEventListener() {},
  32. removeEventListener() {},
  33. activeElement: {
  34. blur() {},
  35. nodeName: ''
  36. },
  37. querySelector() {
  38. return null;
  39. },
  40. querySelectorAll() {
  41. return [];
  42. },
  43. getElementById() {
  44. return null;
  45. },
  46. createEvent() {
  47. return {
  48. initEvent() {}
  49. };
  50. },
  51. createElement() {
  52. return {
  53. children: [],
  54. childNodes: [],
  55. style: {},
  56. setAttribute() {},
  57. getElementsByTagName() {
  58. return [];
  59. }
  60. };
  61. },
  62. createElementNS() {
  63. return {};
  64. },
  65. importNode() {
  66. return null;
  67. },
  68. location: {
  69. hash: '',
  70. host: '',
  71. hostname: '',
  72. href: '',
  73. origin: '',
  74. pathname: '',
  75. protocol: '',
  76. search: ''
  77. }
  78. };
  79. function getDocument() {
  80. const doc = typeof document !== 'undefined' ? document : {};
  81. extend(doc, ssrDocument);
  82. return doc;
  83. }
  84. const ssrWindow = {
  85. document: ssrDocument,
  86. navigator: {
  87. userAgent: ''
  88. },
  89. location: {
  90. hash: '',
  91. host: '',
  92. hostname: '',
  93. href: '',
  94. origin: '',
  95. pathname: '',
  96. protocol: '',
  97. search: ''
  98. },
  99. history: {
  100. replaceState() {},
  101. pushState() {},
  102. go() {},
  103. back() {}
  104. },
  105. CustomEvent: function CustomEvent() {
  106. return this;
  107. },
  108. addEventListener() {},
  109. removeEventListener() {},
  110. getComputedStyle() {
  111. return {
  112. getPropertyValue() {
  113. return '';
  114. }
  115. };
  116. },
  117. Image() {},
  118. Date() {},
  119. screen: {},
  120. setTimeout() {},
  121. clearTimeout() {},
  122. matchMedia() {
  123. return {};
  124. },
  125. requestAnimationFrame(callback) {
  126. if (typeof setTimeout === 'undefined') {
  127. callback();
  128. return null;
  129. }
  130. return setTimeout(callback, 0);
  131. },
  132. cancelAnimationFrame(id) {
  133. if (typeof setTimeout === 'undefined') {
  134. return;
  135. }
  136. clearTimeout(id);
  137. }
  138. };
  139. function getWindow() {
  140. const win = typeof window !== 'undefined' ? window : {};
  141. extend(win, ssrWindow);
  142. return win;
  143. }
  144. export { getWindow as a, getDocument as g };