Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: overlay with warnings #3215

Merged
merged 2 commits into from
May 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 32 additions & 29 deletions client-src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ const defaultOptions = {
hotReload: true,
liveReload: false,
initial: true,
useWarningOverlay: false,
useErrorOverlay: false,
useProgress: false,
progress: false,
overlay: false,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better keep options here as is,in future we can improve this and allow to using this http://192.168.0.5?overlay&no-progress

};
const parsedResourceQuery = parseURL(__resourceQuery);
const options = defaultOptions;
Expand Down Expand Up @@ -84,8 +83,8 @@ const onSocketMessage = {
invalid() {
log.info('App updated. Recompiling...');

// fixes #1042. overlay doesn't clear if errors are fixed but warnings remain.
if (options.useWarningOverlay || options.useErrorOverlay) {
// Fixes #1042. overlay doesn't clear if errors are fixed but warnings remain.
if (options.overlay) {
overlay.clear();
}

Expand All @@ -94,43 +93,37 @@ const onSocketMessage = {
hash(hash) {
status.currentHash = hash;
},
'still-ok': function stillOk() {
log.info('Nothing changed.');

if (options.useWarningOverlay || options.useErrorOverlay) {
overlay.clear();
}

sendMessage('StillOk');
},
logging: setAllLogLevel,
overlay(value) {
if (typeof document !== 'undefined') {
if (typeof value === 'boolean') {
options.useWarningOverlay = false;
options.useErrorOverlay = value;
} else if (value) {
options.useWarningOverlay = value.warnings;
options.useErrorOverlay = value.errors;
}
if (typeof document === 'undefined') {
return;
}

options.overlay = value;
},
progress(progress) {
if (typeof document !== 'undefined') {
options.useProgress = progress;
}
options.progress = progress;
},
'progress-update': function progressUpdate(data) {
if (options.useProgress) {
if (options.progress) {
log.info(`${data.percent}% - ${data.msg}.`);
}

sendMessage('Progress', data);
},
'still-ok': function stillOk() {
log.info('Nothing changed.');

if (options.overlay) {
overlay.clear();
}

sendMessage('StillOk');
},
ok() {
sendMessage('Ok');

if (options.useWarningOverlay || options.useErrorOverlay) {
if (options.overlay) {
overlay.clear();
}

Expand Down Expand Up @@ -158,7 +151,12 @@ const onSocketMessage = {
log.warn(strippedWarnings[i]);
}

if (options.useWarningOverlay) {
const needShowOverlay =
typeof options.overlay === 'boolean'
? options.overlay
: options.overlay && options.overlay.warnings;

if (needShowOverlay) {
overlay.showMessage(warnings);
}

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

if (options.useErrorOverlay) {
const needShowOverlay =
typeof options.overlay === 'boolean'
? options.overlay
: options.overlay && options.overlay.errors;

if (needShowOverlay) {
overlay.showMessage(errors);
}

Expand Down
6 changes: 6 additions & 0 deletions client-src/overlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ ansiHTML.setColors(colors);

function createOverlayIframe(onIframeLoad) {
const iframe = document.createElement('iframe');

iframe.id = 'webpack-dev-server-client-overlay';
iframe.src = 'about:blank';
iframe.style.position = 'fixed';
Expand All @@ -39,11 +40,13 @@ function createOverlayIframe(onIframeLoad) {
iframe.style.border = 'none';
iframe.style.zIndex = 9999999999;
iframe.onload = onIframeLoad;

return iframe;
}

function addOverlayDivTo(iframe) {
const div = iframe.contentDocument.createElement('div');

div.id = 'webpack-dev-server-client-overlay-div';
div.style.position = 'fixed';
div.style.boxSizing = 'border-box';
Expand All @@ -61,7 +64,9 @@ function addOverlayDivTo(iframe) {
div.style.lineHeight = '1.2';
div.style.whiteSpace = 'pre-wrap';
div.style.overflow = 'auto';

iframe.contentDocument.body.appendChild(div);

return div;
}

Expand Down Expand Up @@ -103,6 +108,7 @@ function clear() {

// Clean up and reset internal state.
document.body.removeChild(overlayIframe);

overlayDiv = null;
overlayIframe = null;
lastOnOverlayDivReady = null;
Expand Down
10 changes: 5 additions & 5 deletions client-src/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
camelcase
*/

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

client.onMessage((data) => {
const msg = JSON.parse(data);
if (handlers[msg.type]) {
handlers[msg.type](msg.data);
const message = JSON.parse(data);

if (handlers[message.type]) {
handlers[message.type](message.data);
}
});
};
Expand Down
8 changes: 1 addition & 7 deletions client-src/utils/sendMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@ function sendMsg(type, data) {
(typeof WorkerGlobalScope === 'undefined' ||
!(self instanceof WorkerGlobalScope))
) {
self.postMessage(
{
type: `webpack${type}`,
data,
},
'*'
);
self.postMessage({ type: `webpack${type}`, data }, '*');
}
}

Expand Down
7 changes: 5 additions & 2 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -986,17 +986,20 @@ class Server {
}
}

// send stats to a socket or multiple sockets
// Send stats to a socket or multiple sockets
sendStats(sockets, stats, force) {
const shouldEmit =
!force &&
stats &&
(!stats.errors || stats.errors.length === 0) &&
(!stats.warnings || stats.warnings.length === 0) &&
Copy link
Member Author

@alexander-akait alexander-akait Apr 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we fix bug, when if we have previously warning and run invalidate without change code (for example using API to invalidate), overlay with warnings hide and did not show again

stats.assets &&
stats.assets.every((asset) => !asset.emitted);

if (shouldEmit) {
return this.sockWrite(sockets, 'still-ok');
this.sockWrite(sockets, 'still-ok');

return;
}

this.sockWrite(sockets, 'hash', stats.hash);
Expand Down
5 changes: 2 additions & 3 deletions test/client/__snapshots__/index.test.js.snap.webpack4
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ Object {
"initial": false,
"liveReload": false,
"logging": "info",
"useErrorOverlay": false,
"useProgress": false,
"useWarningOverlay": false,
"overlay": false,
"progress": false,
}
`;

Expand Down
5 changes: 2 additions & 3 deletions test/client/__snapshots__/index.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ Object {
"initial": false,
"liveReload": false,
"logging": "info",
"useErrorOverlay": false,
"useProgress": false,
"useWarningOverlay": false,
"overlay": false,
"progress": false,
}
`;

Expand Down