| 
									
										
										
										
											2018-12-07 19:26:35 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Florent Cailhol @ooflorent | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const { compareChunksNatural } = require("../util/comparators"); | 
					
						
							|  |  |  | const { | 
					
						
							|  |  |  | 	getFullChunkName, | 
					
						
							|  |  |  | 	getUsedChunkIds, | 
					
						
							|  |  |  | 	assignDeterministicIds | 
					
						
							|  |  |  | } = require("./IdHelpers"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** @typedef {import("../Compiler")} Compiler */ | 
					
						
							|  |  |  | /** @typedef {import("../Module")} Module */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class DeterministicChunkIdsPlugin { | 
					
						
							|  |  |  | 	constructor(options) { | 
					
						
							|  |  |  | 		this.options = options || {}; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2020-04-23 16:48:36 +08:00
										 |  |  | 	 * Apply the plugin | 
					
						
							| 
									
										
										
										
											2018-12-07 19:26:35 +08:00
										 |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							|  |  |  | 		compiler.hooks.compilation.tap( | 
					
						
							|  |  |  | 			"DeterministicChunkIdsPlugin", | 
					
						
							|  |  |  | 			compilation => { | 
					
						
							|  |  |  | 				compilation.hooks.chunkIds.tap( | 
					
						
							|  |  |  | 					"DeterministicChunkIdsPlugin", | 
					
						
							|  |  |  | 					chunks => { | 
					
						
							|  |  |  | 						const chunkGraph = compilation.chunkGraph; | 
					
						
							| 
									
										
										
										
											2018-12-07 23:07:40 +08:00
										 |  |  | 						const context = this.options.context | 
					
						
							|  |  |  | 							? this.options.context | 
					
						
							|  |  |  | 							: compiler.context; | 
					
						
							| 
									
										
										
										
											2019-02-20 18:32:12 +08:00
										 |  |  | 						const maxLength = this.options.maxLength || 3; | 
					
						
							| 
									
										
										
										
											2018-12-07 19:26:35 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 						const compareNatural = compareChunksNatural(chunkGraph); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-20 18:32:12 +08:00
										 |  |  | 						const usedIds = getUsedChunkIds(compilation); | 
					
						
							| 
									
										
										
										
											2018-12-07 19:26:35 +08:00
										 |  |  | 						assignDeterministicIds( | 
					
						
							|  |  |  | 							Array.from(chunks).filter(chunk => { | 
					
						
							|  |  |  | 								return chunk.id === null; | 
					
						
							|  |  |  | 							}), | 
					
						
							| 
									
										
										
										
											2018-12-08 01:12:04 +08:00
										 |  |  | 							chunk => | 
					
						
							|  |  |  | 								getFullChunkName(chunk, chunkGraph, context, compiler.root), | 
					
						
							| 
									
										
										
										
											2018-12-07 19:26:35 +08:00
										 |  |  | 							compareNatural, | 
					
						
							|  |  |  | 							(chunk, id) => { | 
					
						
							| 
									
										
										
										
											2019-02-20 18:32:12 +08:00
										 |  |  | 								const size = usedIds.size; | 
					
						
							|  |  |  | 								usedIds.add(`${id}`); | 
					
						
							|  |  |  | 								if (size === usedIds.size) return false; | 
					
						
							| 
									
										
										
										
											2018-12-07 19:26:35 +08:00
										 |  |  | 								chunk.id = id; | 
					
						
							|  |  |  | 								chunk.ids = [id]; | 
					
						
							| 
									
										
										
										
											2019-02-20 18:32:12 +08:00
										 |  |  | 								return true; | 
					
						
							|  |  |  | 							}, | 
					
						
							|  |  |  | 							[Math.pow(10, maxLength)], | 
					
						
							|  |  |  | 							10, | 
					
						
							|  |  |  | 							usedIds.size | 
					
						
							| 
									
										
										
										
											2018-12-07 19:26:35 +08:00
										 |  |  | 						); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = DeterministicChunkIdsPlugin; |