| 
									
										
										
										
											2013-12-03 16:27:15 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2013-12-03 18:28:39 +08:00
										 |  |  | function CommonsChunkPlugin(filenameTemplate, entryPoints, minCount) { | 
					
						
							|  |  |  | 	if(typeof entryPoints === "number") { | 
					
						
							|  |  |  | 		minCount = entryPoints; | 
					
						
							|  |  |  | 		entryPoints = undefined; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-12-03 18:19:30 +08:00
										 |  |  | 	this.filenameTemplate = filenameTemplate; | 
					
						
							| 
									
										
										
										
											2013-12-03 18:44:46 +08:00
										 |  |  | 	this.minCount = minCount; | 
					
						
							| 
									
										
										
										
											2013-12-03 18:28:39 +08:00
										 |  |  | 	this.entryPoints = entryPoints; | 
					
						
							| 
									
										
										
										
											2013-12-03 16:27:15 +08:00
										 |  |  | } | 
					
						
							|  |  |  | module.exports = CommonsChunkPlugin; | 
					
						
							|  |  |  | CommonsChunkPlugin.prototype.apply = function(compiler) { | 
					
						
							| 
									
										
										
										
											2013-12-03 18:19:30 +08:00
										 |  |  | 	var filenameTemplate = this.filenameTemplate; | 
					
						
							| 
									
										
										
										
											2013-12-03 16:27:15 +08:00
										 |  |  | 	var minCount = this.minCount; | 
					
						
							| 
									
										
										
										
											2013-12-03 18:28:39 +08:00
										 |  |  | 	var entryPoints = this.entryPoints; | 
					
						
							| 
									
										
										
										
											2013-12-03 16:27:15 +08:00
										 |  |  | 	compiler.plugin("compilation", function(compilation) { | 
					
						
							|  |  |  | 		compilation.plugin("optimize-chunks", function(chunks) { | 
					
						
							|  |  |  | 			var commonModulesCount = []; | 
					
						
							|  |  |  | 			var commonModules = []; | 
					
						
							| 
									
										
										
										
											2014-01-31 20:12:51 +08:00
										 |  |  | 			var commonChunk = this.addChunk(filenameTemplate); | 
					
						
							| 
									
										
										
										
											2013-12-03 18:28:39 +08:00
										 |  |  | 			var usedChunks = chunks.filter(function(chunk) { | 
					
						
							| 
									
										
										
										
											2014-01-31 20:12:51 +08:00
										 |  |  | 				if(chunk === commonChunk) return false; | 
					
						
							| 
									
										
										
										
											2013-12-03 18:28:39 +08:00
										 |  |  | 				if(!chunk.entry) return false; | 
					
						
							|  |  |  | 				if(!entryPoints) return true; | 
					
						
							|  |  |  | 				return entryPoints.indexOf(chunk.name) >= 0; | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 			usedChunks.forEach(function(chunk) { | 
					
						
							|  |  |  | 				chunk.modules.forEach(function(module) { | 
					
						
							|  |  |  | 					var idx = commonModules.indexOf(module); | 
					
						
							|  |  |  | 					if(idx < 0) { | 
					
						
							|  |  |  | 						commonModules.push(module); | 
					
						
							|  |  |  | 						commonModulesCount.push(1); | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						commonModulesCount[idx]++; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2013-12-03 16:27:15 +08:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 			commonModulesCount.forEach(function(count, idx) { | 
					
						
							| 
									
										
										
										
											2013-12-03 18:44:46 +08:00
										 |  |  | 				if(count >= (minCount || usedChunks.length)) { | 
					
						
							| 
									
										
										
										
											2013-12-03 16:27:15 +08:00
										 |  |  | 					var module = commonModules[idx]; | 
					
						
							| 
									
										
										
										
											2013-12-03 18:34:38 +08:00
										 |  |  | 					usedChunks.forEach(function(chunk) { | 
					
						
							| 
									
										
										
										
											2013-12-03 16:27:15 +08:00
										 |  |  | 						module.removeChunk(chunk); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 					commonChunk.addModule(module); | 
					
						
							|  |  |  | 					module.addChunk(commonChunk); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-12-03 18:28:39 +08:00
										 |  |  | 			usedChunks.forEach(function(chunk) { | 
					
						
							|  |  |  | 				chunk.parents = [commonChunk]; | 
					
						
							|  |  |  | 				commonChunk.chunks.push(chunk); | 
					
						
							|  |  |  | 				chunk.entry = false; | 
					
						
							| 
									
										
										
										
											2013-12-03 16:27:15 +08:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-12-04 06:11:14 +08:00
										 |  |  | 			commonChunk.initial = commonChunk.entry = true; | 
					
						
							| 
									
										
										
										
											2013-12-03 18:19:30 +08:00
										 |  |  | 			commonChunk.filenameTemplate = filenameTemplate; | 
					
						
							| 
									
										
										
										
											2013-12-03 16:27:15 +08:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2013-12-04 00:14:28 +08:00
										 |  |  | }; |