mirror of https://github.com/webpack/webpack.git
				
				
				
			Merge pull request #1738 from lexaurin/master
JS API: ProgressPlugin default handler
This commit is contained in:
		
						commit
						07c1908bd0
					
				|  | @ -382,52 +382,7 @@ module.exports = function(optimist, argv, convertOptions) { | ||||||
| 		ifBooleanArg("progress", function() { | 		ifBooleanArg("progress", function() { | ||||||
| 			var ProgressPlugin = require("../lib/ProgressPlugin"); | 			var ProgressPlugin = require("../lib/ProgressPlugin"); | ||||||
| 			ensureArray(options, "plugins"); | 			ensureArray(options, "plugins"); | ||||||
| 			var chars = 0, | 			options.plugins.push(new ProgressPlugin()); | ||||||
| 				lastState, lastStateTime; |  | ||||||
| 			options.plugins.push(new 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; |  | ||||||
| 					} |  | ||||||
| 				} |  | ||||||
| 				if(options.profile) { |  | ||||||
| 					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); |  | ||||||
| 			} |  | ||||||
| 		}); | 		}); | ||||||
| 
 | 
 | ||||||
| 		ifArg("devtool", function(value) { | 		ifArg("devtool", function(value) { | ||||||
|  |  | ||||||
|  | @ -8,7 +8,7 @@ function ProgressPlugin(handler) { | ||||||
| module.exports = ProgressPlugin; | module.exports = ProgressPlugin; | ||||||
| 
 | 
 | ||||||
| ProgressPlugin.prototype.apply = function(compiler) { | ProgressPlugin.prototype.apply = function(compiler) { | ||||||
| 	var handler = this.handler; | 	var handler = this.handler || defaultHandler; | ||||||
| 	if(compiler.compilers) { | 	if(compiler.compilers) { | ||||||
| 		var states = new Array(compiler.compilers.length); | 		var states = new Array(compiler.compilers.length); | ||||||
| 		compiler.compilers.forEach(function(compiler, idx) { | 		compiler.compilers.forEach(function(compiler, idx) { | ||||||
|  | @ -77,4 +77,53 @@ ProgressPlugin.prototype.apply = function(compiler) { | ||||||
| 			handler(1, ""); | 			handler(1, ""); | ||||||
| 		}); | 		}); | ||||||
| 	} | 	} | ||||||
|  | 
 | ||||||
|  | 	var chars = 0, | ||||||
|  | 		lastState, lastStateTime; | ||||||
|  | 
 | ||||||
|  | 	function defaultHandler(percentage, msg) { | ||||||
|  | 		var state = 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); | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		if(percentage < 1) { | ||||||
|  | 			percentage = Math.floor(percentage * 100); | ||||||
|  | 			msg = percentage + "% " + msg; | ||||||
|  | 			if(percentage < 100) { | ||||||
|  | 				msg = " " + msg; | ||||||
|  | 			} | ||||||
|  | 			if(percentage < 10) { | ||||||
|  | 				msg = " " + msg; | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		if(compiler.options.profile) { | ||||||
|  | 			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); | ||||||
|  | 	} | ||||||
| }; | }; | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue