| 
									
										
										
										
											2018-05-30 18:45:05 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-30 18:45:05 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-07 20:11:48 +08:00
										 |  |  | const formatLocation = require("../formatLocation"); | 
					
						
							| 
									
										
										
										
											2018-07-09 20:48:28 +08:00
										 |  |  | const UnsupportedWebAssemblyFeatureError = require("./UnsupportedWebAssemblyFeatureError"); | 
					
						
							| 
									
										
										
										
											2018-05-30 18:45:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-07 20:11:48 +08:00
										 |  |  | /** @typedef {import("../Compiler")} Compiler */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-30 18:45:05 +08:00
										 |  |  | class WasmFinalizeExportsPlugin { | 
					
						
							| 
									
										
										
										
											2018-09-07 20:11:48 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {Compiler} compiler webpack compiler | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2018-05-30 18:45:05 +08:00
										 |  |  | 	apply(compiler) { | 
					
						
							|  |  |  | 		compiler.hooks.compilation.tap("WasmFinalizeExportsPlugin", compilation => { | 
					
						
							|  |  |  | 			compilation.hooks.finishModules.tap( | 
					
						
							|  |  |  | 				"WasmFinalizeExportsPlugin", | 
					
						
							|  |  |  | 				modules => { | 
					
						
							|  |  |  | 					for (const module of modules) { | 
					
						
							| 
									
										
										
										
											2018-05-30 21:27:42 +08:00
										 |  |  | 						// 1. if a WebAssembly module
 | 
					
						
							|  |  |  | 						if (module.type.startsWith("webassembly") === true) { | 
					
						
							| 
									
										
										
										
											2018-06-04 22:23:41 +08:00
										 |  |  | 							const jsIncompatibleExports = | 
					
						
							|  |  |  | 								module.buildMeta.jsIncompatibleExports; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 							if (jsIncompatibleExports === undefined) { | 
					
						
							|  |  |  | 								continue; | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-24 21:30:37 +08:00
										 |  |  | 							for (const connection of compilation.moduleGraph.getIncomingConnections( | 
					
						
							|  |  |  | 								module | 
					
						
							|  |  |  | 							)) { | 
					
						
							| 
									
										
										
										
											2019-10-30 04:37:59 +08:00
										 |  |  | 								// 2. is active and referenced by a non-WebAssembly module
 | 
					
						
							| 
									
										
										
										
											2018-07-24 21:30:37 +08:00
										 |  |  | 								if ( | 
					
						
							| 
									
										
										
										
											2019-10-30 04:37:59 +08:00
										 |  |  | 									connection.active && | 
					
						
							| 
									
										
										
										
											2018-07-24 21:30:37 +08:00
										 |  |  | 									connection.originModule.type.startsWith("webassembly") === | 
					
						
							| 
									
										
										
										
											2019-10-30 04:37:59 +08:00
										 |  |  | 										false | 
					
						
							| 
									
										
										
										
											2018-07-24 21:30:37 +08:00
										 |  |  | 								) { | 
					
						
							| 
									
										
										
										
											2019-10-30 05:28:42 +08:00
										 |  |  | 									const referencedExports = compilation.getDependencyReferencedExports( | 
					
						
							| 
									
										
										
										
											2018-07-24 21:30:37 +08:00
										 |  |  | 										connection.dependency | 
					
						
							| 
									
										
										
										
											2018-06-08 16:34:38 +08:00
										 |  |  | 									); | 
					
						
							| 
									
										
										
										
											2018-05-30 21:27:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-30 05:28:42 +08:00
										 |  |  | 									for (const names of referencedExports) { | 
					
						
							|  |  |  | 										if (names.length === 0) continue; | 
					
						
							|  |  |  | 										const name = names[0]; | 
					
						
							|  |  |  | 										// 3. and uses a func with an incompatible JS signature
 | 
					
						
							|  |  |  | 										if ( | 
					
						
							|  |  |  | 											Object.prototype.hasOwnProperty.call( | 
					
						
							|  |  |  | 												jsIncompatibleExports, | 
					
						
							|  |  |  | 												name | 
					
						
							|  |  |  | 											) | 
					
						
							|  |  |  | 										) { | 
					
						
							|  |  |  | 											// 4. error
 | 
					
						
							|  |  |  | 											/** @type {TODO} */ | 
					
						
							|  |  |  | 											const error = new UnsupportedWebAssemblyFeatureError( | 
					
						
							|  |  |  | 												`Export "${name}" with ${jsIncompatibleExports[name]} can only be used for direct wasm to wasm dependencies\n` + | 
					
						
							|  |  |  | 													`It's used from ${connection.originModule.readableIdentifier( | 
					
						
							|  |  |  | 														compilation.requestShortener | 
					
						
							|  |  |  | 													)} at ${formatLocation(connection.dependency.loc)}.`
 | 
					
						
							|  |  |  | 											); | 
					
						
							|  |  |  | 											error.module = module; | 
					
						
							|  |  |  | 											compilation.errors.push(error); | 
					
						
							|  |  |  | 										} | 
					
						
							| 
									
										
										
										
											2018-06-04 22:23:41 +08:00
										 |  |  | 									} | 
					
						
							| 
									
										
										
										
											2018-05-30 18:45:05 +08:00
										 |  |  | 								} | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = WasmFinalizeExportsPlugin; |