| 
									
										
										
										
											2017-11-10 18:02:24 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-10 18:02:24 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-28 00:53:07 +08:00
										 |  |  | const Generator = require("../Generator"); | 
					
						
							| 
									
										
										
										
											2018-06-02 15:53:35 +08:00
										 |  |  | const WebAssemblyExportImportedDependency = require("../dependencies/WebAssemblyExportImportedDependency"); | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDependency"); | 
					
						
							| 
									
										
										
										
											2019-10-16 22:38:04 +08:00
										 |  |  | const { compareModulesByIdentifier } = require("../util/comparators"); | 
					
						
							| 
									
										
										
										
											2020-12-27 05:32:57 +08:00
										 |  |  | const memoize = require("../util/memoize"); | 
					
						
							| 
									
										
										
										
											2018-07-02 22:18:49 +08:00
										 |  |  | const WebAssemblyInInitialChunkError = require("./WebAssemblyInInitialChunkError"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-24 23:35:36 +08:00
										 |  |  | /** @typedef {import("webpack-sources").Source} Source */ | 
					
						
							| 
									
										
										
										
											2018-07-02 22:18:49 +08:00
										 |  |  | /** @typedef {import("../Compiler")} Compiler */ | 
					
						
							| 
									
										
										
										
											2018-07-24 23:35:36 +08:00
										 |  |  | /** @typedef {import("../Module")} Module */ | 
					
						
							|  |  |  | /** @typedef {import("../ModuleTemplate")} ModuleTemplate */ | 
					
						
							| 
									
										
										
										
											2021-06-24 22:44:14 +08:00
										 |  |  | /** @typedef {import("../javascript/JavascriptModulesPlugin").RenderContext} RenderContext */ | 
					
						
							| 
									
										
										
										
											2017-11-10 18:02:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-27 05:32:57 +08:00
										 |  |  | const getWebAssemblyGenerator = memoize(() => | 
					
						
							| 
									
										
										
										
											2019-12-04 01:31:39 +08:00
										 |  |  | 	require("./WebAssemblyGenerator") | 
					
						
							|  |  |  | ); | 
					
						
							| 
									
										
										
										
											2020-12-27 05:32:57 +08:00
										 |  |  | const getWebAssemblyJavascriptGenerator = memoize(() => | 
					
						
							| 
									
										
										
										
											2019-12-04 01:31:39 +08:00
										 |  |  | 	require("./WebAssemblyJavascriptGenerator") | 
					
						
							| 
									
										
										
										
											2019-12-02 23:37:55 +08:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2020-12-27 05:32:57 +08:00
										 |  |  | const getWebAssemblyParser = memoize(() => require("./WebAssemblyParser")); | 
					
						
							| 
									
										
										
										
											2019-10-18 19:47:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-10 18:02:24 +08:00
										 |  |  | class WebAssemblyModulesPlugin { | 
					
						
							| 
									
										
										
										
											2018-06-06 05:38:29 +08:00
										 |  |  | 	constructor(options) { | 
					
						
							|  |  |  | 		this.options = options; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-02 22:18:49 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2020-04-23 16:48:36 +08:00
										 |  |  | 	 * Apply the plugin | 
					
						
							|  |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							| 
									
										
										
										
											2018-07-02 22:18:49 +08:00
										 |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-11-10 18:02:24 +08:00
										 |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		compiler.hooks.compilation.tap( | 
					
						
							|  |  |  | 			"WebAssemblyModulesPlugin", | 
					
						
							|  |  |  | 			(compilation, { normalModuleFactory }) => { | 
					
						
							|  |  |  | 				compilation.dependencyFactories.set( | 
					
						
							|  |  |  | 					WebAssemblyImportDependency, | 
					
						
							|  |  |  | 					normalModuleFactory | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-02 15:53:35 +08:00
										 |  |  | 				compilation.dependencyFactories.set( | 
					
						
							|  |  |  | 					WebAssemblyExportImportedDependency, | 
					
						
							|  |  |  | 					normalModuleFactory | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				normalModuleFactory.hooks.createParser | 
					
						
							| 
									
										
										
										
											2019-07-15 21:03:29 +08:00
										 |  |  | 					.for("webassembly/sync") | 
					
						
							| 
									
										
										
										
											2019-12-04 01:31:39 +08:00
										 |  |  | 					.tap("WebAssemblyModulesPlugin", () => { | 
					
						
							|  |  |  | 						const WebAssemblyParser = getWebAssemblyParser(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						return new WebAssemblyParser(); | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				normalModuleFactory.hooks.createGenerator | 
					
						
							| 
									
										
										
										
											2019-07-15 21:03:29 +08:00
										 |  |  | 					.for("webassembly/sync") | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					.tap("WebAssemblyModulesPlugin", () => { | 
					
						
							| 
									
										
										
										
											2021-05-11 15:31:46 +08:00
										 |  |  | 						const WebAssemblyJavascriptGenerator = | 
					
						
							|  |  |  | 							getWebAssemblyJavascriptGenerator(); | 
					
						
							| 
									
										
										
										
											2019-12-04 01:31:39 +08:00
										 |  |  | 						const WebAssemblyGenerator = getWebAssemblyGenerator(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-28 00:53:07 +08:00
										 |  |  | 						return Generator.byType({ | 
					
						
							|  |  |  | 							javascript: new WebAssemblyJavascriptGenerator(), | 
					
						
							| 
									
										
										
										
											2018-06-06 05:38:29 +08:00
										 |  |  | 							webassembly: new WebAssemblyGenerator(this.options) | 
					
						
							| 
									
										
										
										
											2018-04-28 00:53:07 +08:00
										 |  |  | 						}); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-04 18:24:52 +08:00
										 |  |  | 				compilation.hooks.renderManifest.tap( | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					"WebAssemblyModulesPlugin", | 
					
						
							|  |  |  | 					(result, options) => { | 
					
						
							| 
									
										
										
										
											2019-10-09 04:29:46 +08:00
										 |  |  | 						const { chunkGraph } = compilation; | 
					
						
							|  |  |  | 						const { chunk, outputOptions, codeGenerationResults } = options; | 
					
						
							| 
									
										
										
										
											2018-01-24 16:39:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-14 17:18:22 +08:00
										 |  |  | 						for (const module of chunkGraph.getOrderedChunkModulesIterable( | 
					
						
							|  |  |  | 							chunk, | 
					
						
							| 
									
										
										
										
											2019-10-16 22:38:04 +08:00
										 |  |  | 							compareModulesByIdentifier | 
					
						
							| 
									
										
										
										
											2018-08-14 17:18:22 +08:00
										 |  |  | 						)) { | 
					
						
							| 
									
										
										
										
											2019-07-15 21:03:29 +08:00
										 |  |  | 							if (module.type === "webassembly/sync") { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 								const filenameTemplate = | 
					
						
							|  |  |  | 									outputOptions.webassemblyModuleFilename; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 								result.push({ | 
					
						
							|  |  |  | 									render: () => | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | 										codeGenerationResults.getSource( | 
					
						
							|  |  |  | 											module, | 
					
						
							|  |  |  | 											chunk.runtime, | 
					
						
							|  |  |  | 											"webassembly" | 
					
						
							|  |  |  | 										), | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 									filenameTemplate, | 
					
						
							|  |  |  | 									pathOptions: { | 
					
						
							| 
									
										
										
										
											2018-09-28 19:10:37 +08:00
										 |  |  | 										module, | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | 										runtime: chunk.runtime, | 
					
						
							| 
									
										
										
										
											2018-09-28 19:10:37 +08:00
										 |  |  | 										chunkGraph | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 									}, | 
					
						
							| 
									
										
										
										
											2019-07-17 23:30:25 +08:00
										 |  |  | 									auxiliary: true, | 
					
						
							| 
									
										
										
										
											2018-08-28 17:56:48 +08:00
										 |  |  | 									identifier: `webassemblyModule${chunkGraph.getModuleId( | 
					
						
							|  |  |  | 										module | 
					
						
							|  |  |  | 									)}`,
 | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | 									hash: chunkGraph.getModuleHash(module, chunk.runtime) | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 								}); | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						return result; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				); | 
					
						
							| 
									
										
										
										
											2018-07-02 22:18:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				compilation.hooks.afterChunks.tap("WebAssemblyModulesPlugin", () => { | 
					
						
							| 
									
										
										
										
											2018-08-14 17:18:22 +08:00
										 |  |  | 					const chunkGraph = compilation.chunkGraph; | 
					
						
							| 
									
										
										
										
											2018-07-02 22:18:49 +08:00
										 |  |  | 					const initialWasmModules = new Set(); | 
					
						
							|  |  |  | 					for (const chunk of compilation.chunks) { | 
					
						
							|  |  |  | 						if (chunk.canBeInitial()) { | 
					
						
							| 
									
										
										
										
											2018-08-14 17:18:22 +08:00
										 |  |  | 							for (const module of chunkGraph.getChunkModulesIterable(chunk)) { | 
					
						
							| 
									
										
										
										
											2019-07-15 21:03:29 +08:00
										 |  |  | 								if (module.type === "webassembly/sync") { | 
					
						
							| 
									
										
										
										
											2018-07-02 22:18:49 +08:00
										 |  |  | 									initialWasmModules.add(module); | 
					
						
							|  |  |  | 								} | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					for (const module of initialWasmModules) { | 
					
						
							|  |  |  | 						compilation.errors.push( | 
					
						
							|  |  |  | 							new WebAssemblyInInitialChunkError( | 
					
						
							|  |  |  | 								module, | 
					
						
							| 
									
										
										
										
											2018-12-30 18:50:04 +08:00
										 |  |  | 								compilation.moduleGraph, | 
					
						
							| 
									
										
										
										
											2018-08-14 17:18:22 +08:00
										 |  |  | 								compilation.chunkGraph, | 
					
						
							| 
									
										
										
										
											2018-07-02 22:18:49 +08:00
										 |  |  | 								compilation.requestShortener | 
					
						
							|  |  |  | 							) | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2017-11-10 18:02:24 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = WebAssemblyModulesPlugin; |