webpack-dev-server 4.11.0 → 4.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,64 @@
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
+ /**
7
+ * @typedef {Object} StateDefinitions
8
+ * @property {{[event: string]: { target: string; actions?: Array<string> }}} [on]
9
+ */
10
+
11
+ /**
12
+ * @typedef {Object} Options
13
+ * @property {{[state: string]: StateDefinitions}} states
14
+ * @property {object} context;
15
+ * @property {string} initial
16
+ */
17
+
18
+ /**
19
+ * @typedef {Object} Implementation
20
+ * @property {{[actionName: string]: (ctx: object, event: any) => object}} actions
21
+ */
22
+
23
+ /**
24
+ * A simplified `createMachine` from `@xstate/fsm` with the following differences:
25
+ *
26
+ * - the returned machine is technically a "service". No `interpret(machine).start()` is needed.
27
+ * - the state definition only support `on` and target must be declared with { target: 'nextState', actions: [] } explicitly.
28
+ * - event passed to `send` must be an object with `type` property.
29
+ * - actions implementation will be [assign action](https://xstate.js.org/docs/guides/context.html#assign-action) if you return any value.
30
+ * Do not return anything if you just want to invoke side effect.
31
+ *
32
+ * The goal of this custom function is to avoid installing the entire `'xstate/fsm'` package, while enabling modeling using
33
+ * state machine. You can copy the first parameter into the editor at https://stately.ai/viz to visualize the state machine.
34
+ *
35
+ * @param {Options} options
36
+ * @param {Implementation} implementation
37
+ */
38
+ function createMachine(_ref, _ref2) {
39
+ var states = _ref.states,
40
+ context = _ref.context,
41
+ initial = _ref.initial;
42
+ var actions = _ref2.actions;
43
+ var currentState = initial;
44
+ var currentContext = context;
45
+ return {
46
+ send: function send(event) {
47
+ var currentStateOn = states[currentState].on;
48
+ var transitionConfig = currentStateOn && currentStateOn[event.type];
49
+ if (transitionConfig) {
50
+ currentState = transitionConfig.target;
51
+ if (transitionConfig.actions) {
52
+ transitionConfig.actions.forEach(function (actName) {
53
+ var actionImpl = actions[actName];
54
+ var nextContextValue = actionImpl && actionImpl(currentContext, event);
55
+ if (nextContextValue) {
56
+ currentContext = _objectSpread(_objectSpread({}, currentContext), nextContextValue);
57
+ }
58
+ });
59
+ }
60
+ }
61
+ }
62
+ };
63
+ }
64
+ export default createMachine;
@@ -0,0 +1,31 @@
1
+ /**
2
+ *
3
+ * @param {Error} error
4
+ */
5
+ function parseErrorToStacks(error) {
6
+ if (!error || !(error instanceof Error)) {
7
+ throw new Error("parseErrorToStacks expects Error object");
8
+ }
9
+ if (typeof error.stack === "string") {
10
+ return error.stack.split("\n").filter(function (stack) {
11
+ return stack !== "Error: ".concat(error.message);
12
+ });
13
+ }
14
+ }
15
+
16
+ /**
17
+ * @callback ErrorCallback
18
+ * @param {ErrorEvent} error
19
+ * @returns {void}
20
+ */
21
+
22
+ /**
23
+ * @param {ErrorCallback} callback
24
+ */
25
+ function listenToRuntimeError(callback) {
26
+ window.addEventListener("error", callback);
27
+ return function cleanup() {
28
+ window.removeEventListener("error", callback);
29
+ };
30
+ }
31
+ export { listenToRuntimeError, parseErrorToStacks };
@@ -0,0 +1,95 @@
1
+ import createMachine from "./fsm.js";
2
+
3
+ /**
4
+ * @typedef {Object} ShowOverlayData
5
+ * @property {'warning' | 'error'} level
6
+ * @property {Array<string | { moduleIdentifier?: string, moduleName?: string, loc?: string, message?: string }>} messages
7
+ */
8
+
9
+ /**
10
+ * @typedef {Object} CreateOverlayMachineOptions
11
+ * @property {(data: ShowOverlayData) => void} showOverlay
12
+ * @property {() => void} hideOverlay
13
+ */
14
+
15
+ /**
16
+ * @param {CreateOverlayMachineOptions} options
17
+ */
18
+ var createOverlayMachine = function createOverlayMachine(options) {
19
+ var hideOverlay = options.hideOverlay,
20
+ showOverlay = options.showOverlay;
21
+ var overlayMachine = createMachine({
22
+ initial: "hidden",
23
+ context: {
24
+ level: "error",
25
+ messages: []
26
+ },
27
+ states: {
28
+ hidden: {
29
+ on: {
30
+ BUILD_ERROR: {
31
+ target: "displayBuildError",
32
+ actions: ["setMessages", "showOverlay"]
33
+ },
34
+ RUNTIME_ERROR: {
35
+ target: "displayRuntimeError",
36
+ actions: ["setMessages", "showOverlay"]
37
+ }
38
+ }
39
+ },
40
+ displayBuildError: {
41
+ on: {
42
+ DISMISS: {
43
+ target: "hidden",
44
+ actions: ["dismissMessages", "hideOverlay"]
45
+ },
46
+ BUILD_ERROR: {
47
+ target: "displayBuildError",
48
+ actions: ["appendMessages", "showOverlay"]
49
+ }
50
+ }
51
+ },
52
+ displayRuntimeError: {
53
+ on: {
54
+ DISMISS: {
55
+ target: "hidden",
56
+ actions: ["dismissMessages", "hideOverlay"]
57
+ },
58
+ RUNTIME_ERROR: {
59
+ target: "displayRuntimeError",
60
+ actions: ["appendMessages", "showOverlay"]
61
+ },
62
+ BUILD_ERROR: {
63
+ target: "displayBuildError",
64
+ actions: ["setMessages", "showOverlay"]
65
+ }
66
+ }
67
+ }
68
+ }
69
+ }, {
70
+ actions: {
71
+ dismissMessages: function dismissMessages() {
72
+ return {
73
+ messages: [],
74
+ level: "error"
75
+ };
76
+ },
77
+ appendMessages: function appendMessages(context, event) {
78
+ return {
79
+ messages: context.messages.concat(event.messages),
80
+ level: event.level || context.level
81
+ };
82
+ },
83
+ setMessages: function setMessages(context, event) {
84
+ return {
85
+ messages: event.messages,
86
+ level: event.level || context.level
87
+ };
88
+ },
89
+ hideOverlay: hideOverlay,
90
+ showOverlay: showOverlay
91
+ }
92
+ });
93
+ return overlayMachine;
94
+ };
95
+ export default createOverlayMachine;
@@ -0,0 +1,74 @@
1
+ // styles are inspired by `react-error-overlay`
2
+
3
+ var msgStyles = {
4
+ error: {
5
+ backgroundColor: "rgba(206, 17, 38, 0.1)",
6
+ color: "#fccfcf"
7
+ },
8
+ warning: {
9
+ backgroundColor: "rgba(251, 245, 180, 0.1)",
10
+ color: "#fbf5b4"
11
+ }
12
+ };
13
+ var iframeStyle = {
14
+ position: "fixed",
15
+ top: 0,
16
+ left: 0,
17
+ right: 0,
18
+ bottom: 0,
19
+ width: "100vw",
20
+ height: "100vh",
21
+ border: "none",
22
+ "z-index": 9999999999
23
+ };
24
+ var containerStyle = {
25
+ position: "fixed",
26
+ boxSizing: "border-box",
27
+ left: 0,
28
+ top: 0,
29
+ right: 0,
30
+ bottom: 0,
31
+ width: "100vw",
32
+ height: "100vh",
33
+ fontSize: "large",
34
+ padding: "2rem 2rem 4rem 2rem",
35
+ lineHeight: "1.2",
36
+ whiteSpace: "pre-wrap",
37
+ overflow: "auto",
38
+ backgroundColor: "rgba(0, 0, 0, 0.9)",
39
+ color: "white"
40
+ };
41
+ var headerStyle = {
42
+ color: "#e83b46",
43
+ fontSize: "2em",
44
+ whiteSpace: "pre-wrap",
45
+ fontFamily: "sans-serif",
46
+ margin: "0 2rem 2rem 0",
47
+ flex: "0 0 auto",
48
+ maxHeight: "50%",
49
+ overflow: "auto"
50
+ };
51
+ var dismissButtonStyle = {
52
+ color: "#ffffff",
53
+ lineHeight: "1rem",
54
+ fontSize: "1.5rem",
55
+ padding: "1rem",
56
+ cursor: "pointer",
57
+ position: "absolute",
58
+ right: 0,
59
+ top: 0,
60
+ backgroundColor: "transparent",
61
+ border: "none"
62
+ };
63
+ var msgTypeStyle = {
64
+ color: "#e83b46",
65
+ fontSize: "1.2em",
66
+ marginBottom: "1rem",
67
+ fontFamily: "sans-serif"
68
+ };
69
+ var msgTextStyle = {
70
+ lineHeight: "1.5",
71
+ fontSize: "1rem",
72
+ fontFamily: "Menlo, Consolas, monospace"
73
+ };
74
+ export { msgStyles, iframeStyle, containerStyle, headerStyle, dismissButtonStyle, msgTypeStyle, msgTextStyle };
package/client/overlay.js CHANGED
@@ -1,7 +1,16 @@
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); }
1
6
  // The error overlay is inspired (and mostly copied) from Create React App (https://github.com/facebookincubator/create-react-app)
