| 
									
										
										
										
											2015-10-22 03:05:01 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | function FlagDependencyUsagePlugin() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | module.exports = FlagDependencyUsagePlugin; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | FlagDependencyUsagePlugin.prototype.apply = function(compiler) { | 
					
						
							|  |  |  | 	compiler.plugin("compilation", function(compilation) { | 
					
						
							|  |  |  | 		compilation.plugin("optimize-modules-advanced", function(modules) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			modules.forEach(function(module) { | 
					
						
							|  |  |  | 				module.used = false; | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-05 06:47:19 +08:00
										 |  |  | 			var queue = []; | 
					
						
							| 
									
										
										
										
											2015-10-22 03:05:01 +08:00
										 |  |  | 			compilation.chunks.forEach(function(chunk) { | 
					
						
							|  |  |  | 				if(chunk.entryModule) { | 
					
						
							|  |  |  | 					processModule(chunk.entryModule, true); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-05 06:47:19 +08:00
										 |  |  | 			while(queue.length) { | 
					
						
							|  |  |  | 				var queueItem = queue.pop(); | 
					
						
							|  |  |  | 				processDependenciesBlock(queueItem[0], queueItem[1]); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-10-22 03:05:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-05 06:47:19 +08:00
										 |  |  | 			function processModule(module, usedExports) { | 
					
						
							|  |  |  | 				module.used = true; | 
					
						
							|  |  |  | 				if(module.usedExports === true) | 
					
						
							|  |  |  | 					return; | 
					
						
							|  |  |  | 				else if(usedExports === true) | 
					
						
							|  |  |  | 					module.usedExports = true; | 
					
						
							|  |  |  | 				else if(Array.isArray(usedExports)) { | 
					
						
							|  |  |  | 					var old = module.usedExports ? module.usedExports.length : -1; | 
					
						
							|  |  |  | 					module.usedExports = addToSet(module.usedExports || [], usedExports); | 
					
						
							|  |  |  | 					if(module.usedExports.length === old) | 
					
						
							|  |  |  | 						return; | 
					
						
							| 
									
										
										
										
											2016-12-05 06:47:19 +08:00
										 |  |  | 				} else if(Array.isArray(module.usedExports)) | 
					
						
							| 
									
										
										
										
											2016-12-05 06:47:19 +08:00
										 |  |  | 					return; | 
					
						
							|  |  |  | 				else | 
					
						
							|  |  |  | 					module.usedExports = false; | 
					
						
							| 
									
										
										
										
											2015-10-22 03:05:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-05 06:47:19 +08:00
										 |  |  | 				queue.push([module, module.usedExports]); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-10-22 03:05:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-05 06:47:19 +08:00
										 |  |  | 			function processDependenciesBlock(depBlock, usedExports) { | 
					
						
							|  |  |  | 				depBlock.dependencies.forEach(function(dep) { | 
					
						
							| 
									
										
										
										
											2015-10-22 03:05:01 +08:00
										 |  |  | 					processDependency(dep, usedExports); | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2016-12-05 06:47:19 +08:00
										 |  |  | 				depBlock.variables.forEach(function(variable) { | 
					
						
							|  |  |  | 					variable.dependencies.forEach(function(dep) { | 
					
						
							|  |  |  | 						processDependency(dep, usedExports); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 				depBlock.blocks.forEach(function(block) { | 
					
						
							|  |  |  | 					queue.push([block, usedExports]); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-10-22 03:05:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-05 06:47:19 +08:00
										 |  |  | 			function processDependency(dep, usedExports) { | 
					
						
							|  |  |  | 				var reference = dep.getReference && dep.getReference(); | 
					
						
							|  |  |  | 				if(!reference) return; | 
					
						
							|  |  |  | 				var module = reference.module; | 
					
						
							|  |  |  | 				var importedNames = reference.importedNames; | 
					
						
							|  |  |  | 				var oldUsed = module.used; | 
					
						
							|  |  |  | 				var oldUsedExports = module.usedExports; | 
					
						
							|  |  |  | 				if(!oldUsed || (importedNames && (!oldUsedExports || !isSubset(oldUsedExports, importedNames)))) { | 
					
						
							|  |  |  | 					processModule(module, importedNames); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2015-10-22 03:05:01 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-12-05 06:47:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2015-10-22 03:05:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		function addToSet(a, b) { | 
					
						
							|  |  |  | 			b.forEach(function(item) { | 
					
						
							|  |  |  | 				if(a.indexOf(item) < 0) | 
					
						
							|  |  |  | 					a.push(item); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 			return a; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		function isSubset(biggerSet, subset) { | 
					
						
							|  |  |  | 			if(biggerSet === true) return true; | 
					
						
							|  |  |  | 			if(subset === true) return false; | 
					
						
							|  |  |  | 			return subset.every(function(item) { | 
					
						
							|  |  |  | 				return biggerSet.indexOf(item) >= 0; | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; |