| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-12 18:43:20 +08:00
										 |  |  | function generateInitParams(module) { | 
					
						
							|  |  |  | 	const list = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (const dep of module.dependencies) { | 
					
						
							|  |  |  | 		if (dep instanceof WebAssemblyImportDependency) { | 
					
						
							|  |  |  | 			if (dep.description.type === "GlobalType") { | 
					
						
							|  |  |  | 				const exportName = dep.name; | 
					
						
							|  |  |  | 				const usedName = dep.module && dep.module.isUsed(exportName); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-29 19:52:26 +08:00
										 |  |  | 				if (usedName !== false) { | 
					
						
							|  |  |  | 					list.push( | 
					
						
							|  |  |  | 						`__webpack_require__(${JSON.stringify( | 
					
						
							|  |  |  | 							dep.module.id | 
					
						
							|  |  |  | 						)})[${JSON.stringify(usedName)}]`
 | 
					
						
							|  |  |  | 					); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-03-12 18:43:20 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return list; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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;`; | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					}; | 
					
						
							| 
									
										
										
										
											2018-03-10 02:03:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-12 18:43:20 +08:00
										 |  |  | 					// FIXME(sven): assert that the exports exists in the modules
 | 
					
						
							|  |  |  | 					// otherwise it will default to i32 0
 | 
					
						
							| 
									
										
										
										
											2018-03-10 02:03:33 +08:00
										 |  |  | 					const initParams = generateInitParams(module).join(","); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					const source = new RawSource( | 
					
						
							|  |  |  | 						[ | 
					
						
							|  |  |  | 							'"use strict";', | 
					
						
							|  |  |  | 							"// Instantiate WebAssembly module", | 
					
						
							| 
									
										
										
										
											2018-03-09 00:54:06 +08:00
										 |  |  | 							"var instance = __webpack_require__.w[module.i]", | 
					
						
							| 
									
										
										
										
											2018-03-29 20:53:59 +08:00
										 |  |  | 							`instance.exports.__webpack_init__(${initParams})`, | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 							"// 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; |