ssr-window.umd.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. (function (global, factory) {
  13. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  14. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  15. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.ssrWindow = {}));
  16. }(this, (function (exports) { 'use strict';
  17. /* eslint-disable no-param-reassign */
  18. function isObject(obj) {
  19. return (obj !== null &&
  20. typeof obj === 'object' &&
  21. 'constructor' in obj &&
  22. obj.constructor === Object);
  23. }
  24. function extend(target = {}, src = {}) {
  25. Object.keys(src).forEach((key) => {
  26. if (typeof target[key] === 'undefined')
  27. target[key] = src[key];
  28. else if (isObject(src[key]) &&
  29. isObject(target[key]) &&
  30. Object.keys(src[key]).length > 0) {
  31. extend(target[key], src[key]);
  32. }
  33. });
  34. }
  35. const ssrDocument = {
  36. body: {},
  37. addEventListener() { },
  38. removeEventListener() { },
  39. activeElement: {
  40. blur() { },
  41. nodeName: '',
  42. },
  43. querySelector() {
  44. return null;
  45. },
  46. querySelectorAll() {
  47. return [];
  48. },
  49. getElementById() {
  50. return null;
  51. },
  52. createEvent() {
  53. return {
  54. initEvent() { },
  55. };
  56. },
  57. createElement() {
  58. return {
  59. children: [],
  60. childNodes: [],
  61. style: {},
  62. setAttribute() { },
  63. getElementsByTagName() {
  64. return [];
  65. },
  66. };
  67. },
  68. createElementNS() {
  69. return {};
  70. },
  71. importNode() {
  72. return null;
  73. },
  74. location: {
  75. hash: '',
  76. host: '',
  77. hostname: '',
  78. href: '',
  79. origin: '',
  80. pathname: '',
  81. protocol: '',
  82. search: '',
  83. },
  84. };
  85. function getDocument() {
  86. const doc = typeof document !== 'undefined' ? document : {};
  87. extend(doc, ssrDocument);
  88. return doc;
  89. }
  90. const ssrWindow = {
  91. document: ssrDocument,
  92. navigator: {
  93. userAgent: '',
  94. },
  95. location: {
  96. hash: '',
  97. host: '',
  98. hostname: '',
  99. href: '',
  100. origin: '',
  101. pathname: '',
  102. protocol: '',
  103. search: '',
  104. },
  105. history: {
  106. replaceState() { },
  107. pushState() { },
  108. go() { },
  109. back() { },
  110. },
  111. CustomEvent: function CustomEvent() {
  112. return this;
  113. },
  114. addEventListener() { },
  115. removeEventListener() { },
  116. getComputedStyle() {
  117. return {
  118. getPropertyValue() {
  119. return '';
  120. },
  121. };
  122. },
  123. Image() { },
  124. Date() { },
  125. screen: {},
  126. setTimeout() { },
  127. clearTimeout() { },
  128. matchMedia() {
  129. return {};
  130. },
  131. requestAnimationFrame(callback) {
  132. if (typeof setTimeout === 'undefined') {
  133. callback();
  134. return null;
  135. }
  136. return setTimeout(callback, 0);
  137. },
  138. cancelAnimationFrame(id) {
  139. if (typeof setTimeout === 'undefined') {
  140. return;
  141. }
  142. clearTimeout(id);
  143. },
  144. };
  145. function getWindow() {
  146. const win = typeof window !== 'undefined' ? window : {};
  147. extend(win, ssrWindow);
  148. return win;
  149. }
  150. exports.extend = extend;
  151. exports.getDocument = getDocument;
  152. exports.getWindow = getWindow;
  153. exports.ssrDocument = ssrDocument;
  154. exports.ssrWindow = ssrWindow;
  155. Object.defineProperty(exports, '__esModule', { value: true });
  156. })));
  157. //# sourceMappingURL=ssr-window.umd.js.map