| 
									
										
										
										
											2016-05-05 16:13:50 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2017-01-05 06:21:38 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class EnsureChunkConditionsPlugin { | 
					
						
							| 
									
										
										
										
											2016-05-05 16:13:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-05 06:21:38 +08:00
										 |  |  | 	apply(compiler) { | 
					
						
							|  |  |  | 		compiler.plugin("compilation", (compilation) => { | 
					
						
							| 
									
										
										
										
											2017-06-01 22:10:27 +08:00
										 |  |  | 			const triesMap = new Map(); | 
					
						
							| 
									
										
										
										
											2017-01-05 06:21:38 +08:00
										 |  |  | 			compilation.plugin(["optimize-chunks-basic", "optimize-extracted-chunks-basic"], (chunks) => { | 
					
						
							|  |  |  | 				let changed = false; | 
					
						
							|  |  |  | 				chunks.forEach((chunk) => { | 
					
						
							| 
									
										
										
										
											2017-04-21 16:05:56 +08:00
										 |  |  | 					chunk.forEachModule((module) => { | 
					
						
							| 
									
										
										
										
											2017-01-05 06:21:38 +08:00
										 |  |  | 						if(!module.chunkCondition) return; | 
					
						
							|  |  |  | 						if(!module.chunkCondition(chunk)) { | 
					
						
							| 
									
										
										
										
											2017-06-01 22:10:27 +08:00
										 |  |  | 							let usedChunks = triesMap.get(module); | 
					
						
							|  |  |  | 							if(!usedChunks) triesMap.set(module, usedChunks = new Set()); | 
					
						
							|  |  |  | 							usedChunks.add(chunk); | 
					
						
							| 
									
										
										
										
											2017-02-05 07:40:10 +08:00
										 |  |  | 							const newChunks = []; | 
					
						
							| 
									
										
										
										
											2017-01-05 06:21:38 +08:00
										 |  |  | 							chunk.parents.forEach((parent) => { | 
					
						
							| 
									
										
										
										
											2017-06-01 22:10:27 +08:00
										 |  |  | 								if(!usedChunks.has(parent)) { | 
					
						
							| 
									
										
										
										
											2017-01-05 06:21:38 +08:00
										 |  |  | 									parent.addModule(module); | 
					
						
							|  |  |  | 									newChunks.push(parent); | 
					
						
							|  |  |  | 								} | 
					
						
							|  |  |  | 							}); | 
					
						
							|  |  |  | 							module.rewriteChunkInReasons(chunk, newChunks); | 
					
						
							|  |  |  | 							chunk.removeModule(module); | 
					
						
							|  |  |  | 							changed = true; | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2016-05-05 16:13:50 +08:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2017-01-05 06:21:38 +08:00
										 |  |  | 				if(changed) return true; | 
					
						
							| 
									
										
										
										
											2016-05-05 16:13:50 +08:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-01-05 06:21:38 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | module.exports = EnsureChunkConditionsPlugin; |