2
7
  // They, in turn, got inspired by webpack-hot-middleware (https://github.com/glenjamin/webpack-hot-middleware).
8
+
3
9
  import ansiHTML from "ansi-html-community";
4
10
  import { encode } from "html-entities";
11
+ import { listenToRuntimeError, 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";
5
14
  var colors = {
6
15
  reset: ["transparent", "transparent"],
7
16
  black: "181818",
@@ -14,201 +23,224 @@ var colors = {
14
23
  lightgrey: "EBE7E3",
15
24
  darkgrey: "6D7891"
16
25
  };
17
- /** @type {HTMLIFrameElement | null | undefined} */
18
-
19
- var iframeContainerElement;
20
- /** @type {HTMLDivElement | null | undefined} */
21
-
22
- var containerElement;
23
- /** @type {Array<(element: HTMLDivElement) => void>} */
24
-
25
- var onLoadQueue = [];
26
- /** @type {TrustedTypePolicy | undefined} */
27
-
28
- var overlayTrustedTypesPolicy;
29
26
  ansiHTML.setColors(colors);
30
- /**
31
- * @param {string | null} trustedTypesPolicyName
32
- */
33
-
34
- function createContainer(trustedTypesPolicyName) {
35
- // Enable Trusted Types if they are available in the current browser.
36
- if (window.trustedTypes) {
37
- overlayTrustedTypesPolicy = window.trustedTypes.createPolicy(trustedTypesPolicyName || "webpack-dev-server#overlay", {
38
- createHTML: function createHTML(value) {
39
- return value;
40
- }
41
- });
42
- }
43
-
44
- iframeContainerElement = document.createElement("iframe");
45
- iframeContainerElement.id = "webpack-dev-server-client-overlay";
46
- iframeContainerElement.src = "about:blank";
47
- iframeContainerElement.style.position = "fixed";
48
- iframeContainerElement.style.left = 0;
49
- iframeContainerElement.style.top = 0;
50
- iframeContainerElement.style.right = 0;
51
- iframeContainerElement.style.bottom = 0;
52
- iframeContainerElement.style.width = "100vw";
53
- iframeContainerElement.style.height = "100vh";
54
- iframeContainerElement.style.border = "none";
55
- iframeContainerElement.style.zIndex = 9999999999;
56
-
57
- iframeContainerElement.onload = function () {
58
- containerElement =
59
- /** @type {Document} */
60
-
61
- /** @type {HTMLIFrameElement} */
62
- iframeContainerElement.contentDocument.createElement("div");
63
- containerElement.id = "webpack-dev-server-client-overlay-div";
64
- containerElement.style.position = "fixed";
65
- containerElement.style.boxSizing = "border-box";
66
- containerElement.style.left = 0;
67
- containerElement.style.top = 0;
68
- containerElement.style.right = 0;
69
- containerElement.style.bottom = 0;
70
- containerElement.style.width = "100vw";
71
- containerElement.style.height = "100vh";
72
- containerElement.style.backgroundColor = "rgba(0, 0, 0, 0.85)";
73
- containerElement.style.color = "#E8E8E8";
74
- containerElement.style.fontFamily = "Menlo, Consolas, monospace";
75
- containerElement.style.fontSize = "large";
76
- containerElement.style.padding = "2rem";
77
- containerElement.style.lineHeight = "1.2";
78
- containerElement.style.whiteSpace = "pre-wrap";
79
- containerElement.style.overflow = "auto";
80
- var headerElement = document.createElement("span");
81
- headerElement.innerText = "Compiled with problems:";
82
- var closeButtonElement = document.createElement("button");
83
- closeButtonElement.innerText = "X";
84
- closeButtonElement.style.background = "transparent";
85
- closeButtonElement.style.border = "none";
86
- closeButtonElement.style.fontSize = "20px";
87
- closeButtonElement.style.fontWeight = "bold";
88
- closeButtonElement.style.color = "white";
89
- closeButtonElement.style.cursor = "pointer";
90
- closeButtonElement.style.cssFloat = "right"; // @ts-ignore
91
-
92
- closeButtonElement.style.styleFloat = "right";
93
- closeButtonElement.addEventListener("click", function () {
94
- hide();
95
- });
96
- containerElement.appendChild(headerElement);
97
- containerElement.appendChild(closeButtonElement);
98
- containerElement.appendChild(document.createElement("br"));
99
- containerElement.appendChild(document.createElement("br"));
100
- /** @type {Document} */
101
-
102
- /** @type {HTMLIFrameElement} */
103
- iframeContainerElement.contentDocument.body.appendChild(containerElement);
104
- onLoadQueue.forEach(function (onLoad) {
105
- onLoad(
106
- /** @type {HTMLDivElement} */
107
- containerElement);
108
- });
109
- onLoadQueue = [];
110
- /** @type {HTMLIFrameElement} */
111
-
112
- iframeContainerElement.onload = null;
113
- };
114
-
115
- document.body.appendChild(iframeContainerElement);
116
- }
117
- /**
118
- * @param {(element: HTMLDivElement) => void} callback
119
- * @param {string | null} trustedTypesPolicyName
120
- */
121
-
122
-
123
- function ensureOverlayExists(callback, trustedTypesPolicyName) {
124
- if (containerElement) {
125
- // Everything is ready, call the callback right away.
126
- callback(containerElement);
127
- return;
128
- }
129
-
130
- onLoadQueue.push(callback);
131
-
132
- if (iframeContainerElement) {
133
- return;
134
- }
135
27
 
136
- createContainer(trustedTypesPolicyName);
137
- } // Successful compilation.
138
-
139
-
140
- function hide() {
141
- if (!iframeContainerElement) {
142
- return;
143
- } // Clean up and reset internal state.
144
-
145
-
146
- document.body.removeChild(iframeContainerElement);
147
- iframeContainerElement = null;
148
- containerElement = null;
149
- }
150
28
  /**
151
29
  * @param {string} type
152
- * @param {string | { file?: string, moduleName?: string, loc?: string, message?: string }} item
30
+ * @param {string | { file?: string, moduleName?: string, loc?: string, message?: string; stack?: string[] }} item
153
31
  * @returns {{ header: string, body: string }}
154
32
  */
155
-
156
-
157
33
  function formatProblem(type, item) {
158
34
  var header = type === "warning" ? "WARNING" : "ERROR";
159
35
  var body = "";
160
-
161
36
  if (typeof item === "string") {
162
37
  body += item;
163
38
  } else {
164
- var file = item.file || ""; // eslint-disable-next-line no-nested-ternary
165
-
39
+ var file = item.file || "";
40
+ // eslint-disable-next-line no-nested-ternary
166
41
  var moduleName = item.moduleName ? item.moduleName.indexOf("!") !== -1 ? "".concat(item.moduleName.replace(/^(\s|\S)*!/, ""), " (").concat(item.moduleName, ")") : "".concat(item.moduleName) : "";
167
42
  var loc = item.loc;
168
43
  header += "".concat(moduleName || file ? " in ".concat(moduleName ? "".concat(moduleName).concat(file ? " (".concat(file, ")") : "") : file).concat(loc ? " ".concat(loc) : "") : "");
169
44
  body += item.message || "";
170
45
  }
171
-
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
+ }
172
53
  return {
173
54
  header: header,
174
55
  body: body
175
56
  };
176
- } // Compilation with errors (e.g. syntax error or missing modules).
57
+ }
177
58
 
