| 
									
										
										
										
											2015-10-22 03:05:01 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 23:16:56 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2015-10-22 03:05:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-14 19:06:59 +08:00
										 |  |  | const { UsageState } = require("./ModuleGraph"); | 
					
						
							| 
									
										
										
										
											2018-07-31 04:30:27 +08:00
										 |  |  | const { STAGE_DEFAULT } = require("./OptimizationStages"); | 
					
						
							| 
									
										
										
										
											2019-05-29 17:51:47 +08:00
										 |  |  | const { NS_OBJECT_IMPORTED } = require("./dependencies/DependencyReference"); | 
					
						
							| 
									
										
										
										
											2019-01-23 19:12:44 +08:00
										 |  |  | const Queue = require("./util/Queue"); | 
					
						
							| 
									
										
										
										
											2018-08-07 01:39:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-28 03:12:09 +08:00
										 |  |  | /** @typedef {import("./Compiler")} Compiler */ | 
					
						
							| 
									
										
										
										
											2018-06-08 16:34:38 +08:00
										 |  |  | /** @typedef {import("./DependenciesBlock")} DependenciesBlock */ | 
					
						
							| 
									
										
										
										
											2018-08-07 01:39:43 +08:00
										 |  |  | /** @typedef {import("./Dependency")} Dependency */ | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | /** @typedef {import("./Module")} Module */ | 
					
						
							| 
									
										
										
										
											2019-03-14 19:06:59 +08:00
										 |  |  | /** @typedef {import("./ModuleGraph").ExportsInfo} ExportsInfo */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 23:16:56 +08:00
										 |  |  | class FlagDependencyUsagePlugin { | 
					
						
							| 
									
										
										
										
											2018-07-28 03:12:09 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-02-23 23:16:56 +08:00
										 |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		compiler.hooks.compilation.tap("FlagDependencyUsagePlugin", compilation => { | 
					
						
							| 
									
										
										
										
											2018-08-07 01:39:43 +08:00
										 |  |  | 			const moduleGraph = compilation.moduleGraph; | 
					
						
							| 
									
										
										
										
											2018-05-27 05:07:02 +08:00
										 |  |  | 			compilation.hooks.optimizeDependencies.tap( | 
					
						
							| 
									
										
										
										
											2018-12-09 19:54:17 +08:00
										 |  |  | 				{ | 
					
						
							| 
									
										
										
										
											2018-07-31 04:30:27 +08:00
										 |  |  | 					name: "FlagDependencyUsagePlugin", | 
					
						
							|  |  |  | 					stage: STAGE_DEFAULT | 
					
						
							| 
									
										
										
										
											2018-12-09 19:54:17 +08:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				modules => { | 
					
						
							| 
									
										
										
										
											2019-03-14 19:06:59 +08:00
										 |  |  | 					/** @type {Map<ExportsInfo, Module>} */ | 
					
						
							|  |  |  | 					const exportInfoToModuleMap = new Map(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-20 22:24:35 +08:00
										 |  |  | 					/** | 
					
						
							| 
									
										
										
										
											2019-03-14 19:06:59 +08:00
										 |  |  | 					 * @typedef {string[]} StringArray | 
					
						
							| 
									
										
										
										
											2018-07-20 22:24:35 +08:00
										 |  |  | 					 * @param {Module} module module to process | 
					
						
							| 
									
										
										
										
											2019-03-14 19:06:59 +08:00
										 |  |  | 					 * @param {StringArray[]} usedExports list of used exports | 
					
						
							| 
									
										
										
										
											2018-07-20 22:24:35 +08:00
										 |  |  | 					 * @returns {void} | 
					
						
							|  |  |  | 					 */ | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					const processModule = (module, usedExports) => { | 
					
						
							| 
									
										
										
										
											2019-01-23 19:12:44 +08:00
										 |  |  | 						const exportsInfo = moduleGraph.getExportsInfo(module); | 
					
						
							| 
									
										
										
										
											2019-03-14 19:06:59 +08:00
										 |  |  | 						if (usedExports.length > 0) { | 
					
						
							|  |  |  | 							for (const usedExport of usedExports) { | 
					
						
							|  |  |  | 								if (usedExport.length === 0) { | 
					
						
							|  |  |  | 									if (exportsInfo.setUsedInUnknownWay()) { | 
					
						
							|  |  |  | 										queue.enqueue(module); | 
					
						
							| 
									
										
										
										
											2019-01-28 17:40:32 +08:00
										 |  |  | 									} | 
					
						
							|  |  |  | 								} else { | 
					
						
							| 
									
										
										
										
											2019-03-14 19:06:59 +08:00
										 |  |  | 									if ( | 
					
						
							|  |  |  | 										usedExport[0] === "default" && | 
					
						
							|  |  |  | 										module.buildMeta.exportsType === "named" | 
					
						
							|  |  |  | 									) { | 
					
						
							|  |  |  | 										if (exportsInfo.setUsedAsNamedExportType()) { | 
					
						
							|  |  |  | 											queue.enqueue(module); | 
					
						
							|  |  |  | 										} | 
					
						
							|  |  |  | 									} else { | 
					
						
							|  |  |  | 										let currentExportsInfo = exportsInfo; | 
					
						
							|  |  |  | 										for (let i = 0; i < usedExport.length; i++) { | 
					
						
							|  |  |  | 											const exportName = usedExport[i]; | 
					
						
							|  |  |  | 											const exportInfo = currentExportsInfo.getExportInfo( | 
					
						
							|  |  |  | 												exportName | 
					
						
							|  |  |  | 											); | 
					
						
							|  |  |  | 											const lastOne = i === usedExport.length - 1; | 
					
						
							|  |  |  | 											const nestedInfo = exportInfo.exportsInfo; | 
					
						
							|  |  |  | 											if (!nestedInfo || lastOne) { | 
					
						
							|  |  |  | 												if (exportInfo.used !== UsageState.Used) { | 
					
						
							|  |  |  | 													exportInfo.used = UsageState.Used; | 
					
						
							| 
									
										
										
										
											2019-05-30 03:58:26 +08:00
										 |  |  | 													const currentModule = | 
					
						
							|  |  |  | 														currentExportsInfo === exportsInfo | 
					
						
							|  |  |  | 															? module | 
					
						
							|  |  |  | 															: exportInfoToModuleMap.get(currentExportsInfo); | 
					
						
							| 
									
										
										
										
											2019-03-14 19:06:59 +08:00
										 |  |  | 													if (currentModule) { | 
					
						
							|  |  |  | 														queue.enqueue(currentModule); | 
					
						
							|  |  |  | 													} | 
					
						
							|  |  |  | 												} | 
					
						
							|  |  |  | 												break; | 
					
						
							|  |  |  | 											} else { | 
					
						
							|  |  |  | 												if (exportInfo.used === UsageState.Unused) { | 
					
						
							|  |  |  | 													exportInfo.used = UsageState.OnlyPropertiesUsed; | 
					
						
							| 
									
										
										
										
											2019-05-30 03:58:26 +08:00
										 |  |  | 													const currentModule = | 
					
						
							|  |  |  | 														currentExportsInfo === exportsInfo | 
					
						
							|  |  |  | 															? module | 
					
						
							|  |  |  | 															: exportInfoToModuleMap.get(currentExportsInfo); | 
					
						
							| 
									
										
										
										
											2019-03-14 19:06:59 +08:00
										 |  |  | 													if (currentModule) { | 
					
						
							|  |  |  | 														queue.enqueue(currentModule); | 
					
						
							|  |  |  | 													} | 
					
						
							|  |  |  | 												} | 
					
						
							|  |  |  | 												currentExportsInfo = nestedInfo; | 
					
						
							|  |  |  | 											} | 
					
						
							|  |  |  | 										} | 
					
						
							| 
									
										
										
										
											2019-01-28 17:40:32 +08:00
										 |  |  | 									} | 
					
						
							| 
									
										
										
										
											2018-08-07 01:39:43 +08:00
										 |  |  | 								} | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 							} | 
					
						
							|  |  |  | 						} else { | 
					
						
							| 
									
										
										
										
											2019-01-23 19:12:44 +08:00
										 |  |  | 							// for a module without side effects we stop tracking usage here when no export is used
 | 
					
						
							|  |  |  | 							// This module won't be evaluated in this case
 | 
					
						
							| 
									
										
										
										
											2019-05-29 17:51:47 +08:00
										 |  |  | 							// TODO webpack 6 remove this check
 | 
					
						
							| 
									
										
										
										
											2019-01-23 19:12:44 +08:00
										 |  |  | 							if (module.factoryMeta.sideEffectFree) return; | 
					
						
							| 
									
										
										
										
											2019-03-14 19:06:59 +08:00
										 |  |  | 							if (exportsInfo.setUsedForSideEffectsOnly()) { | 
					
						
							|  |  |  | 								queue.enqueue(module); | 
					
						
							|  |  |  | 							} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						} | 
					
						
							|  |  |  | 					}; | 
					
						
							| 
									
										
										
										
											2015-10-22 03:05:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-07 01:39:43 +08:00
										 |  |  | 					/** | 
					
						
							|  |  |  | 					 * @param {DependenciesBlock} depBlock the dependencies block | 
					
						
							|  |  |  | 					 * @returns {void} | 
					
						
							|  |  |  | 					 */ | 
					
						
							| 
									
										
										
										
											2019-01-23 19:12:44 +08:00
										 |  |  | 					const processDependenciesBlock = depBlock => { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						for (const dep of depBlock.dependencies) { | 
					
						
							| 
									
										
										
										
											2019-01-23 19:12:44 +08:00
										 |  |  | 							processDependency(dep); | 
					
						
							| 
									
										
										
										
											2018-01-22 20:52:43 +08:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						for (const block of depBlock.blocks) { | 
					
						
							| 
									
										
										
										
											2019-01-23 19:12:44 +08:00
										 |  |  | 							queue.enqueue(block); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						} | 
					
						
							|  |  |  | 					}; | 
					
						
							| 
									
										
										
										
											2016-12-05 06:47:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-07 01:39:43 +08:00
										 |  |  | 					/** | 
					
						
							|  |  |  | 					 * @param {Dependency} dep the dependency | 
					
						
							|  |  |  | 					 * @returns {void} | 
					
						
							|  |  |  | 					 */ | 
					
						
							| 
									
										
										
										
											2019-01-23 19:12:44 +08:00
										 |  |  | 					const processDependency = dep => { | 
					
						
							| 
									
										
										
										
											2019-01-23 17:29:35 +08:00
										 |  |  | 						const reference = compilation.getDependencyReference(dep); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						if (!reference) return; | 
					
						
							| 
									
										
										
										
											2018-06-08 16:34:38 +08:00
										 |  |  | 						const referenceModule = reference.module; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						const importedNames = reference.importedNames; | 
					
						
							| 
									
										
										
										
											2019-03-14 19:06:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-29 17:51:47 +08:00
										 |  |  | 						processModule(referenceModule, importedNames); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					}; | 
					
						
							| 
									
										
										
										
											2015-10-22 03:05:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					for (const module of modules) { | 
					
						
							| 
									
										
										
										
											2019-03-14 19:06:59 +08:00
										 |  |  | 						const exportsInfo = moduleGraph.getExportsInfo(module); | 
					
						
							|  |  |  | 						exportInfoToModuleMap.set(exportsInfo, module); | 
					
						
							|  |  |  | 						exportsInfo.setHasUseInfo(); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2015-10-22 03:05:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-23 19:12:44 +08:00
										 |  |  | 					/** @type {Queue<DependenciesBlock>} */ | 
					
						
							|  |  |  | 					const queue = new Queue(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-21 02:30:27 +08:00
										 |  |  | 					for (const deps of compilation.entryDependencies.values()) { | 
					
						
							|  |  |  | 						for (const dep of deps) { | 
					
						
							|  |  |  | 							const module = moduleGraph.getModule(dep); | 
					
						
							| 
									
										
										
										
											2018-08-14 22:40:37 +08:00
										 |  |  | 							if (module) { | 
					
						
							| 
									
										
										
										
											2019-05-29 17:51:47 +08:00
										 |  |  | 								processModule(module, NS_OBJECT_IMPORTED); | 
					
						
							| 
									
										
										
										
											2018-08-14 22:40:37 +08:00
										 |  |  | 							} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2017-02-23 23:16:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					while (queue.length) { | 
					
						
							| 
									
										
										
										
											2019-01-23 19:12:44 +08:00
										 |  |  | 						const depBlock = queue.dequeue(); | 
					
						
							|  |  |  | 						processDependenciesBlock(depBlock); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			); | 
					
						
							| 
									
										
										
										
											2017-02-23 23:16:56 +08:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-07-28 03:12:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 23:16:56 +08:00
										 |  |  | module.exports = FlagDependencyUsagePlugin; |