ssr-window.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * SSR Window 2.0.0
  3. * Better handling for window object in SSR environment
  4. * https://github.com/nolimits4web/ssr-window
  5. *
  6. * Copyright 2020, Vladimir Kharlampidi
  7. *
  8. * Licensed under MIT
  9. *
  10. * Released on: May 12, 2020
  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 = 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. if (target === void 0) { target = {}; }
  26. if (src === void 0) { src = {}; }
  27. Object.keys(src).forEach(function (key) {
  28. if (typeof target[key] === 'undefined')
  29. target[key] = src[key];
  30. else if (isObject(src[key]) &&
  31. isObject(target[key]) &&
  32. Object.keys(src[key]).length > 0) {
  33. extend(target[key], src[key]);
  34. }
  35. });
  36. }
  37. var doc = typeof document !== 'undefined' ? document : {};
  38. var ssrDocument = {
  39. body: {},
  40. addEventListener: function () { },
  41. removeEventListener: function () { },
  42. activeElement: {
  43. blur: function () { },
  44. nodeName: '',
  45. },
  46. querySelector: function () {
  47. return null;
  48. },
  49. querySelectorAll: function () {
  50. return [];
  51. },
  52. getElementById: function () {
  53. return null;
  54. },
  55. createEvent: function () {
  56. return {
  57. initEvent: function () { },
  58. };
  59. },
  60. createElement: function () {
  61. return {
  62. children: [],
  63. childNodes: [],
  64. style: {},
  65. setAttribute: function () { },
  66. getElementsByTagName: function () {
  67. return [];
  68. },
  69. };
  70. },
  71. createElementNS: function () {
  72. return {};
  73. },
  74. importNode: function () {
  75. return null;
  76. },
  77. location: {
  78. hash: '',
  79. host: '',
  80. hostname: '',
  81. href: '',
  82. origin: '',
  83. pathname: '',
  84. protocol: '',
  85. search: '',
  86. },
  87. };
  88. extend(doc, ssrDocument);
  89. var win = typeof window !== 'undefined' ? window : {};
  90. var 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: function () { },
  107. pushState: function () { },
  108. go: function () { },
  109. back: function () { },
  110. },
  111. CustomEvent: function CustomEvent() {
  112. return this;
  113. },
  114. addEventListener: function () { },
  115. removeEventListener: function () { },
  116. getComputedStyle: function () {
  117. return {
  118. getPropertyValue: function () {
  119. return '';
  120. },
  121. };
  122. },
  123. Image: function () { },
  124. Date: function () { },
  125. screen: {},
  126. setTimeout: function () { },
  127. clearTimeout: function () { },
  128. matchMedia: function () {
  129. return {};
  130. },
  131. };
  132. extend(win, ssrWindow);
  133. exports.document = doc;
  134. exports.extend = extend;
  135. exports.window = win;
  136. Object.defineProperty(exports, '__esModule', { value: true });
  137. })));
  138. //# sourceMappingURL=ssr-window.js.map