178
59
  /**
179
- * @param {string} type
180
- * @param {Array<string | { file?: string, moduleName?: string, loc?: string, message?: string }>} messages
181
- * @param {string | null} trustedTypesPolicyName
60
+ * @typedef {Object} CreateOverlayOptions
61
+ * @property {string | null} trustedTypesPolicyName
182
62
  */
183
63
 
64
+ /**
65
+ *
66
+ * @param {CreateOverlayOptions} options
67
+ */
68
+ var createOverlay = function createOverlay(options) {
69
+ /** @type {HTMLIFrameElement | null | undefined} */
70
+ var iframeContainerElement;
71
+ /** @type {HTMLDivElement | null | undefined} */
72
+ var containerElement;
73
+ /** @type {Array<(element: HTMLDivElement) => void>} */
74
+ var onLoadQueue = [];
75
+ /** @type {TrustedTypePolicy | undefined} */
76
+ var overlayTrustedTypesPolicy;
77
+
78
+ /**
79
+ *
80
+ * @param {HTMLElement} element
81
+ * @param {CSSStyleDeclaration} style
82
+ */
83
+ function applyStyle(element, style) {
84
+ Object.keys(style).forEach(function (prop) {
85
+ element.style[prop] = style[prop];
86
+ });
87
+ }
184
88
 
185
- function show(type, messages, trustedTypesPolicyName) {
186
- ensureOverlayExists(function () {
187
- messages.forEach(function (message) {
188
- var entryElement = document.createElement("div");
189
- var typeElement = document.createElement("span");
89
+ /**
90
+ * @param {string | null} trustedTypesPolicyName
91
+ */
92
+ function createContainer(trustedTypesPolicyName) {
93
+ // Enable Trusted Types if they are available in the current browser.
94
+ if (window.trustedTypes) {
95
+ overlayTrustedTypesPolicy = window.trustedTypes.createPolicy(trustedTypesPolicyName || "webpack-dev-server#overlay", {
96
+ createHTML: function createHTML(value) {
97
+ return value;
98
+ }
99
+ });
100
+ }
101
+ iframeContainerElement = document.createElement("iframe");
102
+ iframeContainerElement.id = "webpack-dev-server-client-overlay";
103
+ iframeContainerElement.src = "about:blank";
104
+ applyStyle(iframeContainerElement, iframeStyle);
105
+ iframeContainerElement.onload = function () {
106
+ var contentElement = /** @type {Document} */
107
+ /** @type {HTMLIFrameElement} */
108
+ iframeContainerElement.contentDocument.createElement("div");
109
+ containerElement = /** @type {Document} */
110
+ /** @type {HTMLIFrameElement} */
111
+ iframeContainerElement.contentDocument.createElement("div");
112
+ contentElement.id = "webpack-dev-server-client-overlay-div";
113
+ applyStyle(contentElement, containerStyle);
114
+ var headerElement = document.createElement("div");
115
+ headerElement.innerText = "Compiled with problems:";
116
+ applyStyle(headerElement, headerStyle);
117
+ var closeButtonElement = document.createElement("button");
118
+ applyStyle(closeButtonElement, dismissButtonStyle);
119
+ closeButtonElement.innerText = "×";
120
+ closeButtonElement.ariaLabel = "Dismiss";
121
+ closeButtonElement.addEventListener("click", function () {
122
+ // eslint-disable-next-line no-use-before-define
123
+ overlayService.send({
124
+ type: "DISMISS"
125
+ });
126
+ });
127
+ contentElement.appendChild(headerElement);
128
+ contentElement.appendChild(closeButtonElement);
129
+ contentElement.appendChild(containerElement);
130
+
131
+ /** @type {Document} */
132
+ /** @type {HTMLIFrameElement} */
133
+ iframeContainerElement.contentDocument.body.appendChild(contentElement);
134
+ onLoadQueue.forEach(function (onLoad) {
135
+ onLoad( /** @type {HTMLDivElement} */contentElement);
136
+ });
137
+ onLoadQueue = [];
138
+
139
+ /** @type {HTMLIFrameElement} */
140
+ iframeContainerElement.onload = null;
141
+ };
142
+ document.body.appendChild(iframeContainerElement);
143
+ }
190
144
 
191
- var _formatProblem = formatProblem(type, message),
192
- header = _formatProblem.header,
193
- body = _formatProblem.body;
145
+ /**
146
+ * @param {(element: HTMLDivElement) => void} callback
147
+ * @param {string | null} trustedTypesPolicyName
148
+ */
149
+ function ensureOverlayExists(callback, trustedTypesPolicyName) {
150
+ if (containerElement) {
151
+ containerElement.innerHTML = "";
152
+ // Everything is ready, call the callback right away.
153
+ callback(containerElement);
154
+ return;
155
+ }
156
+ onLoadQueue.push(callback);
157
+ if (iframeContainerElement) {
158
+ return;
159
+ }
160
+ createContainer(trustedTypesPolicyName);
161
+ }
194
162
 
195
- typeElement.innerText = header;
196
- typeElement.style.color = "#".concat(colors.red); // Make it look similar to our terminal.
163
+ // Successful compilation.
164
+ function hide() {
165
+ if (!iframeContainerElement) {
166
+ return;
167
+ }
197
168
 
198
- var text = ansiHTML(encode(body));
199
- var messageTextNode = document.createElement("div");
200
- messageTextNode.innerHTML = overlayTrustedTypesPolicy ? overlayTrustedTypesPolicy.createHTML(text) : text;
201
- entryElement.appendChild(typeElement);
202
- entryElement.appendChild(document.createElement("br"));
203
- entryElement.appendChild(document.createElement("br"));
204
- entryElement.appendChild(messageTextNode);
205
- entryElement.appendChild(document.createElement("br"));
206
- entryElement.appendChild(document.createElement("br"));
207
- /** @type {HTMLDivElement} */
169
+ // Clean up and reset internal state.
170
+ document.body.removeChild(iframeContainerElement);
171
+ iframeContainerElement = null;
172
+ containerElement = null;
173
+ }
208
174
 
209
- containerElement.appendChild(entryElement);
175
+ // Compilation with errors (e.g. syntax error or missing modules).
176
+ /**
177
+ * @param {string} type
178
+ * @param {Array<string | { moduleIdentifier?: string, moduleName?: string, loc?: string, message?: string }>} messages
179
+ * @param {string | null} trustedTypesPolicyName
180
+ */
181
+ function show(type, messages, trustedTypesPolicyName) {
182
+ ensureOverlayExists(function () {
183
+ messages.forEach(function (message) {
184
+ var entryElement = document.createElement("div");
185
+ var msgStyle = type === "warning" ? msgStyles.warning : msgStyles.error;
186
+ applyStyle(entryElement, _objectSpread(_objectSpread({}, msgStyle), {}, {
187
+ padding: "1rem 1rem 1.5rem 1rem"
188
+ }));
189
+ var typeElement = document.createElement("div");
190
+ var _formatProblem = formatProblem(type, message),
191
+ header = _formatProblem.header,
192
+ body = _formatProblem.body;
193
+ typeElement.innerText = header;
194
+ applyStyle(typeElement, msgTypeStyle);
195
+ if (message.moduleIdentifier) {
196
+ applyStyle(typeElement, {
197
+ cursor: "pointer"
198
+ });
199
+ // element.dataset not supported in IE
200
+ typeElement.setAttribute("data-can-open", true);
201
+ typeElement.addEventListener("click", function () {
202
+ fetch("/webpack-dev-server/open-editor?fileName=".concat(message.moduleIdentifier));
203
+ });
204
+ }
205
+
206
+ // Make it look similar to our terminal.
207
+ var text = ansiHTML(encode(body));
208
+ var messageTextNode = document.createElement("div");
209
+ applyStyle(messageTextNode, msgTextStyle);
210
+ messageTextNode.innerHTML = overlayTrustedTypesPolicy ? overlayTrustedTypesPolicy.createHTML(text) : text;
211
+ entryElement.appendChild(typeElement);
212
+ entryElement.appendChild(messageTextNode);
213
+
214
+ /** @type {HTMLDivElement} */
215
+ containerElement.appendChild(entryElement);
216
+ });
217
+ }, trustedTypesPolicyName);
218
+ }
219
+ var overlayService = createOverlayMachine({
220
+ showOverlay: function showOverlay(_ref) {
221
+ var _ref$level = _ref.level,
222
+ level = _ref$level === void 0 ? "error" : _ref$level,
223
+ messages = _ref.messages;
224
+ return show(level, messages, options.trustedTypesPolicyName);
225
+ },
226
+ hideOverlay: hide
227
+ });
228
+ listenToRuntimeError(function (errorEvent) {
229
+ // error property may be empty in older browser like IE
230
+ var error = errorEvent.error,
231
+ message = errorEvent.message;
232
+ if (!error && !message) {
233
+ return;
234
+ }
235
+ var errorObject = error instanceof Error ? error : new Error(error || message);
236
+ overlayService.send({
237
+ type: "RUNTIME_ERROR",
238
+ messages: [{
239
+ message: errorObject.message,
240
+ stack: parseErrorToStacks(errorObject)
241
+ }]
210
242
  });
211
- }, trustedTypesPolicyName);
212
- }
213
-
214
- export { formatProblem, show, hide };
243
+ });
244
+ return overlayService;
245
+ };
246
+ export { formatProblem, createOverlay };