overlay.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. 'use strict';
  2. // The error overlay is inspired (and mostly copied) from Create React App (https://github.com/facebookincubator/create-react-app)
  3. // They, in turn, got inspired by webpack-hot-middleware (https://github.com/glenjamin/webpack-hot-middleware).
  4. var ansiHTML = require('ansi-html');
  5. var Entities = require('html-entities').AllHtmlEntities;
  6. var entities = new Entities();
  7. var colors = {
  8. reset: ['transparent', 'transparent'],
  9. black: '181818',
  10. red: 'E36049',
  11. green: 'B3CB74',
  12. yellow: 'FFD080',
  13. blue: '7CAFC2',
  14. magenta: '7FACCA',
  15. cyan: 'C3C2EF',
  16. lightgrey: 'EBE7E3',
  17. darkgrey: '6D7891'
  18. };
  19. ansiHTML.setColors(colors);
  20. function createOverlayIframe(onIframeLoad) {
  21. var iframe = document.createElement('iframe');
  22. iframe.id = 'webpack-dev-server-client-overlay';
  23. iframe.src = 'about:blank';
  24. iframe.style.position = 'fixed';
  25. iframe.style.left = 0;
  26. iframe.style.top = 0;
  27. iframe.style.right = 0;
  28. iframe.style.bottom = 0;
  29. iframe.style.width = '100vw';
  30. iframe.style.height = '100vh';
  31. iframe.style.border = 'none';
  32. iframe.style.zIndex = 9999999999;
  33. iframe.onload = onIframeLoad;
  34. return iframe;
  35. }
  36. function addOverlayDivTo(iframe) {
  37. var div = iframe.contentDocument.createElement('div');
  38. div.id = 'webpack-dev-server-client-overlay-div';
  39. div.style.position = 'fixed';
  40. div.style.boxSizing = 'border-box';
  41. div.style.left = 0;
  42. div.style.top = 0;
  43. div.style.right = 0;
  44. div.style.bottom = 0;
  45. div.style.width = '100vw';
  46. div.style.height = '100vh';
  47. div.style.backgroundColor = 'rgba(0, 0, 0, 0.85)';
  48. div.style.color = '#E8E8E8';
  49. div.style.fontFamily = 'Menlo, Consolas, monospace';
  50. div.style.fontSize = 'large';
  51. div.style.padding = '2rem';
  52. div.style.lineHeight = '1.2';
  53. div.style.whiteSpace = 'pre-wrap';
  54. div.style.overflow = 'auto';
  55. iframe.contentDocument.body.appendChild(div);
  56. return div;
  57. }
  58. var overlayIframe = null;
  59. var overlayDiv = null;
  60. var lastOnOverlayDivReady = null;
  61. function ensureOverlayDivExists(onOverlayDivReady) {
  62. if (overlayDiv) {
  63. // Everything is ready, call the callback right away.
  64. onOverlayDivReady(overlayDiv);
  65. return;
  66. }
  67. // Creating an iframe may be asynchronous so we'll schedule the callback.
  68. // In case of multiple calls, last callback wins.
  69. lastOnOverlayDivReady = onOverlayDivReady;
  70. if (overlayIframe) {
  71. // We're already creating it.
  72. return;
  73. }
  74. // Create iframe and, when it is ready, a div inside it.
  75. overlayIframe = createOverlayIframe(function () {
  76. overlayDiv = addOverlayDivTo(overlayIframe);
  77. // Now we can talk!
  78. lastOnOverlayDivReady(overlayDiv);
  79. });
  80. // Zalgo alert: onIframeLoad() will be called either synchronously
  81. // or asynchronously depending on the browser.
  82. // We delay adding it so `overlayIframe` is set when `onIframeLoad` fires.
  83. document.body.appendChild(overlayIframe);
  84. }
  85. function showMessageOverlay(message) {
  86. ensureOverlayDivExists(function (div) {
  87. // Make it look similar to our terminal.
  88. div.innerHTML = '<span style="color: #' + colors.red + '">Failed to compile.</span><br><br>' + ansiHTML(entities.encode(message));
  89. });
  90. }
  91. function destroyErrorOverlay() {
  92. if (!overlayDiv) {
  93. // It is not there in the first place.
  94. return;
  95. }
  96. // Clean up and reset internal state.
  97. document.body.removeChild(overlayIframe);
  98. overlayDiv = null;
  99. overlayIframe = null;
  100. lastOnOverlayDivReady = null;
  101. }
  102. // Successful compilation.
  103. exports.clear = function handleSuccess() {
  104. destroyErrorOverlay();
  105. };
  106. // Compilation with errors (e.g. syntax error or missing modules).
  107. exports.showMessage = function handleMessage(messages) {
  108. showMessageOverlay(messages[0]);
  109. };