Skip to content

Commit 90d4a7c

Browse files
authored
refactor(client): overlay (#2003)
1 parent 47453cb commit 90d4a7c

File tree

4 files changed

+61
-26
lines changed

4 files changed

+61
-26
lines changed

client-src/default/overlay.js

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
// They, in turn, got inspired by webpack-hot-middleware (https://github.com/glenjamin/webpack-hot-middleware).
55

66
const ansiHTML = require('ansi-html');
7-
const Entities = require('html-entities').AllHtmlEntities;
8-
9-
const entities = new Entities();
7+
const { AllHtmlEntities } = require('html-entities');
108

9+
const entities = new AllHtmlEntities();
1110
const colors = {
1211
reset: ['transparent', 'transparent'],
1312
black: '181818',
@@ -20,6 +19,11 @@ const colors = {
2019
lightgrey: 'EBE7E3',
2120
darkgrey: '6D7891',
2221
};
22+
23+
let overlayIframe = null;
24+
let overlayDiv = null;
25+
let lastOnOverlayDivReady = null;
26+
2327
ansiHTML.setColors(colors);
2428

2529
function createOverlayIframe(onIframeLoad) {
@@ -62,10 +66,6 @@ function addOverlayDivTo(iframe) {
6266
return div;
6367
}
6468

65-
let overlayIframe = null;
66-
let overlayDiv = null;
67-
let lastOnOverlayDivReady = null;
68-
6969
function ensureOverlayDivExists(onOverlayDivReady) {
7070
if (overlayDiv) {
7171
// Everything is ready, call the callback right away.
@@ -78,7 +78,7 @@ function ensureOverlayDivExists(onOverlayDivReady) {
7878
lastOnOverlayDivReady = onOverlayDivReady;
7979

8080
if (overlayIframe) {
81-
// We're already creating it.
81+
// We've already created it.
8282
return;
8383
}
8484

@@ -95,16 +95,8 @@ function ensureOverlayDivExists(onOverlayDivReady) {
9595
document.body.appendChild(overlayIframe);
9696
}
9797

98-
function showMessageOverlay(message) {
99-
ensureOverlayDivExists((div) => {
100-
// Make it look similar to our terminal.
101-
div.innerHTML = `<span style="color: #${
102-
colors.red
103-
}">Failed to compile.</span><br><br>${ansiHTML(entities.encode(message))}`;
104-
});
105-
}
106-
107-
function destroyErrorOverlay() {
98+
// Successful compilation.
99+
function clear() {
108100
if (!overlayDiv) {
109101
// It is not there in the first place.
110102
return;
@@ -117,12 +109,19 @@ function destroyErrorOverlay() {
117109
lastOnOverlayDivReady = null;
118110
}
119111

120-
// Successful compilation.
121-
exports.clear = function handleSuccess() {
122-
destroyErrorOverlay();
123-
};
124-
125112
// Compilation with errors (e.g. syntax error or missing modules).
126-
exports.showMessage = function handleMessage(messages) {
127-
showMessageOverlay(messages[0]);
113+
function showMessage(messages) {
114+
ensureOverlayDivExists((div) => {
115+
// Make it look similar to our terminal.
116+
div.innerHTML = `<span style="color: #${
117+
colors.red
118+
}">Failed to compile.</span><br><br>${ansiHTML(
119+
entities.encode(messages[0])
120+
)}`;
121+
});
122+
}
123+
124+
module.exports = {
125+
clear,
126+
showMessage,
128127
};

examples/cli/overlay/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ target.classList.add('pass');
66
target.innerHTML = 'Success!';
77

88
// This results in an error:
9-
// if(!window) require("test");
9+
// if (!window) require('test');
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`overlay should run clear 1`] = `"<div id=\\"webpack-dev-server-client-overlay-div\\" style=\\"position: fixed; box-sizing: border-box; left: 0px; top: 0px; right: 0px; bottom: 0px; width: 100vw; height: 100vh; background-color: rgba(0, 0, 0, 0.85); color: rgb(232, 232, 232); font-family: Menlo, Consolas, monospace; font-size: large; padding: 2rem; line-height: 1.2; white-space: pre-wrap; overflow: auto;\\"><span style=\\"color: #E36049\\">Failed to compile.</span><br><br>test!</div>"`;
4+
5+
exports[`overlay should run clear 2`] = `""`;
6+
7+
exports[`overlay should run showMessage 1`] = `""`;
8+
9+
exports[`overlay should run showMessage 2`] = `"<div id=\\"webpack-dev-server-client-overlay-div\\" style=\\"position: fixed; box-sizing: border-box; left: 0px; top: 0px; right: 0px; bottom: 0px; width: 100vw; height: 100vh; background-color: rgba(0, 0, 0, 0.85); color: rgb(232, 232, 232); font-family: Menlo, Consolas, monospace; font-size: large; padding: 2rem; line-height: 1.2; white-space: pre-wrap; overflow: auto;\\"><span style=\\"color: #E36049\\">Failed to compile.</span><br><br>test!</div>"`;

test/client/overlay.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
const overlay = require('../../client-src/default/overlay');
4+
5+
describe('overlay', () => {
6+
it('should run showMessage', () => {
7+
expect(document.body.innerHTML).toMatchSnapshot();
8+
9+
overlay.showMessage(['test!']);
10+
11+
expect(
12+
document.getElementsByTagName('iframe')[0].contentDocument.body.innerHTML
13+
).toMatchSnapshot();
14+
});
15+
16+
it('should run clear', () => {
17+
overlay.showMessage(['test!']);
18+
19+
expect(
20+
document.getElementsByTagName('iframe')[0].contentDocument.body.innerHTML
21+
).toMatchSnapshot();
22+
23+
overlay.clear();
24+
25+
expect(document.body.innerHTML).toMatchSnapshot();
26+
});
27+
});

0 commit comments

Comments
 (0)