Created
July 20, 2015 17:51
-
-
Save bendman/c87427251b8eb1a75ea8 to your computer and use it in GitHub Desktop.
Return a plugin which logs initial build progress in webpack with live updates.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* getProgressPlugin | |
* Return a progress plugin instance which outputs build progress percentages in realtime | |
* | |
* @return {Object} A ProgressPlugin instance | |
*/ | |
function getProgressPlugin() { | |
var chars = 0, lastState, lastStateTime; | |
return new webpack.ProgressPlugin(function(percentage, msg) { | |
var state = msg; | |
if (percentage < 1) { | |
percentage = Math.floor(percentage * 100); | |
msg = percentage + '% ' + msg; | |
if(percentage < 100) { | |
msg = ' ' + msg; | |
} | |
if(percentage < 10) { | |
msg = ' ' + msg; | |
} | |
} | |
state = state.replace(/^\d+\/\d+\s+/, ''); | |
if (percentage === 0) { | |
lastState = null; | |
lastStateTime = +new Date(); | |
} else if (state !== lastState || percentage === 1) { | |
var now = +new Date(); | |
if (lastState) { | |
var stateMsg = (now - lastStateTime) + 'ms ' + lastState; | |
goToLineStart(stateMsg); | |
process.stderr.write(stateMsg + '\n'); | |
chars = 0; | |
} | |
lastState = state; | |
lastStateTime = now; | |
} | |
goToLineStart(msg); | |
process.stderr.write(msg); | |
}); | |
function goToLineStart(nextMessage) { | |
var str = ''; | |
for(; chars > nextMessage.length; chars--) { | |
str += '\b \b'; | |
} | |
chars = nextMessage.length; | |
for(var i = 0; i < chars; i++) { | |
str += '\b'; | |
} | |
if (str) { | |
process.stderr.write(str); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment