Skip to content

Commit 7e18161

Browse files
fix: overlay with warnings (#3215)
1 parent f4f4b45 commit 7e18161

File tree

7 files changed

+53
-49
lines changed

7 files changed

+53
-49
lines changed

client-src/index.js

+32-29
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ const defaultOptions = {
2121
hotReload: true,
2222
liveReload: false,
2323
initial: true,
24-
useWarningOverlay: false,
25-
useErrorOverlay: false,
26-
useProgress: false,
24+
progress: false,
25+
overlay: false,
2726
};
2827
const parsedResourceQuery = parseURL(__resourceQuery);
2928
const options = defaultOptions;
@@ -84,8 +83,8 @@ const onSocketMessage = {
8483
invalid() {
8584
log.info('App updated. Recompiling...');
8685

87-
// fixes #1042. overlay doesn't clear if errors are fixed but warnings remain.
88-
if (options.useWarningOverlay || options.useErrorOverlay) {
86+
// Fixes #1042. overlay doesn't clear if errors are fixed but warnings remain.
87+
if (options.overlay) {
8988
overlay.clear();
9089
}
9190

@@ -94,43 +93,37 @@ const onSocketMessage = {
9493
hash(hash) {
9594
status.currentHash = hash;
9695
},
97-
'still-ok': function stillOk() {
98-
log.info('Nothing changed.');
99-
100-
if (options.useWarningOverlay || options.useErrorOverlay) {
101-
overlay.clear();
102-
}
103-
104-
sendMessage('StillOk');
105-
},
10696
logging: setAllLogLevel,
10797
overlay(value) {
108-
if (typeof document !== 'undefined') {
109-
if (typeof value === 'boolean') {
110-
options.useWarningOverlay = false;
111-
options.useErrorOverlay = value;
112-
} else if (value) {
113-
options.useWarningOverlay = value.warnings;
114-
options.useErrorOverlay = value.errors;
115-
}
98+
if (typeof document === 'undefined') {
99+
return;
116100
}
101+
102+
options.overlay = value;
117103
},
118104
progress(progress) {
119-
if (typeof document !== 'undefined') {
120-
options.useProgress = progress;
121-
}
105+
options.progress = progress;
122106
},
123107
'progress-update': function progressUpdate(data) {
124-
if (options.useProgress) {
108+
if (options.progress) {
125109
log.info(`${data.percent}% - ${data.msg}.`);
126110
}
127111

128112
sendMessage('Progress', data);
129113
},
114+
'still-ok': function stillOk() {
115+
log.info('Nothing changed.');
116+
117+
if (options.overlay) {
118+
overlay.clear();
119+
}
120+
121+
sendMessage('StillOk');
122+
},
130123
ok() {
131124
sendMessage('Ok');
132125

133-
if (options.useWarningOverlay || options.useErrorOverlay) {
126+
if (options.overlay) {
134127
overlay.clear();
135128
}
136129

@@ -158,7 +151,12 @@ const onSocketMessage = {
158151
log.warn(strippedWarnings[i]);
159152
}
160153

161-
if (options.useWarningOverlay) {
154+
const needShowOverlay =
155+
typeof options.overlay === 'boolean'
156+
? options.overlay
157+
: options.overlay && options.overlay.warnings;
158+
159+
if (needShowOverlay) {
162160
overlay.showMessage(warnings);
163161
}
164162

@@ -181,7 +179,12 @@ const onSocketMessage = {
181179
log.error(strippedErrors[i]);
182180
}
183181

184-
if (options.useErrorOverlay) {
182+
const needShowOverlay =
183+
typeof options.overlay === 'boolean'
184+
? options.overlay
185+
: options.overlay && options.overlay.errors;
186+
187+
if (needShowOverlay) {
185188
overlay.showMessage(errors);
186189
}
187190

client-src/overlay.js

+6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ ansiHTML.setColors(colors);
2727

2828
function createOverlayIframe(onIframeLoad) {
2929
const iframe = document.createElement('iframe');
30+
3031
iframe.id = 'webpack-dev-server-client-overlay';
3132
iframe.src = 'about:blank';
3233
iframe.style.position = 'fixed';
@@ -39,11 +40,13 @@ function createOverlayIframe(onIframeLoad) {
3940
iframe.style.border = 'none';
4041
iframe.style.zIndex = 9999999999;
4142
iframe.onload = onIframeLoad;
43+
4244
return iframe;
4345
}
4446

4547
function addOverlayDivTo(iframe) {
4648
const div = iframe.contentDocument.createElement('div');
49+
4750
div.id = 'webpack-dev-server-client-overlay-div';
4851
div.style.position = 'fixed';
4952
div.style.boxSizing = 'border-box';
@@ -61,7 +64,9 @@ function addOverlayDivTo(iframe) {
6164
div.style.lineHeight = '1.2';
6265
div.style.whiteSpace = 'pre-wrap';
6366
div.style.overflow = 'auto';
67+
6468
iframe.contentDocument.body.appendChild(div);
69+
6570
return div;
6671
}
6772

@@ -103,6 +108,7 @@ function clear() {
103108

104109
// Clean up and reset internal state.
105110
document.body.removeChild(overlayIframe);
111+
106112
overlayDiv = null;
107113
overlayIframe = null;
108114
lastOnOverlayDivReady = null;

client-src/socket.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
camelcase
66
*/
77

8-
// this WebsocketClient is here as a default fallback,
9-
// in case the client is not injected
8+
// this WebsocketClient is here as a default fallback, in case the client is not injected
109
const Client =
1110
typeof __webpack_dev_server_client__ !== 'undefined'
1211
? __webpack_dev_server_client__
@@ -47,9 +46,10 @@ const socket = function initSocket(url, handlers) {
4746
});
4847

4948
client.onMessage((data) => {
50-
const msg = JSON.parse(data);
51-
if (handlers[msg.type]) {
52-
handlers[msg.type](msg.data);
49+
const message = JSON.parse(data);
50+
51+
if (handlers[message.type]) {
52+
handlers[message.type](message.data);
5353
}
5454
});
5555
};

client-src/utils/sendMessage.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,7 @@ function sendMsg(type, data) {
99
(typeof WorkerGlobalScope === 'undefined' ||
1010
!(self instanceof WorkerGlobalScope))
1111
) {
12-
self.postMessage(
13-
{
14-
type: `webpack${type}`,
15-
data,
16-
},
17-
'*'
18-
);
12+
self.postMessage({ type: `webpack${type}`, data }, '*');
1913
}
2014
}
2115

lib/Server.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -985,17 +985,20 @@ class Server {
985985
}
986986
}
987987

988-
// send stats to a socket or multiple sockets
988+
// Send stats to a socket or multiple sockets
989989
sendStats(sockets, stats, force) {
990990
const shouldEmit =
991991
!force &&
992992
stats &&
993993
(!stats.errors || stats.errors.length === 0) &&
994+
(!stats.warnings || stats.warnings.length === 0) &&
994995
stats.assets &&
995996
stats.assets.every((asset) => !asset.emitted);
996997

997998
if (shouldEmit) {
998-
return this.sockWrite(sockets, 'still-ok');
999+
this.sockWrite(sockets, 'still-ok');
1000+
1001+
return;
9991002
}
10001003

10011004
this.sockWrite(sockets, 'hash', stats.hash);

test/client/__snapshots__/index.test.js.snap.webpack4

+2-3
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,8 @@ Object {
4949
"initial": false,
5050
"liveReload": false,
5151
"logging": "info",
52-
"useErrorOverlay": false,
53-
"useProgress": false,
54-
"useWarningOverlay": false,
52+
"overlay": false,
53+
"progress": false,
5554
}
5655
`;
5756

test/client/__snapshots__/index.test.js.snap.webpack5

+2-3
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,8 @@ Object {
4949
"initial": false,
5050
"liveReload": false,
5151
"logging": "info",
52-
"useErrorOverlay": false,
53-
"useProgress": false,
54-
"useWarningOverlay": false,
52+
"overlay": false,
53+
"progress": false,
5554
}
5655
`;
5756

0 commit comments

Comments
 (0)