| 
									
										
										
										
											2015-10-22 03:05:01 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2017-02-23 23:16:56 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2015-10-22 03:05:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | const addToSet = (a, b) => { | 
					
						
							|  |  |  | 	b.forEach(item => { | 
					
						
							|  |  |  | 		if(a.indexOf(item) < 0) | 
					
						
							|  |  |  | 			a.push(item); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	return a; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const isSubset = (biggerSet, subset) => { | 
					
						
							|  |  |  | 	if(biggerSet === true) return true; | 
					
						
							|  |  |  | 	if(subset === true) return false; | 
					
						
							|  |  |  | 	return subset.every(item => biggerSet.indexOf(item) >= 0); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 23:16:56 +08:00
										 |  |  | class FlagDependencyUsagePlugin { | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							|  |  |  | 		compiler.plugin("compilation", compilation => { | 
					
						
							|  |  |  | 			compilation.plugin("optimize-modules-advanced", modules => { | 
					
						
							| 
									
										
										
										
											2015-10-22 03:05:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | 				const processModule = (module, usedExports) => { | 
					
						
							| 
									
										
										
										
											2017-02-23 23:16:56 +08:00
										 |  |  | 					module.used = true; | 
					
						
							|  |  |  | 					if(module.usedExports === true) | 
					
						
							| 
									
										
										
										
											2016-12-05 06:47:19 +08:00
										 |  |  | 						return; | 
					
						
							| 
									
										
										
										
											2017-02-23 23:16:56 +08:00
										 |  |  | 					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; | 
					
						
							|  |  |  | 					} else if(Array.isArray(module.usedExports)) | 
					
						
							|  |  |  | 						return; | 
					
						
							|  |  |  | 					else | 
					
						
							|  |  |  | 						module.usedExports = false; | 
					
						
							| 
									
										
										
										
											2015-10-22 03:05:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 19:35:25 +08:00
										 |  |  | 					// for a module without side effects we stop tracking usage here when no export is used
 | 
					
						
							| 
									
										
										
										
											2017-08-08 15:40:17 +08:00
										 |  |  | 					// This module won't be evaluated in this case
 | 
					
						
							| 
									
										
										
										
											2017-09-14 19:35:25 +08:00
										 |  |  | 					if(module.sideEffectFree) { | 
					
						
							| 
									
										
										
										
											2017-08-08 15:40:17 +08:00
										 |  |  | 						if(module.usedExports === false) return; | 
					
						
							|  |  |  | 						if(Array.isArray(module.usedExports) && module.usedExports.length === 0) return; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 23:16:56 +08:00
										 |  |  | 					queue.push([module, module.usedExports]); | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | 				}; | 
					
						
							| 
									
										
										
										
											2015-10-22 03:05:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | 				const processDependenciesBlock = (depBlock, usedExports) => { | 
					
						
							| 
									
										
										
										
											2017-03-18 20:33:50 +08:00
										 |  |  | 					depBlock.dependencies.forEach(dep => processDependency(dep)); | 
					
						
							|  |  |  | 					depBlock.variables.forEach(variable => variable.dependencies.forEach(dep => processDependency(dep))); | 
					
						
							| 
									
										
										
										
											2017-02-23 23:16:56 +08:00
										 |  |  | 					depBlock.blocks.forEach(block => queue.push([block, usedExports])); | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | 				}; | 
					
						
							| 
									
										
										
										
											2016-12-05 06:47:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | 				const processDependency = dep => { | 
					
						
							| 
									
										
										
										
											2017-02-23 23:16:56 +08:00
										 |  |  | 					const reference = dep.getReference && dep.getReference(); | 
					
						
							|  |  |  | 					if(!reference) return; | 
					
						
							|  |  |  | 					const module = reference.module; | 
					
						
							|  |  |  | 					const importedNames = reference.importedNames; | 
					
						
							|  |  |  | 					const oldUsed = module.used; | 
					
						
							|  |  |  | 					const oldUsedExports = module.usedExports; | 
					
						
							|  |  |  | 					if(!oldUsed || (importedNames && (!oldUsedExports || !isSubset(oldUsedExports, importedNames)))) { | 
					
						
							|  |  |  | 						processModule(module, importedNames); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | 				}; | 
					
						
							| 
									
										
										
										
											2015-10-22 03:05:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | 				modules.forEach(module => module.used = false); | 
					
						
							| 
									
										
										
										
											2015-10-22 03:05:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | 				const queue = []; | 
					
						
							|  |  |  | 				compilation.chunks.forEach(chunk => { | 
					
						
							|  |  |  | 					if(chunk.entryModule) { | 
					
						
							|  |  |  | 						processModule(chunk.entryModule, true); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2017-02-23 23:16:56 +08:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | 				while(queue.length) { | 
					
						
							|  |  |  | 					const queueItem = queue.pop(); | 
					
						
							|  |  |  | 					processDependenciesBlock(queueItem[0], queueItem[1]); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-02-23 23:16:56 +08:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | module.exports = FlagDependencyUsagePlugin; |