Skip to content

Commit

Permalink
Print webpack progress to browser console (#1063)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Wernerson authored and shellscape committed Sep 13, 2017
1 parent d3a650f commit ccef0d1
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 2 deletions.
11 changes: 10 additions & 1 deletion client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function getCurrentScriptSource() {
const currentScript = scriptElements[scriptElements.length - 1];
if (currentScript) { return currentScript.getAttribute('src'); }
// Fail as there was no script to use.
throw new Error('[WDS] Failed to get current script source');
throw new Error('[WDS] Failed to get current script source.');
}

let urlParts;
Expand Down Expand Up @@ -47,6 +47,7 @@ let initial = true;
let currentHash = '';
let useWarningOverlay = false;
let useErrorOverlay = false;
let useProgress = false;

const INFO = 'info';
const WARNING = 'warning';
Expand Down Expand Up @@ -122,6 +123,14 @@ const onSocketMsg = {
}
}
},
progress(progress) {
if (typeof document !== 'undefined') {
useProgress = progress;
}
},
'progress-update': function progressUpdate(data) {
if (useProgress) log.info(`[WDS] ${data.percent}% - ${data.msg}.`);
},
ok() {
sendMsg('Ok');
if (useWarningOverlay || useErrorOverlay) overlay.clear();
Expand Down
11 changes: 11 additions & 0 deletions examples/progress/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Status

```shell
node ../../bin/webpack-dev-server.js --open
```

## What should happen

The script should open the browser and show a heading with "Example: progress".

In `app.js`, change the text and save. You should see the compilation progress in the browser console.
5 changes: 5 additions & 0 deletions examples/progress/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

// Change the following line and save to see the compilation status

document.write('Change me to see compilation progress in console...');
10 changes: 10 additions & 0 deletions examples/progress/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title>Progress example</title>
<script src="/bundle.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<h1>Example: progress</h1>
</body>
</html>
9 changes: 9 additions & 0 deletions examples/progress/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

module.exports = {
context: __dirname,
entry: './app.js',
devServer: {
progress: true
}
};
12 changes: 12 additions & 0 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function Server(compiler, options) {
this.headers = options.headers;
this.clientLogLevel = options.clientLogLevel;
this.clientOverlay = options.overlay;
this.progress = options.progress;
this.disableHostCheck = !!options.disableHostCheck;
this.publicHost = options.public;
this.allowedHosts = options.allowedHosts;
Expand All @@ -52,6 +53,15 @@ function Server(compiler, options) {
const invalidPlugin = () => {
this.sockWrite(this.sockets, 'invalid');
};
if (this.progress) {
const progressPlugin = new webpack.ProgressPlugin((percent, msg, addInfo) => {
percent = Math.floor(percent * 100);
if (percent === 100) msg = 'Compilation competed';
if (addInfo) msg = `${msg} (${addInfo})`;
this.sockWrite(this.sockets, 'progress-update', { percent, msg });
});
compiler.apply(progressPlugin);
}
compiler.plugin('compile', invalidPlugin);
compiler.plugin('invalid', invalidPlugin);
compiler.plugin('done', (stats) => {
Expand Down Expand Up @@ -563,6 +573,8 @@ Server.prototype.listen = function (port, hostname, fn) {

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

if (this.progress) { this.sockWrite([conn], 'progress', this.progress); }

if (this.clientOverlay) { this.sockWrite([conn], 'overlay', this.clientOverlay); }

if (this.hot) this.sockWrite([conn], 'hot');
Expand Down
8 changes: 8 additions & 0 deletions lib/optionsSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@
}
]
},
"progress": {
"description": "Shows compilation progress in browser console.",
"anyOf": [
{
"type": "boolean"
}
]
},
"key": {
"description": "The contents of a SSL key.",
"anyOf": [
Expand Down
2 changes: 1 addition & 1 deletion test/Validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('Validation', () => {
message: [
" - configuration has an unknown property 'asdf'. These properties are valid:",
' object { hot?, hotOnly?, lazy?, bonjour?, host?, allowedHosts?, filename?, publicPath?, port?, socket?, ' +
'watchOptions?, headers?, clientLogLevel?, overlay?, key?, cert?, ca?, pfx?, pfxPassphrase?, requestCert?, ' +
'watchOptions?, headers?, clientLogLevel?, overlay?, progress?, key?, cert?, ca?, pfx?, pfxPassphrase?, requestCert?, ' +
'inline?, disableHostCheck?, public?, https?, contentBase?, watchContentBase?, open?, useLocalIp?, openPage?, features?, ' +
'compress?, proxy?, historyApiFallback?, staticOptions?, setup?, stats?, reporter?, ' +
'noInfo?, quiet?, serverSideRender?, index?, log?, warn? }'
Expand Down

0 comments on commit ccef0d1

Please sign in to comment.