| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 14:54:21 +08:00
										 |  |  | const { SyncWaterfallHook } = require("tapable"); | 
					
						
							|  |  |  | const Compilation = require("../Compilation"); | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | const Generator = require("../Generator"); | 
					
						
							| 
									
										
										
										
											2019-10-02 14:54:21 +08:00
										 |  |  | const { tryRunOrWebpackError } = require("../HookWebpackError"); | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDependency"); | 
					
						
							| 
									
										
										
										
											2019-10-16 22:38:04 +08:00
										 |  |  | const { compareModulesByIdentifier } = require("../util/comparators"); | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 14:54:21 +08:00
										 |  |  | /** @typedef {import("webpack-sources").Source} Source */ | 
					
						
							| 
									
										
										
										
											2019-10-09 04:29:46 +08:00
										 |  |  | /** @typedef {import("../Compilation")} Compilation */ | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | /** @typedef {import("../Compiler")} Compiler */ | 
					
						
							| 
									
										
										
										
											2019-10-02 14:54:21 +08:00
										 |  |  | /** @typedef {import("../Module")} Module */ | 
					
						
							| 
									
										
										
										
											2019-10-09 04:29:46 +08:00
										 |  |  | /** @typedef {import("../Module").CodeGenerationResult} CodeGenerationResult */ | 
					
						
							|  |  |  | /** @typedef {import("../Template").RenderManifestEntry} RenderManifestEntry */ | 
					
						
							|  |  |  | /** @typedef {import("../Template").RenderManifestOptions} RenderManifestOptions */ | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-18 19:47:10 +08:00
										 |  |  | let AsyncWebAssemblyGenerator; | 
					
						
							|  |  |  | let AsyncWebAssemblyJavascriptGenerator; | 
					
						
							|  |  |  | let AsyncWebAssemblyParser; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 14:54:21 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @typedef {Object} RenderContext | 
					
						
							|  |  |  |  * @property {Chunk} chunk the chunk | 
					
						
							|  |  |  |  * @property {DependencyTemplates} dependencyTemplates the dependency templates | 
					
						
							|  |  |  |  * @property {RuntimeTemplate} runtimeTemplate the runtime template | 
					
						
							|  |  |  |  * @property {ModuleGraph} moduleGraph the module graph | 
					
						
							|  |  |  |  * @property {ChunkGraph} chunkGraph the chunk graph | 
					
						
							| 
									
										
										
										
											2019-10-09 04:29:46 +08:00
										 |  |  |  * @property {Map<Module, CodeGenerationResult>} codeGenerationResults results of code generation | 
					
						
							| 
									
										
										
										
											2019-10-02 14:54:21 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @typedef {Object} CompilationHooks | 
					
						
							|  |  |  |  * @property {SyncWaterfallHook<[Source, Module, RenderContext]>} renderModuleContent | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** @type {WeakMap<Compilation, CompilationHooks>} */ | 
					
						
							|  |  |  | const compilationHooksMap = new WeakMap(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | class AsyncWebAssemblyModulesPlugin { | 
					
						
							| 
									
										
										
										
											2019-10-02 14:54:21 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {Compilation} compilation the compilation | 
					
						
							|  |  |  | 	 * @returns {CompilationHooks} the attached hooks | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	static getCompilationHooks(compilation) { | 
					
						
							|  |  |  | 		if (!(compilation instanceof Compilation)) { | 
					
						
							|  |  |  | 			throw new TypeError( | 
					
						
							| 
									
										
										
										
											2019-10-09 21:52:38 +08:00
										 |  |  | 				"The 'compilation' argument must be an instance of Compilation" | 
					
						
							| 
									
										
										
										
											2019-10-02 14:54:21 +08:00
										 |  |  | 			); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		let hooks = compilationHooksMap.get(compilation); | 
					
						
							|  |  |  | 		if (hooks === undefined) { | 
					
						
							|  |  |  | 			hooks = { | 
					
						
							|  |  |  | 				renderModuleContent: new SyncWaterfallHook([ | 
					
						
							|  |  |  | 					"source", | 
					
						
							|  |  |  | 					"module", | 
					
						
							|  |  |  | 					"renderContext" | 
					
						
							|  |  |  | 				]) | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 			compilationHooksMap.set(compilation, hooks); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return hooks; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | 	constructor(options) { | 
					
						
							|  |  |  | 		this.options = options; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {Compiler} compiler compiler | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							|  |  |  | 		compiler.hooks.compilation.tap( | 
					
						
							|  |  |  | 			"AsyncWebAssemblyModulesPlugin", | 
					
						
							|  |  |  | 			(compilation, { normalModuleFactory }) => { | 
					
						
							| 
									
										
										
										
											2019-10-02 14:54:21 +08:00
										 |  |  | 				const hooks = AsyncWebAssemblyModulesPlugin.getCompilationHooks( | 
					
						
							|  |  |  | 					compilation | 
					
						
							|  |  |  | 				); | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | 				compilation.dependencyFactories.set( | 
					
						
							|  |  |  | 					WebAssemblyImportDependency, | 
					
						
							|  |  |  | 					normalModuleFactory | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				normalModuleFactory.hooks.createParser | 
					
						
							| 
									
										
										
										
											2019-07-15 21:03:29 +08:00
										 |  |  | 					.for("webassembly/async") | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | 					.tap("AsyncWebAssemblyModulesPlugin", () => { | 
					
						
							| 
									
										
										
										
											2019-10-18 19:47:10 +08:00
										 |  |  | 						if (AsyncWebAssemblyParser === undefined) { | 
					
						
							|  |  |  | 							AsyncWebAssemblyParser = require("./AsyncWebAssemblyParser"); | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | 						return new AsyncWebAssemblyParser(); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				normalModuleFactory.hooks.createGenerator | 
					
						
							| 
									
										
										
										
											2019-07-15 21:03:29 +08:00
										 |  |  | 					.for("webassembly/async") | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | 					.tap("AsyncWebAssemblyModulesPlugin", () => { | 
					
						
							| 
									
										
										
										
											2019-10-18 19:47:10 +08:00
										 |  |  | 						if (AsyncWebAssemblyGenerator === undefined) { | 
					
						
							|  |  |  | 							AsyncWebAssemblyGenerator = require("./AsyncWebAssemblyGenerator"); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						if (AsyncWebAssemblyJavascriptGenerator === undefined) { | 
					
						
							|  |  |  | 							AsyncWebAssemblyJavascriptGenerator = require("./AsyncWebAssemblyJavascriptGenerator"); | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | 						return Generator.byType({ | 
					
						
							|  |  |  | 							javascript: new AsyncWebAssemblyJavascriptGenerator( | 
					
						
							|  |  |  | 								compilation.outputOptions.webassemblyModuleFilename | 
					
						
							|  |  |  | 							), | 
					
						
							|  |  |  | 							webassembly: new AsyncWebAssemblyGenerator(this.options) | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 14:54:21 +08:00
										 |  |  | 				compilation.hooks.renderManifest.tap( | 
					
						
							|  |  |  | 					"WebAssemblyModulesPlugin", | 
					
						
							|  |  |  | 					(result, options) => { | 
					
						
							|  |  |  | 						const { moduleGraph, chunkGraph, runtimeTemplate } = compilation; | 
					
						
							| 
									
										
										
										
											2019-10-09 04:29:46 +08:00
										 |  |  | 						const { | 
					
						
							|  |  |  | 							chunk, | 
					
						
							|  |  |  | 							outputOptions, | 
					
						
							|  |  |  | 							dependencyTemplates, | 
					
						
							|  |  |  | 							codeGenerationResults | 
					
						
							|  |  |  | 						} = options; | 
					
						
							| 
									
										
										
										
											2019-10-02 14:54:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 						for (const module of chunkGraph.getOrderedChunkModulesIterable( | 
					
						
							|  |  |  | 							chunk, | 
					
						
							| 
									
										
										
										
											2019-10-16 22:38:04 +08:00
										 |  |  | 							compareModulesByIdentifier | 
					
						
							| 
									
										
										
										
											2019-10-02 14:54:21 +08:00
										 |  |  | 						)) { | 
					
						
							|  |  |  | 							if (module.type === "webassembly/async") { | 
					
						
							|  |  |  | 								const filenameTemplate = | 
					
						
							|  |  |  | 									outputOptions.webassemblyModuleFilename; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 								result.push({ | 
					
						
							|  |  |  | 									render: () => | 
					
						
							|  |  |  | 										this.renderModule( | 
					
						
							|  |  |  | 											module, | 
					
						
							|  |  |  | 											{ | 
					
						
							|  |  |  | 												chunk, | 
					
						
							|  |  |  | 												dependencyTemplates, | 
					
						
							|  |  |  | 												runtimeTemplate, | 
					
						
							|  |  |  | 												moduleGraph, | 
					
						
							| 
									
										
										
										
											2019-10-09 04:29:46 +08:00
										 |  |  | 												chunkGraph, | 
					
						
							|  |  |  | 												codeGenerationResults | 
					
						
							| 
									
										
										
										
											2019-10-02 14:54:21 +08:00
										 |  |  | 											}, | 
					
						
							|  |  |  | 											hooks | 
					
						
							|  |  |  | 										), | 
					
						
							|  |  |  | 									filenameTemplate, | 
					
						
							|  |  |  | 									pathOptions: { | 
					
						
							|  |  |  | 										module, | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | 										chunkGraph | 
					
						
							| 
									
										
										
										
											2019-10-02 14:54:21 +08:00
										 |  |  | 									}, | 
					
						
							|  |  |  | 									auxiliary: true, | 
					
						
							|  |  |  | 									identifier: `webassemblyAsyncModule${chunkGraph.getModuleId( | 
					
						
							|  |  |  | 										module | 
					
						
							|  |  |  | 									)}`,
 | 
					
						
							|  |  |  | 									hash: chunkGraph.getModuleHash(module) | 
					
						
							|  |  |  | 								}); | 
					
						
							|  |  |  | 							} | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | 						} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 14:54:21 +08:00
										 |  |  | 						return result; | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | 				); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-10-02 14:54:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	renderModule(module, renderContext, hooks) { | 
					
						
							| 
									
										
										
										
											2019-10-09 04:29:46 +08:00
										 |  |  | 		const { codeGenerationResults } = renderContext; | 
					
						
							| 
									
										
										
										
											2019-10-02 14:54:21 +08:00
										 |  |  | 		try { | 
					
						
							| 
									
										
										
										
											2019-10-09 04:29:46 +08:00
										 |  |  | 			const moduleSource = codeGenerationResults | 
					
						
							|  |  |  | 				.get(module) | 
					
						
							|  |  |  | 				.sources.get("webassembly"); | 
					
						
							| 
									
										
										
										
											2019-10-02 14:54:21 +08:00
										 |  |  | 			return tryRunOrWebpackError( | 
					
						
							|  |  |  | 				() => | 
					
						
							|  |  |  | 					hooks.renderModuleContent.call(moduleSource, module, renderContext), | 
					
						
							|  |  |  | 				"AsyncWebAssemblyModulesPlugin.getCompilationHooks().renderModuleContent" | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 		} catch (e) { | 
					
						
							|  |  |  | 			e.module = module; | 
					
						
							|  |  |  | 			throw e; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = AsyncWebAssemblyModulesPlugin; |