| 
									
										
										
										
											2019-03-31 22:12:19 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Yuta Hiroto @hiroppy | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const Generator = require("../Generator"); | 
					
						
							| 
									
										
										
										
											2019-10-16 22:38:04 +08:00
										 |  |  | const { compareModulesByIdentifier } = require("../util/comparators"); | 
					
						
							| 
									
										
										
										
											2019-07-16 19:16:27 +08:00
										 |  |  | const AssetGenerator = require("./AssetGenerator"); | 
					
						
							|  |  |  | const AssetJavascriptGenerator = require("./AssetJavascriptGenerator"); | 
					
						
							|  |  |  | const AssetParser = require("./AssetParser"); | 
					
						
							| 
									
										
										
										
											2019-03-31 22:12:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** @typedef {import("webpack-sources").Source} Source */ | 
					
						
							|  |  |  | /** @typedef {import("../Chunk")} Chunk */ | 
					
						
							|  |  |  | /** @typedef {import("../Compiler")} Compiler */ | 
					
						
							|  |  |  | /** @typedef {import("../Module")} Module */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 19:16:27 +08:00
										 |  |  | const type = "asset"; | 
					
						
							|  |  |  | const plugin = "AssetModulesPlugin"; | 
					
						
							| 
									
										
										
										
											2019-03-31 22:12:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 19:33:45 +08:00
										 |  |  | class AssetModulesPlugin { | 
					
						
							| 
									
										
										
										
											2019-03-31 22:12:19 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {Compiler} compiler webpack compiler | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							|  |  |  | 		compiler.hooks.compilation.tap( | 
					
						
							|  |  |  | 			plugin, | 
					
						
							|  |  |  | 			(compilation, { normalModuleFactory }) => { | 
					
						
							|  |  |  | 				normalModuleFactory.hooks.createParser.for(type).tap(plugin, () => { | 
					
						
							| 
									
										
										
										
											2019-07-16 19:16:27 +08:00
										 |  |  | 					return new AssetParser(); | 
					
						
							| 
									
										
										
										
											2019-03-31 22:12:19 +08:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				normalModuleFactory.hooks.createGenerator.for(type).tap(plugin, () => { | 
					
						
							|  |  |  | 					return Generator.byType({ | 
					
						
							| 
									
										
										
										
											2019-07-16 19:16:27 +08:00
										 |  |  | 						asset: new AssetGenerator(), | 
					
						
							|  |  |  | 						javascript: new AssetJavascriptGenerator(compilation) | 
					
						
							| 
									
										
										
										
											2019-03-31 22:12:19 +08:00
										 |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-04 18:24:52 +08:00
										 |  |  | 				compilation.hooks.renderManifest.tap(plugin, (result, options) => { | 
					
						
							| 
									
										
										
										
											2019-10-09 04:29:46 +08:00
										 |  |  | 					const { chunkGraph } = compilation; | 
					
						
							|  |  |  | 					const { chunk, runtimeTemplate, codeGenerationResults } = options; | 
					
						
							| 
									
										
										
										
											2019-03-31 22:12:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-04 18:24:52 +08:00
										 |  |  | 					const { outputOptions } = runtimeTemplate; | 
					
						
							| 
									
										
										
										
											2019-03-31 22:12:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-04 18:24:52 +08:00
										 |  |  | 					for (const module of chunkGraph.getOrderedChunkModulesIterable( | 
					
						
							|  |  |  | 						chunk, | 
					
						
							| 
									
										
										
										
											2019-10-16 22:38:04 +08:00
										 |  |  | 						compareModulesByIdentifier | 
					
						
							| 
									
										
										
										
											2019-10-04 18:24:52 +08:00
										 |  |  | 					)) { | 
					
						
							|  |  |  | 						if (module.getSourceTypes().has("asset")) { | 
					
						
							|  |  |  | 							const filename = module.nameForCondition(); | 
					
						
							|  |  |  | 							const filenameTemplate = outputOptions.assetModuleFilename; | 
					
						
							| 
									
										
										
										
											2019-03-31 22:12:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-04 18:24:52 +08:00
										 |  |  | 							result.push({ | 
					
						
							|  |  |  | 								render: () => | 
					
						
							| 
									
										
										
										
											2019-10-09 04:29:46 +08:00
										 |  |  | 									codeGenerationResults.get(module).sources.get(type), | 
					
						
							| 
									
										
										
										
											2019-10-04 18:24:52 +08:00
										 |  |  | 								filenameTemplate, | 
					
						
							|  |  |  | 								pathOptions: { | 
					
						
							|  |  |  | 									module, | 
					
						
							|  |  |  | 									filename, | 
					
						
							|  |  |  | 									chunkGraph | 
					
						
							|  |  |  | 								}, | 
					
						
							|  |  |  | 								auxiliary: true, | 
					
						
							|  |  |  | 								identifier: `assetModule${chunkGraph.getModuleId(module)}`, | 
					
						
							|  |  |  | 								hash: chunkGraph.getModuleHash(module) | 
					
						
							|  |  |  | 							}); | 
					
						
							| 
									
										
										
										
											2019-03-31 22:12:19 +08:00
										 |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2019-10-04 18:24:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 					return result; | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2019-03-31 22:12:19 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-16 19:16:27 +08:00
										 |  |  | module.exports = AssetModulesPlugin; |