Skip to content

Commit ccef0d1

Browse files
Wernersonshellscape
authored andcommitted
Print webpack progress to browser console (#1063)
* copied status.js from overlay.js * first version * added example * added checkif status is set * changed description * fixed config schema * fixed validation testcase * added status to title * changed status div style * added status window delay status window now stays for a moment after recompilation before reloading the app * made status work better with overlay * added status bar * added status update * added progress updates on backend * changed progress bar color to webpack logo color * added second webpack logo color as comment The second webpack color (the light blue) doesn't look that great as background for the status window, I added it just for reference. * added compilation status to title * ran prepublish, posttest and beautify * fixed codacy issues * fixed codacy issues This time for good * fixed merge error * console-progress v1 * fixed lint errrors (LF -> CRLF) * load plugin only if progress is enabled * linted Server.js
1 parent d3a650f commit ccef0d1

File tree

8 files changed

+66
-2
lines changed

8 files changed

+66
-2
lines changed

client/index.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function getCurrentScriptSource() {
1818
const currentScript = scriptElements[scriptElements.length - 1];
1919
if (currentScript) { return currentScript.getAttribute('src'); }
2020
// Fail as there was no script to use.
21-
throw new Error('[WDS] Failed to get current script source');
21+
throw new Error('[WDS] Failed to get current script source.');
2222
}
2323

2424
let urlParts;
@@ -47,6 +47,7 @@ let initial = true;
4747
let currentHash = '';
4848
let useWarningOverlay = false;
4949
let useErrorOverlay = false;
50+
let useProgress = false;
5051

5152
const INFO = 'info';
5253
const WARNING = 'warning';
@@ -122,6 +123,14 @@ const onSocketMsg = {
122123
}
123124
}
124125
},
126+
progress(progress) {
127+
if (typeof document !== 'undefined') {
128+
useProgress = progress;
129+
}
130+
},
131+
'progress-update': function progressUpdate(data) {
132+
if (useProgress) log.info(`[WDS] ${data.percent}% - ${data.msg}.`);
133+
},
125134
ok() {
126135
sendMsg('Ok');
127136
if (useWarningOverlay || useErrorOverlay) overlay.clear();

examples/progress/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Status
2+
3+
```shell
4+
node ../../bin/webpack-dev-server.js --open
5+
```
6+
7+
## What should happen
8+
9+
The script should open the browser and show a heading with "Example: progress".
10+
11+
In `app.js`, change the text and save. You should see the compilation progress in the browser console.

examples/progress/app.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
// Change the following line and save to see the compilation status
4+
5+
document.write('Change me to see compilation progress in console...');

examples/progress/index.html

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Progress example</title>
5+
<script src="/bundle.js" type="text/javascript" charset="utf-8"></script>
6+
</head>
7+
<body>
8+
<h1>Example: progress</h1>
9+
</body>
10+
</html>

examples/progress/webpack.config.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
module.exports = {
4+
context: __dirname,
5+
entry: './app.js',
6+
devServer: {
7+
progress: true
8+
}
9+
};

lib/Server.js

+12
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ function Server(compiler, options) {
4242
this.headers = options.headers;
4343
this.clientLogLevel = options.clientLogLevel;
4444
this.clientOverlay = options.overlay;
45+
this.progress = options.progress;
4546
this.disableHostCheck = !!options.disableHostCheck;
4647
this.publicHost = options.public;
4748
this.allowedHosts = options.allowedHosts;
@@ -52,6 +53,15 @@ function Server(compiler, options) {
5253
const invalidPlugin = () => {
5354
this.sockWrite(this.sockets, 'invalid');
5455
};
56+
if (this.progress) {
57+
const progressPlugin = new webpack.ProgressPlugin((percent, msg, addInfo) => {
58+
percent = Math.floor(percent * 100);
59+
if (percent === 100) msg = 'Compilation competed';
60+
if (addInfo) msg = `${msg} (${addInfo})`;
61+
this.sockWrite(this.sockets, 'progress-update', { percent, msg });
62+
});
63+
compiler.apply(progressPlugin);
64+
}
5565
compiler.plugin('compile', invalidPlugin);
5666
compiler.plugin('invalid', invalidPlugin);
5767
compiler.plugin('done', (stats) => {
@@ -563,6 +573,8 @@ Server.prototype.listen = function (port, hostname, fn) {
563573

564574
if (this.clientLogLevel) { this.sockWrite([conn], 'log-level', this.clientLogLevel); }
565575

576+
if (this.progress) { this.sockWrite([conn], 'progress', this.progress); }
577+
566578
if (this.clientOverlay) { this.sockWrite([conn], 'overlay', this.clientOverlay); }
567579

568580
if (this.hot) this.sockWrite([conn], 'hot');

lib/optionsSchema.json

+8
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@
9797
}
9898
]
9999
},
100+
"progress": {
101+
"description": "Shows compilation progress in browser console.",
102+
"anyOf": [
103+
{
104+
"type": "boolean"
105+
}
106+
]
107+
},
100108
"key": {
101109
"description": "The contents of a SSL key.",
102110
"anyOf": [

test/Validation.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe('Validation', () => {
4949
message: [
5050
" - configuration has an unknown property 'asdf'. These properties are valid:",
5151
' object { hot?, hotOnly?, lazy?, bonjour?, host?, allowedHosts?, filename?, publicPath?, port?, socket?, ' +
52-
'watchOptions?, headers?, clientLogLevel?, overlay?, key?, cert?, ca?, pfx?, pfxPassphrase?, requestCert?, ' +
52+
'watchOptions?, headers?, clientLogLevel?, overlay?, progress?, key?, cert?, ca?, pfx?, pfxPassphrase?, requestCert?, ' +
5353
'inline?, disableHostCheck?, public?, https?, contentBase?, watchContentBase?, open?, useLocalIp?, openPage?, features?, ' +
5454
'compress?, proxy?, historyApiFallback?, staticOptions?, setup?, stats?, reporter?, ' +
5555
'noInfo?, quiet?, serverSideRender?, index?, log?, warn? }'

0 commit comments

Comments
 (0)