| 
									
										
										
										
											2018-09-04 01:23:27 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Florent Cailhol @ooflorent | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-04 15:04:05 +08:00
										 |  |  | const { | 
					
						
							|  |  |  | 	compareModulesByPreOrderIndexOrIdentifier | 
					
						
							|  |  |  | } = require("../util/comparators"); | 
					
						
							| 
									
										
										
										
											2018-12-07 19:26:35 +08:00
										 |  |  | const { assignAscendingModuleIds } = require("./IdHelpers"); | 
					
						
							| 
									
										
										
										
											2018-09-04 01:23:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** @typedef {import("../Compiler")} Compiler */ | 
					
						
							|  |  |  | /** @typedef {import("../Module")} Module */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class NaturalModuleIdsPlugin { | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2020-04-23 16:48:36 +08:00
										 |  |  | 	 * Apply the plugin | 
					
						
							| 
									
										
										
										
											2018-09-04 01:23:27 +08:00
										 |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							|  |  |  | 		compiler.hooks.compilation.tap("NaturalModuleIdsPlugin", compilation => { | 
					
						
							|  |  |  | 			compilation.hooks.moduleIds.tap("NaturalModuleIdsPlugin", modules => { | 
					
						
							| 
									
										
										
										
											2018-09-04 14:52:22 +08:00
										 |  |  | 				const chunkGraph = compilation.chunkGraph; | 
					
						
							|  |  |  | 				const modulesInNaturalOrder = Array.from(modules) | 
					
						
							| 
									
										
										
										
											2019-09-26 21:51:40 +08:00
										 |  |  | 					.filter( | 
					
						
							|  |  |  | 						m => | 
					
						
							|  |  |  | 							m.needId && | 
					
						
							|  |  |  | 							chunkGraph.getNumberOfModuleChunks(m) > 0 && | 
					
						
							|  |  |  | 							chunkGraph.getModuleId(m) === null | 
					
						
							|  |  |  | 					) | 
					
						
							| 
									
										
										
										
											2018-09-04 15:04:05 +08:00
										 |  |  | 					.sort( | 
					
						
							|  |  |  | 						compareModulesByPreOrderIndexOrIdentifier(compilation.moduleGraph) | 
					
						
							|  |  |  | 					); | 
					
						
							| 
									
										
										
										
											2018-09-05 20:22:10 +08:00
										 |  |  | 				assignAscendingModuleIds(modulesInNaturalOrder, compilation); | 
					
						
							| 
									
										
										
										
											2018-09-04 01:23:27 +08:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = NaturalModuleIdsPlugin; |