dev-build-watcher.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = initializeBuildWatcher;
  6. var _websocket = require("./error-overlay/websocket");
  7. function initializeBuildWatcher(toggleCallback, position = 'bottom-right') {
  8. const shadowHost = document.createElement('div');
  9. const [verticalProperty, horizontalProperty] = position.split('-');
  10. shadowHost.id = '__next-build-watcher';
  11. // Make sure container is fixed and on a high zIndex so it shows
  12. shadowHost.style.position = 'fixed';
  13. // Ensure container's position to be top or bottom (default)
  14. shadowHost.style[verticalProperty] = '10px';
  15. // Ensure container's position to be left or right (default)
  16. shadowHost.style[horizontalProperty] = '20px';
  17. shadowHost.style.width = 0;
  18. shadowHost.style.height = 0;
  19. shadowHost.style.zIndex = 99999;
  20. document.body.appendChild(shadowHost);
  21. let shadowRoot;
  22. let prefix = '';
  23. if (shadowHost.attachShadow) {
  24. shadowRoot = shadowHost.attachShadow({
  25. mode: 'open'
  26. });
  27. } else {
  28. // If attachShadow is undefined then the browser does not support
  29. // the Shadow DOM, we need to prefix all the names so there
  30. // will be no conflicts
  31. shadowRoot = shadowHost;
  32. prefix = '__next-build-watcher-';
  33. }
  34. // Container
  35. const container = createContainer(prefix);
  36. shadowRoot.appendChild(container);
  37. // CSS
  38. const css = createCss(prefix, {
  39. horizontalProperty,
  40. verticalProperty
  41. });
  42. shadowRoot.appendChild(css);
  43. // State
  44. let isVisible = false;
  45. let isBuilding = false;
  46. let timeoutId = null;
  47. // Handle events
  48. (0, _websocket).addMessageListener((event)=>{
  49. // This is the heartbeat event
  50. if (event.data === '\uD83D\uDC93') {
  51. return;
  52. }
  53. try {
  54. handleMessage(event);
  55. } catch (e) {}
  56. });
  57. function handleMessage(event) {
  58. const obj = typeof event === 'string' ? {
  59. action: event
  60. } : JSON.parse(event.data);
  61. // eslint-disable-next-line default-case
  62. switch(obj.action){
  63. case 'building':
  64. timeoutId && clearTimeout(timeoutId);
  65. isVisible = true;
  66. isBuilding = true;
  67. updateContainer();
  68. break;
  69. case 'built':
  70. case 'sync':
  71. isBuilding = false;
  72. // Wait for the fade out transition to complete
  73. timeoutId = setTimeout(()=>{
  74. isVisible = false;
  75. updateContainer();
  76. }, 100);
  77. updateContainer();
  78. break;
  79. }
  80. }
  81. toggleCallback(handleMessage);
  82. function updateContainer() {
  83. if (isBuilding) {
  84. container.classList.add(`${prefix}building`);
  85. } else {
  86. container.classList.remove(`${prefix}building`);
  87. }
  88. if (isVisible) {
  89. container.classList.add(`${prefix}visible`);
  90. } else {
  91. container.classList.remove(`${prefix}visible`);
  92. }
  93. }
  94. }
  95. function createContainer(prefix) {
  96. const container = document.createElement('div');
  97. container.id = `${prefix}container`;
  98. container.innerHTML = `
  99. <div id="${prefix}icon-wrapper">
  100. <svg viewBox="0 0 226 200">
  101. <defs>
  102. <linearGradient
  103. x1="114.720775%"
  104. y1="181.283245%"
  105. x2="39.5399306%"
  106. y2="100%"
  107. id="${prefix}linear-gradient"
  108. >
  109. <stop stop-color="#000000" offset="0%" />
  110. <stop stop-color="#FFFFFF" offset="100%" />
  111. </linearGradient>
  112. </defs>
  113. <g id="${prefix}icon-group" fill="none" stroke="url(#${prefix}linear-gradient)" stroke-width="18">
  114. <path d="M113,5.08219117 L4.28393801,197.5 L221.716062,197.5 L113,5.08219117 Z" />
  115. </g>
  116. </svg>
  117. </div>
  118. `;
  119. return container;
  120. }
  121. function createCss(prefix, { horizontalProperty , verticalProperty }) {
  122. const css = document.createElement('style');
  123. css.textContent = `
  124. #${prefix}container {
  125. position: absolute;
  126. ${verticalProperty}: 10px;
  127. ${horizontalProperty}: 30px;
  128. border-radius: 3px;
  129. background: #000;
  130. color: #fff;
  131. font: initial;
  132. cursor: initial;
  133. letter-spacing: initial;
  134. text-shadow: initial;
  135. text-transform: initial;
  136. visibility: initial;
  137. padding: 7px 10px 8px 10px;
  138. align-items: center;
  139. box-shadow: 0 11px 40px 0 rgba(0, 0, 0, 0.25), 0 2px 10px 0 rgba(0, 0, 0, 0.12);
  140. display: none;
  141. opacity: 0;
  142. transition: opacity 0.1s ease, ${verticalProperty} 0.1s ease;
  143. animation: ${prefix}fade-in 0.1s ease-in-out;
  144. }
  145. #${prefix}container.${prefix}visible {
  146. display: flex;
  147. }
  148. #${prefix}container.${prefix}building {
  149. ${verticalProperty}: 20px;
  150. opacity: 1;
  151. }
  152. #${prefix}icon-wrapper {
  153. width: 16px;
  154. height: 16px;
  155. }
  156. #${prefix}icon-wrapper > svg {
  157. width: 100%;
  158. height: 100%;
  159. }
  160. #${prefix}icon-group {
  161. animation: ${prefix}strokedash 1s ease-in-out both infinite;
  162. }
  163. @keyframes ${prefix}fade-in {
  164. from {
  165. ${verticalProperty}: 10px;
  166. opacity: 0;
  167. }
  168. to {
  169. ${verticalProperty}: 20px;
  170. opacity: 1;
  171. }
  172. }
  173. @keyframes ${prefix}strokedash {
  174. 0% {
  175. stroke-dasharray: 0 226;
  176. }
  177. 80%,
  178. 100% {
  179. stroke-dasharray: 659 226;
  180. }
  181. }
  182. `;
  183. return css;
  184. }
  185. if ((typeof exports.default === 'function' || (typeof exports.default === 'object' && exports.default !== null)) && typeof exports.default.__esModule === 'undefined') {
  186. Object.defineProperty(exports.default, '__esModule', { value: true });
  187. Object.assign(exports.default, exports);
  188. module.exports = exports.default;
  189. }
  190. //# sourceMappingURL=dev-build-watcher.js.map