| 1 | function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|---|
| 2 | function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|---|
| 3 | function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|---|
| 4 | function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
|
|---|
| 5 | function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|---|
| 6 | // The error overlay is inspired (and mostly copied) from Create React App (https://github.com/facebookincubator/create-react-app)
|
|---|
| 7 | // They, in turn, got inspired by webpack-hot-middleware (https://github.com/glenjamin/webpack-hot-middleware).
|
|---|
| 8 |
|
|---|
| 9 | import ansiHTML from "ansi-html-community";
|
|---|
| 10 | import { encode } from "html-entities";
|
|---|
| 11 | import { listenToRuntimeError, listenToUnhandledRejection, parseErrorToStacks } from "./overlay/runtime-error.js";
|
|---|
| 12 | import createOverlayMachine from "./overlay/state-machine.js";
|
|---|
| 13 | import { containerStyle, dismissButtonStyle, headerStyle, iframeStyle, msgStyles, msgTextStyle, msgTypeStyle } from "./overlay/styles.js";
|
|---|
| 14 | var colors = {
|
|---|
| 15 | reset: ["transparent", "transparent"],
|
|---|
| 16 | black: "181818",
|
|---|
| 17 | red: "E36049",
|
|---|
| 18 | green: "B3CB74",
|
|---|
| 19 | yellow: "FFD080",
|
|---|
| 20 | blue: "7CAFC2",
|
|---|
| 21 | magenta: "7FACCA",
|
|---|
| 22 | cyan: "C3C2EF",
|
|---|
| 23 | lightgrey: "EBE7E3",
|
|---|
| 24 | darkgrey: "6D7891"
|
|---|
| 25 | };
|
|---|
| 26 | ansiHTML.setColors(colors);
|
|---|
| 27 |
|
|---|
| 28 | /**
|
|---|
| 29 | * @param {string} type
|
|---|
| 30 | * @param {string | { file?: string, moduleName?: string, loc?: string, message?: string; stack?: string[] }} item
|
|---|
| 31 | * @returns {{ header: string, body: string }}
|
|---|
| 32 | */
|
|---|
| 33 | function formatProblem(type, item) {
|
|---|
| 34 | var header = type === "warning" ? "WARNING" : "ERROR";
|
|---|
| 35 | var body = "";
|
|---|
| 36 | if (typeof item === "string") {
|
|---|
| 37 | body += item;
|
|---|
| 38 | } else {
|
|---|
| 39 | var file = item.file || "";
|
|---|
| 40 | // eslint-disable-next-line no-nested-ternary
|
|---|
| 41 | var moduleName = item.moduleName ? item.moduleName.indexOf("!") !== -1 ? "".concat(item.moduleName.replace(/^(\s|\S)*!/, ""), " (").concat(item.moduleName, ")") : "".concat(item.moduleName) : "";
|
|---|
| 42 | var loc = item.loc;
|
|---|
| 43 | header += "".concat(moduleName || file ? " in ".concat(moduleName ? "".concat(moduleName).concat(file ? " (".concat(file, ")") : "") : file).concat(loc ? " ".concat(loc) : "") : "");
|
|---|
| 44 | body += item.message || "";
|
|---|
| 45 | }
|
|---|
| 46 | if (Array.isArray(item.stack)) {
|
|---|
| 47 | item.stack.forEach(function (stack) {
|
|---|
| 48 | if (typeof stack === "string") {
|
|---|
| 49 | body += "\r\n".concat(stack);
|
|---|
| 50 | }
|
|---|
| 51 | });
|
|---|
| 52 | }
|
|---|
| 53 | return {
|
|---|
| 54 | header: header,
|
|---|
| 55 | body: body
|
|---|
| 56 | };
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | /**
|
|---|
| 60 | * @typedef {Object} CreateOverlayOptions
|
|---|
| 61 | * @property {string | null} trustedTypesPolicyName
|
|---|
| 62 | * @property {boolean | (error: Error) => void} [catchRuntimeError]
|
|---|
| 63 | */
|
|---|
| 64 |
|
|---|
| 65 | /**
|
|---|
| 66 | *
|
|---|
| 67 | * @param {CreateOverlayOptions} options
|
|---|
| 68 | */
|
|---|
| 69 | var createOverlay = function createOverlay(options) {
|
|---|
| 70 | /** @type {HTMLIFrameElement | null | undefined} */
|
|---|
| 71 | var iframeContainerElement;
|
|---|
| 72 | /** @type {HTMLDivElement | null | undefined} */
|
|---|
| 73 | var containerElement;
|
|---|
| 74 | /** @type {HTMLDivElement | null | undefined} */
|
|---|
| 75 | var headerElement;
|
|---|
| 76 | /** @type {Array<(element: HTMLDivElement) => void>} */
|
|---|
| 77 | var onLoadQueue = [];
|
|---|
| 78 | /** @type {TrustedTypePolicy | undefined} */
|
|---|
| 79 | var overlayTrustedTypesPolicy;
|
|---|
| 80 |
|
|---|
| 81 | /**
|
|---|
| 82 | *
|
|---|
| 83 | * @param {HTMLElement} element
|
|---|
| 84 | * @param {CSSStyleDeclaration} style
|
|---|
| 85 | */
|
|---|
| 86 | function applyStyle(element, style) {
|
|---|
| 87 | Object.keys(style).forEach(function (prop) {
|
|---|
| 88 | element.style[prop] = style[prop];
|
|---|
| 89 | });
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | /**
|
|---|
| 93 | * @param {string | null} trustedTypesPolicyName
|
|---|
| 94 | */
|
|---|
| 95 | function createContainer(trustedTypesPolicyName) {
|
|---|
| 96 | // Enable Trusted Types if they are available in the current browser.
|
|---|
| 97 | if (window.trustedTypes) {
|
|---|
| 98 | overlayTrustedTypesPolicy = window.trustedTypes.createPolicy(trustedTypesPolicyName || "webpack-dev-server#overlay", {
|
|---|
| 99 | createHTML: function createHTML(value) {
|
|---|
| 100 | return value;
|
|---|
| 101 | }
|
|---|
| 102 | });
|
|---|
| 103 | }
|
|---|
| 104 | iframeContainerElement = document.createElement("iframe");
|
|---|
| 105 | iframeContainerElement.id = "webpack-dev-server-client-overlay";
|
|---|
| 106 | iframeContainerElement.src = "about:blank";
|
|---|
| 107 | applyStyle(iframeContainerElement, iframeStyle);
|
|---|
| 108 | iframeContainerElement.onload = function () {
|
|---|
| 109 | var contentElement = /** @type {Document} */
|
|---|
| 110 | /** @type {HTMLIFrameElement} */
|
|---|
| 111 | iframeContainerElement.contentDocument.createElement("div");
|
|---|
| 112 | containerElement = /** @type {Document} */
|
|---|
| 113 | /** @type {HTMLIFrameElement} */
|
|---|
| 114 | iframeContainerElement.contentDocument.createElement("div");
|
|---|
| 115 | contentElement.id = "webpack-dev-server-client-overlay-div";
|
|---|
| 116 | applyStyle(contentElement, containerStyle);
|
|---|
| 117 | headerElement = document.createElement("div");
|
|---|
| 118 | headerElement.innerText = "Compiled with problems:";
|
|---|
| 119 | applyStyle(headerElement, headerStyle);
|
|---|
| 120 | var closeButtonElement = document.createElement("button");
|
|---|
| 121 | applyStyle(closeButtonElement, dismissButtonStyle);
|
|---|
| 122 | closeButtonElement.innerText = "×";
|
|---|
| 123 | closeButtonElement.ariaLabel = "Dismiss";
|
|---|
| 124 | closeButtonElement.addEventListener("click", function () {
|
|---|
| 125 | // eslint-disable-next-line no-use-before-define
|
|---|
| 126 | overlayService.send({
|
|---|
| 127 | type: "DISMISS"
|
|---|
| 128 | });
|
|---|
| 129 | });
|
|---|
| 130 | contentElement.appendChild(headerElement);
|
|---|
| 131 | contentElement.appendChild(closeButtonElement);
|
|---|
| 132 | contentElement.appendChild(containerElement);
|
|---|
| 133 |
|
|---|
| 134 | /** @type {Document} */
|
|---|
| 135 | /** @type {HTMLIFrameElement} */
|
|---|
| 136 | iframeContainerElement.contentDocument.body.appendChild(contentElement);
|
|---|
| 137 | onLoadQueue.forEach(function (onLoad) {
|
|---|
| 138 | onLoad( /** @type {HTMLDivElement} */contentElement);
|
|---|
| 139 | });
|
|---|
| 140 | onLoadQueue = [];
|
|---|
| 141 |
|
|---|
| 142 | /** @type {HTMLIFrameElement} */
|
|---|
| 143 | iframeContainerElement.onload = null;
|
|---|
| 144 | };
|
|---|
| 145 | document.body.appendChild(iframeContainerElement);
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | /**
|
|---|
| 149 | * @param {(element: HTMLDivElement) => void} callback
|
|---|
| 150 | * @param {string | null} trustedTypesPolicyName
|
|---|
| 151 | */
|
|---|
| 152 | function ensureOverlayExists(callback, trustedTypesPolicyName) {
|
|---|
| 153 | if (containerElement) {
|
|---|
| 154 | containerElement.innerHTML = "";
|
|---|
| 155 | // Everything is ready, call the callback right away.
|
|---|
| 156 | callback(containerElement);
|
|---|
| 157 | return;
|
|---|
| 158 | }
|
|---|
| 159 | onLoadQueue.push(callback);
|
|---|
| 160 | if (iframeContainerElement) {
|
|---|
| 161 | return;
|
|---|
| 162 | }
|
|---|
| 163 | createContainer(trustedTypesPolicyName);
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | // Successful compilation.
|
|---|
| 167 | function hide() {
|
|---|
| 168 | if (!iframeContainerElement) {
|
|---|
| 169 | return;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | // Clean up and reset internal state.
|
|---|
| 173 | document.body.removeChild(iframeContainerElement);
|
|---|
| 174 | iframeContainerElement = null;
|
|---|
| 175 | containerElement = null;
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | // Compilation with errors (e.g. syntax error or missing modules).
|
|---|
| 179 | /**
|
|---|
| 180 | * @param {string} type
|
|---|
| 181 | * @param {Array<string | { moduleIdentifier?: string, moduleName?: string, loc?: string, message?: string }>} messages
|
|---|
| 182 | * @param {string | null} trustedTypesPolicyName
|
|---|
| 183 | * @param {'build' | 'runtime'} messageSource
|
|---|
| 184 | */
|
|---|
| 185 | function show(type, messages, trustedTypesPolicyName, messageSource) {
|
|---|
| 186 | ensureOverlayExists(function () {
|
|---|
| 187 | headerElement.innerText = messageSource === "runtime" ? "Uncaught runtime errors:" : "Compiled with problems:";
|
|---|
| 188 | messages.forEach(function (message) {
|
|---|
| 189 | var entryElement = document.createElement("div");
|
|---|
| 190 | var msgStyle = type === "warning" ? msgStyles.warning : msgStyles.error;
|
|---|
| 191 | applyStyle(entryElement, _objectSpread(_objectSpread({}, msgStyle), {}, {
|
|---|
| 192 | padding: "1rem 1rem 1.5rem 1rem"
|
|---|
| 193 | }));
|
|---|
| 194 | var typeElement = document.createElement("div");
|
|---|
| 195 | var _formatProblem = formatProblem(type, message),
|
|---|
| 196 | header = _formatProblem.header,
|
|---|
| 197 | body = _formatProblem.body;
|
|---|
| 198 | typeElement.innerText = header;
|
|---|
| 199 | applyStyle(typeElement, msgTypeStyle);
|
|---|
| 200 | if (message.moduleIdentifier) {
|
|---|
| 201 | applyStyle(typeElement, {
|
|---|
| 202 | cursor: "pointer"
|
|---|
| 203 | });
|
|---|
| 204 | // element.dataset not supported in IE
|
|---|
| 205 | typeElement.setAttribute("data-can-open", true);
|
|---|
| 206 | typeElement.addEventListener("click", function () {
|
|---|
| 207 | fetch("/webpack-dev-server/open-editor?fileName=".concat(message.moduleIdentifier));
|
|---|
| 208 | });
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | // Make it look similar to our terminal.
|
|---|
| 212 | var text = ansiHTML(encode(body));
|
|---|
| 213 | var messageTextNode = document.createElement("div");
|
|---|
| 214 | applyStyle(messageTextNode, msgTextStyle);
|
|---|
| 215 | messageTextNode.innerHTML = overlayTrustedTypesPolicy ? overlayTrustedTypesPolicy.createHTML(text) : text;
|
|---|
| 216 | entryElement.appendChild(typeElement);
|
|---|
| 217 | entryElement.appendChild(messageTextNode);
|
|---|
| 218 |
|
|---|
| 219 | /** @type {HTMLDivElement} */
|
|---|
| 220 | containerElement.appendChild(entryElement);
|
|---|
| 221 | });
|
|---|
| 222 | }, trustedTypesPolicyName);
|
|---|
| 223 | }
|
|---|
| 224 | var overlayService = createOverlayMachine({
|
|---|
| 225 | showOverlay: function showOverlay(_ref) {
|
|---|
| 226 | var _ref$level = _ref.level,
|
|---|
| 227 | level = _ref$level === void 0 ? "error" : _ref$level,
|
|---|
| 228 | messages = _ref.messages,
|
|---|
| 229 | messageSource = _ref.messageSource;
|
|---|
| 230 | return show(level, messages, options.trustedTypesPolicyName, messageSource);
|
|---|
| 231 | },
|
|---|
| 232 | hideOverlay: hide
|
|---|
| 233 | });
|
|---|
| 234 | if (options.catchRuntimeError) {
|
|---|
| 235 | /**
|
|---|
| 236 | * @param {Error | undefined} error
|
|---|
| 237 | * @param {string} fallbackMessage
|
|---|
| 238 | */
|
|---|
| 239 | var handleError = function handleError(error, fallbackMessage) {
|
|---|
| 240 | var errorObject = error instanceof Error ? error : new Error(error || fallbackMessage);
|
|---|
| 241 | var shouldDisplay = typeof options.catchRuntimeError === "function" ? options.catchRuntimeError(errorObject) : true;
|
|---|
| 242 | if (shouldDisplay) {
|
|---|
| 243 | overlayService.send({
|
|---|
| 244 | type: "RUNTIME_ERROR",
|
|---|
| 245 | messages: [{
|
|---|
| 246 | message: errorObject.message,
|
|---|
| 247 | stack: parseErrorToStacks(errorObject)
|
|---|
| 248 | }]
|
|---|
| 249 | });
|
|---|
| 250 | }
|
|---|
| 251 | };
|
|---|
| 252 | listenToRuntimeError(function (errorEvent) {
|
|---|
| 253 | // error property may be empty in older browser like IE
|
|---|
| 254 | var error = errorEvent.error,
|
|---|
| 255 | message = errorEvent.message;
|
|---|
| 256 | if (!error && !message) {
|
|---|
| 257 | return;
|
|---|
| 258 | }
|
|---|
| 259 | handleError(error, message);
|
|---|
| 260 | });
|
|---|
| 261 | listenToUnhandledRejection(function (promiseRejectionEvent) {
|
|---|
| 262 | var reason = promiseRejectionEvent.reason;
|
|---|
| 263 | handleError(reason, "Unknown promise rejection reason");
|
|---|
| 264 | });
|
|---|
| 265 | }
|
|---|
| 266 | return overlayService;
|
|---|
| 267 | };
|
|---|
| 268 | export { formatProblem, createOverlay }; |
|---|