| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const RuntimeGlobals = require("../RuntimeGlobals"); | 
					
						
							|  |  |  | const Template = require("../Template"); | 
					
						
							|  |  |  | const AsyncWasmChunkLoadingRuntimeModule = require("../wasm-async/AsyncWasmChunkLoadingRuntimeModule"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** @typedef {import("../Compiler")} Compiler */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ReadFileCompileAsyncWasmPlugin { | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2020-04-23 16:48:36 +08:00
										 |  |  | 	 * Apply the plugin | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							|  |  |  | 		compiler.hooks.thisCompilation.tap( | 
					
						
							|  |  |  | 			"ReadFileCompileAsyncWasmPlugin", | 
					
						
							|  |  |  | 			compilation => { | 
					
						
							|  |  |  | 				const generateLoadBinaryCode = path => | 
					
						
							|  |  |  | 					Template.asString([ | 
					
						
							|  |  |  | 						"new Promise(function (resolve, reject) {", | 
					
						
							|  |  |  | 						Template.indent([ | 
					
						
							|  |  |  | 							"var { readFile } = require('fs');", | 
					
						
							|  |  |  | 							"var { join } = require('path');", | 
					
						
							|  |  |  | 							"", | 
					
						
							|  |  |  | 							"try {", | 
					
						
							|  |  |  | 							Template.indent([ | 
					
						
							|  |  |  | 								`readFile(join(__dirname, ${path}), function(err, buffer){`, | 
					
						
							|  |  |  | 								Template.indent([ | 
					
						
							|  |  |  | 									"if (err) return reject(err);", | 
					
						
							|  |  |  | 									"", | 
					
						
							|  |  |  | 									"// Fake fetch response", | 
					
						
							|  |  |  | 									"resolve({", | 
					
						
							| 
									
										
										
										
											2020-07-28 23:08:22 +08:00
										 |  |  | 									Template.indent(["arrayBuffer() { return buffer; }"]), | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | 									"});" | 
					
						
							|  |  |  | 								]), | 
					
						
							|  |  |  | 								"});" | 
					
						
							|  |  |  | 							]), | 
					
						
							|  |  |  | 							"} catch (err) { reject(err); }" | 
					
						
							|  |  |  | 						]), | 
					
						
							|  |  |  | 						"})" | 
					
						
							|  |  |  | 					]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				compilation.hooks.runtimeRequirementInTree | 
					
						
							|  |  |  | 					.for(RuntimeGlobals.instantiateWasm) | 
					
						
							|  |  |  | 					.tap("ReadFileCompileAsyncWasmPlugin", (chunk, set) => { | 
					
						
							|  |  |  | 						const chunkGraph = compilation.chunkGraph; | 
					
						
							|  |  |  | 						if ( | 
					
						
							|  |  |  | 							!chunkGraph.hasModuleInGraph( | 
					
						
							|  |  |  | 								chunk, | 
					
						
							| 
									
										
										
										
											2019-07-15 21:03:29 +08:00
										 |  |  | 								m => m.type === "webassembly/async" | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | 							) | 
					
						
							|  |  |  | 						) { | 
					
						
							|  |  |  | 							return; | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						set.add(RuntimeGlobals.publicPath); | 
					
						
							|  |  |  | 						compilation.addRuntimeModule( | 
					
						
							|  |  |  | 							chunk, | 
					
						
							| 
									
										
										
										
											2019-08-27 02:21:07 +08:00
										 |  |  | 							new AsyncWasmChunkLoadingRuntimeModule({ | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | 								generateLoadBinaryCode, | 
					
						
							|  |  |  | 								supportsStreaming: false | 
					
						
							|  |  |  | 							}) | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = ReadFileCompileAsyncWasmPlugin; |