| 
									
										
										
										
											2017-10-30 20:56:57 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 19:05:58 +08:00
										 |  |  | const { RawSource } = require("webpack-sources"); | 
					
						
							| 
									
										
										
										
											2017-12-15 22:39:53 +08:00
										 |  |  | const WebAssemblyImportDependency = require("../dependencies/WebAssemblyImportDependency"); | 
					
						
							| 
									
										
										
										
											2017-10-30 20:56:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-14 08:21:44 +08:00
										 |  |  | class WasmModuleTemplatePlugin { | 
					
						
							| 
									
										
										
										
											2017-10-30 20:56:57 +08:00
										 |  |  | 	apply(moduleTemplate) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		moduleTemplate.hooks.content.tap( | 
					
						
							|  |  |  | 			"WasmModuleTemplatePlugin", | 
					
						
							|  |  |  | 			(moduleSource, module, { chunk }) => { | 
					
						
							|  |  |  | 				if (module.type && module.type.startsWith("webassembly")) { | 
					
						
							|  |  |  | 					if (chunk.canBeInitial()) | 
					
						
							|  |  |  | 						throw new Error( | 
					
						
							|  |  |  | 							"Sync WebAssembly compilation is not yet implemented" | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 					const generateExports = () => { | 
					
						
							|  |  |  | 						if ( | 
					
						
							|  |  |  | 							Array.isArray(module.buildMeta.providedExports) && | 
					
						
							|  |  |  | 							Array.isArray(module.usedExports) | 
					
						
							|  |  |  | 						) { | 
					
						
							|  |  |  | 							// generate mangled exports
 | 
					
						
							|  |  |  | 							return module.buildMeta.providedExports | 
					
						
							|  |  |  | 								.map(exp => { | 
					
						
							|  |  |  | 									const usedName = module.isUsed(exp); | 
					
						
							|  |  |  | 									if (usedName) { | 
					
						
							|  |  |  | 										return `${module.exportsArgument}[${JSON.stringify( | 
					
						
							|  |  |  | 											usedName | 
					
						
							|  |  |  | 										)}] = instance.exports[${JSON.stringify(exp)}];`;
 | 
					
						
							|  |  |  | 									} else { | 
					
						
							|  |  |  | 										return `// unused ${JSON.stringify(exp)} export`; | 
					
						
							|  |  |  | 									} | 
					
						
							|  |  |  | 								}) | 
					
						
							|  |  |  | 								.join("\n"); | 
					
						
							|  |  |  | 						} else { | 
					
						
							|  |  |  | 							// generate simple export
 | 
					
						
							|  |  |  | 							return `${module.moduleArgument}.exports = instance.exports;`; | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					}; | 
					
						
							|  |  |  | 					const generateImports = () => { | 
					
						
							|  |  |  | 						const depsByRequest = new Map(); | 
					
						
							|  |  |  | 						for (const dep of module.dependencies) { | 
					
						
							|  |  |  | 							if (dep instanceof WebAssemblyImportDependency) { | 
					
						
							|  |  |  | 								const request = dep.request; | 
					
						
							|  |  |  | 								let array = depsByRequest.get(request); | 
					
						
							|  |  |  | 								if (!array) { | 
					
						
							|  |  |  | 									depsByRequest.set(request, (array = [])); | 
					
						
							|  |  |  | 								} | 
					
						
							|  |  |  | 								const exportName = dep.name; | 
					
						
							|  |  |  | 								const usedName = dep.module && dep.module.isUsed(exportName); | 
					
						
							|  |  |  | 								array.push({ | 
					
						
							|  |  |  | 									exportName, | 
					
						
							|  |  |  | 									usedName, | 
					
						
							|  |  |  | 									module: dep.module | 
					
						
							|  |  |  | 								}); | 
					
						
							| 
									
										
										
										
											2017-12-15 22:39:53 +08:00
										 |  |  | 							} | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						const importsCode = []; | 
					
						
							|  |  |  | 						for (const pair of depsByRequest) { | 
					
						
							|  |  |  | 							const properties = []; | 
					
						
							|  |  |  | 							for (const data of pair[1]) { | 
					
						
							|  |  |  | 								properties.push( | 
					
						
							|  |  |  | 									`\n\t\t${JSON.stringify( | 
					
						
							|  |  |  | 										data.exportName | 
					
						
							|  |  |  | 									)}: __webpack_require__(${JSON.stringify( | 
					
						
							|  |  |  | 										data.module.id | 
					
						
							|  |  |  | 									)})[${JSON.stringify(data.usedName)}]`
 | 
					
						
							|  |  |  | 								); | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 							importsCode.push( | 
					
						
							|  |  |  | 								`\n\t${JSON.stringify(pair[0])}: {${properties.join(",")}\n\t}` | 
					
						
							|  |  |  | 							); | 
					
						
							| 
									
										
										
										
											2017-12-15 22:39:53 +08:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						return importsCode.join(","); | 
					
						
							|  |  |  | 					}; | 
					
						
							|  |  |  | 					const source = new RawSource( | 
					
						
							|  |  |  | 						[ | 
					
						
							|  |  |  | 							'"use strict";', | 
					
						
							|  |  |  | 							"", | 
					
						
							|  |  |  | 							"// Instantiate WebAssembly module", | 
					
						
							|  |  |  | 							"var instance = new WebAssembly.Instance(__webpack_require__.w[module.i], {" + | 
					
						
							|  |  |  | 								generateImports(), | 
					
						
							|  |  |  | 							"});", | 
					
						
							|  |  |  | 							"", | 
					
						
							|  |  |  | 							"// export exports from WebAssembly module", | 
					
						
							|  |  |  | 							// TODO rewrite this to getters depending on exports to support circular dependencies
 | 
					
						
							|  |  |  | 							generateExports() | 
					
						
							|  |  |  | 						].join("\n") | 
					
						
							|  |  |  | 					); | 
					
						
							|  |  |  | 					return source; | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					return moduleSource; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2017-10-30 20:56:57 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		); | 
					
						
							| 
									
										
										
										
											2017-10-30 20:56:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-14 04:35:39 +08:00
										 |  |  | 		moduleTemplate.hooks.hash.tap("WasmModuleTemplatePlugin", hash => { | 
					
						
							| 
									
										
										
										
											2017-12-14 08:21:44 +08:00
										 |  |  | 			hash.update("WasmModuleTemplatePlugin"); | 
					
						
							| 
									
										
										
										
											2017-10-30 20:56:57 +08:00
										 |  |  | 			hash.update("1"); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-12-14 08:21:44 +08:00
										 |  |  | module.exports = WasmModuleTemplatePlugin; |