| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2016-01-10 06:48:37 +08:00
										 |  |  | function ProgressPlugin(options) { | 
					
						
							|  |  |  | 	if(typeof options === "function") { | 
					
						
							|  |  |  | 		options = { | 
					
						
							|  |  |  | 			handler: options | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	options = options || {}; | 
					
						
							|  |  |  | 	this.profile = options.profile; | 
					
						
							|  |  |  | 	this.handler = options.handler; | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | } | 
					
						
							|  |  |  | module.exports = ProgressPlugin; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ProgressPlugin.prototype.apply = function(compiler) { | 
					
						
							| 
									
										
										
										
											2015-12-09 23:10:38 +08:00
										 |  |  | 	var handler = this.handler || defaultHandler; | 
					
						
							| 
									
										
										
										
											2016-01-10 06:48:37 +08:00
										 |  |  | 	var profile = this.profile; | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 	if(compiler.compilers) { | 
					
						
							| 
									
										
										
										
											2014-06-19 05:02:33 +08:00
										 |  |  | 		var states = new Array(compiler.compilers.length); | 
					
						
							|  |  |  | 		compiler.compilers.forEach(function(compiler, idx) { | 
					
						
							|  |  |  | 			compiler.apply(new ProgressPlugin(function(p, msg) { | 
					
						
							| 
									
										
										
										
											2015-11-22 05:59:08 +08:00
										 |  |  | 				states[idx] = Array.prototype.slice.apply(arguments); | 
					
						
							| 
									
										
										
										
											2016-01-12 03:09:30 +08:00
										 |  |  | 				handler.apply(null, [ | 
					
						
							|  |  |  | 					states.map(function(state) { | 
					
						
							|  |  |  | 						return state && state[0] || 0; | 
					
						
							|  |  |  | 					}).reduce(function(a, b) { | 
					
						
							|  |  |  | 						return a + b; | 
					
						
							|  |  |  | 					}) / states.length, | 
					
						
							|  |  |  | 					"[" + idx + "] " + msg | 
					
						
							|  |  |  | 				].concat(Array.prototype.slice.call(arguments, 2))); | 
					
						
							| 
									
										
										
										
											2014-06-19 05:02:33 +08:00
										 |  |  | 			})); | 
					
						
							| 
									
										
										
										
											2013-07-04 17:02:35 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-06-19 05:02:33 +08:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		var lastModulesCount = 0; | 
					
						
							| 
									
										
										
										
											2015-11-22 05:59:08 +08:00
										 |  |  | 		var moduleCount = 500; | 
					
						
							| 
									
										
										
										
											2014-06-19 05:02:33 +08:00
										 |  |  | 		var doneModules = 0; | 
					
						
							| 
									
										
										
										
											2015-11-22 05:59:08 +08:00
										 |  |  | 		var activeModules = []; | 
					
						
							| 
									
										
										
										
											2015-07-13 06:20:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-22 05:59:08 +08:00
										 |  |  | 		function update(module) { | 
					
						
							|  |  |  | 			handler( | 
					
						
							|  |  |  | 				0.1 + (doneModules / Math.max(lastModulesCount, moduleCount)) * 0.6, | 
					
						
							|  |  |  | 				"building modules", | 
					
						
							|  |  |  | 				doneModules + "/" + moduleCount + " modules", | 
					
						
							|  |  |  | 				activeModules.length + " active", | 
					
						
							|  |  |  | 				activeModules[activeModules.length - 1] | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		function moduleDone(module) { | 
					
						
							|  |  |  | 			doneModules++; | 
					
						
							|  |  |  | 			var ident = module.identifier(); | 
					
						
							|  |  |  | 			if(ident) { | 
					
						
							|  |  |  | 				var idx = activeModules.indexOf(ident); | 
					
						
							|  |  |  | 				if(idx >= 0) activeModules.splice(idx, 1); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			update(); | 
					
						
							| 
									
										
										
										
											2014-06-19 05:02:33 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		compiler.plugin("compilation", function(compilation) { | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 			if(compilation.compiler.isChild()) return; | 
					
						
							| 
									
										
										
										
											2014-06-19 05:02:33 +08:00
										 |  |  | 			lastModulesCount = moduleCount; | 
					
						
							|  |  |  | 			moduleCount = 0; | 
					
						
							|  |  |  | 			doneModules = 0; | 
					
						
							| 
									
										
										
										
											2015-11-22 05:59:08 +08:00
										 |  |  | 			handler(0, "compiling"); | 
					
						
							|  |  |  | 			compilation.plugin("build-module", function(module) { | 
					
						
							| 
									
										
										
										
											2014-06-19 05:02:33 +08:00
										 |  |  | 				moduleCount++; | 
					
						
							| 
									
										
										
										
											2015-11-22 05:59:08 +08:00
										 |  |  | 				var ident = module.identifier(); | 
					
						
							|  |  |  | 				if(ident) { | 
					
						
							|  |  |  | 					activeModules.push(ident); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-06-19 05:02:33 +08:00
										 |  |  | 				update(); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2015-11-22 05:59:08 +08:00
										 |  |  | 			compilation.plugin("failed-module", moduleDone); | 
					
						
							|  |  |  | 			compilation.plugin("succeed-module", moduleDone); | 
					
						
							|  |  |  | 			var syncHooks = { | 
					
						
							|  |  |  | 				"seal": [0.71, "sealing"], | 
					
						
							|  |  |  | 				"optimize": [0.72, "optimizing"], | 
					
						
							|  |  |  | 				"optimize-modules-basic": [0.73, "basic module optimization"], | 
					
						
							|  |  |  | 				"optimize-modules": [0.74, "module optimization"], | 
					
						
							|  |  |  | 				"optimize-modules-advanced": [0.75, "advanced module optimization"], | 
					
						
							|  |  |  | 				"optimize-chunks-basic": [0.76, "basic chunk optimization"], | 
					
						
							|  |  |  | 				"optimize-chunks": [0.77, "chunk optimization"], | 
					
						
							|  |  |  | 				"optimize-chunks-advanced": [0.78, "advanced chunk optimization"], | 
					
						
							|  |  |  | 				// optimize-tree
 | 
					
						
							|  |  |  | 				"revive-modules": [0.80, "module reviving"], | 
					
						
							|  |  |  | 				"optimize-module-order": [0.81, "module order optimization"], | 
					
						
							|  |  |  | 				"optimize-module-ids": [0.82, "module id optimization"], | 
					
						
							|  |  |  | 				"revive-chunks": [0.83, "chunk reviving"], | 
					
						
							|  |  |  | 				"optimize-chunk-order": [0.84, "chunk order optimization"], | 
					
						
							|  |  |  | 				"optimize-chunk-ids": [0.85, "chunk id optimization"], | 
					
						
							|  |  |  | 				"before-hash": [0.86, "hashing"], | 
					
						
							|  |  |  | 				"before-module-assets": [0.87, "module assets processing"], | 
					
						
							|  |  |  | 				"before-chunk-assets": [0.88, "chunk assets processing"], | 
					
						
							|  |  |  | 				"additional-chunk-assets": [0.89, "additional chunk assets processing"], | 
					
						
							|  |  |  | 				"record": [0.90, "recording"] | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 			Object.keys(syncHooks).forEach(function(name) { | 
					
						
							|  |  |  | 				var pass = 0; | 
					
						
							|  |  |  | 				var settings = syncHooks[name]; | 
					
						
							|  |  |  | 				compilation.plugin(name, function() { | 
					
						
							|  |  |  | 					if(pass++ > 0) | 
					
						
							|  |  |  | 						handler(settings[0], settings[1], "pass " + pass); | 
					
						
							|  |  |  | 					else | 
					
						
							|  |  |  | 						handler(settings[0], settings[1]); | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2015-02-05 06:20:36 +08:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2015-11-22 05:59:08 +08:00
										 |  |  | 			compilation.plugin("optimize-tree", function(chunks, modules, callback) { | 
					
						
							|  |  |  | 				handler(0.79, "module and chunk tree optimization"); | 
					
						
							|  |  |  | 				callback(); | 
					
						
							| 
									
										
										
										
											2014-06-19 05:02:33 +08:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2015-11-22 05:59:08 +08:00
										 |  |  | 			compilation.plugin("additional-assets", function(callback) { | 
					
						
							|  |  |  | 				handler(0.91, "additional asset processing"); | 
					
						
							|  |  |  | 				callback(); | 
					
						
							| 
									
										
										
										
											2014-06-19 05:02:33 +08:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 			compilation.plugin("optimize-chunk-assets", function(chunks, callback) { | 
					
						
							| 
									
										
										
										
											2015-11-22 05:59:08 +08:00
										 |  |  | 				handler(0.92, "chunk asset optimization"); | 
					
						
							| 
									
										
										
										
											2014-06-19 05:02:33 +08:00
										 |  |  | 				callback(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 			compilation.plugin("optimize-assets", function(assets, callback) { | 
					
						
							| 
									
										
										
										
											2015-11-22 05:59:08 +08:00
										 |  |  | 				handler(0.94, "asset optimization"); | 
					
						
							| 
									
										
										
										
											2014-06-19 05:02:33 +08:00
										 |  |  | 				callback(); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-10-13 04:40:15 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-06-19 05:02:33 +08:00
										 |  |  | 		compiler.plugin("emit", function(compilation, callback) { | 
					
						
							| 
									
										
										
										
											2015-11-22 05:59:08 +08:00
										 |  |  | 			handler(0.95, "emitting"); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 			callback(); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2015-04-24 05:55:50 +08:00
										 |  |  | 		compiler.plugin("done", function() { | 
					
						
							| 
									
										
										
										
											2014-06-19 05:02:33 +08:00
										 |  |  | 			handler(1, ""); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-06-19 05:02:33 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-12-09 23:10:38 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	var chars = 0, | 
					
						
							|  |  |  | 		lastState, lastStateTime; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function defaultHandler(percentage, msg) { | 
					
						
							|  |  |  | 		var state = msg; | 
					
						
							| 
									
										
										
										
											2015-11-22 05:59:08 +08:00
										 |  |  | 		var details = Array.prototype.slice.call(arguments, 2); | 
					
						
							| 
									
										
										
										
											2015-12-09 23:10:38 +08:00
										 |  |  | 		if(percentage < 1) { | 
					
						
							|  |  |  | 			percentage = Math.floor(percentage * 100); | 
					
						
							|  |  |  | 			msg = percentage + "% " + msg; | 
					
						
							|  |  |  | 			if(percentage < 100) { | 
					
						
							|  |  |  | 				msg = " " + msg; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if(percentage < 10) { | 
					
						
							|  |  |  | 				msg = " " + msg; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-11-22 05:59:08 +08:00
										 |  |  | 			details.forEach(function(detail) { | 
					
						
							|  |  |  | 				if(!detail) return; | 
					
						
							|  |  |  | 				if(detail.length > 40) { | 
					
						
							|  |  |  | 					detail = "..." + detail.substr(detail.length - 37); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				msg += " " + detail | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2015-12-09 23:10:38 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-01-10 06:48:37 +08:00
										 |  |  | 		if(profile) { | 
					
						
							| 
									
										
										
										
											2015-12-09 23:10:38 +08:00
										 |  |  | 			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); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-11-22 05:59:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	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); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-02-05 06:20:36 +08:00
										 |  |  | }; |