| 
									
										
										
										
											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"); | 
					
						
							| 
									
										
										
										
											2021-08-16 15:43:50 +08:00
										 |  |  | const AsyncWasmLoadingRuntimeModule = require("../wasm-async/AsyncWasmLoadingRuntimeModule"); | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** @typedef {import("../Compiler")} Compiler */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ReadFileCompileAsyncWasmPlugin { | 
					
						
							| 
									
										
										
										
											2021-06-24 16:05:37 +08:00
										 |  |  | 	constructor({ type = "async-node", import: useImport = false } = {}) { | 
					
						
							|  |  |  | 		this._type = type; | 
					
						
							|  |  |  | 		this._import = useImport; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											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 => { | 
					
						
							| 
									
										
										
										
											2020-09-09 15:23:01 +08:00
										 |  |  | 				const globalWasmLoading = compilation.outputOptions.wasmLoading; | 
					
						
							|  |  |  | 				const isEnabledForChunk = chunk => { | 
					
						
							|  |  |  | 					const options = chunk.getEntryOptions(); | 
					
						
							|  |  |  | 					const wasmLoading = | 
					
						
							| 
									
										
										
										
											2021-04-15 02:21:17 +08:00
										 |  |  | 						options && options.wasmLoading !== undefined | 
					
						
							|  |  |  | 							? options.wasmLoading | 
					
						
							|  |  |  | 							: globalWasmLoading; | 
					
						
							| 
									
										
										
										
											2021-06-24 16:05:37 +08:00
										 |  |  | 					return wasmLoading === this._type; | 
					
						
							| 
									
										
										
										
											2020-09-09 15:23:01 +08:00
										 |  |  | 				}; | 
					
						
							| 
									
										
										
										
											2021-06-24 16:05:37 +08:00
										 |  |  | 				const generateLoadBinaryCode = this._import | 
					
						
							|  |  |  | 					? path => | 
					
						
							|  |  |  | 							Template.asString([ | 
					
						
							|  |  |  | 								"Promise.all([import('fs'), import('url')]).then(([{ readFile }, { URL }]) => new Promise((resolve, reject) => {", | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | 								Template.indent([ | 
					
						
							| 
									
										
										
										
											2021-06-24 16:05:37 +08:00
										 |  |  | 									`readFile(new URL(${path}, import.meta.url), (err, buffer) => {`, | 
					
						
							|  |  |  | 									Template.indent([ | 
					
						
							|  |  |  | 										"if (err) return reject(err);", | 
					
						
							|  |  |  | 										"", | 
					
						
							|  |  |  | 										"// Fake fetch response", | 
					
						
							|  |  |  | 										"resolve({", | 
					
						
							|  |  |  | 										Template.indent(["arrayBuffer() { return buffer; }"]), | 
					
						
							|  |  |  | 										"});" | 
					
						
							|  |  |  | 									]), | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | 									"});" | 
					
						
							|  |  |  | 								]), | 
					
						
							| 
									
										
										
										
											2021-06-24 16:05:37 +08:00
										 |  |  | 								"}))" | 
					
						
							|  |  |  | 							]) | 
					
						
							|  |  |  | 					: path => | 
					
						
							|  |  |  | 							Template.asString([ | 
					
						
							|  |  |  | 								"new Promise(function (resolve, reject) {", | 
					
						
							|  |  |  | 								Template.indent([ | 
					
						
							|  |  |  | 									"try {", | 
					
						
							|  |  |  | 									Template.indent([ | 
					
						
							|  |  |  | 										"var { readFile } = require('fs');", | 
					
						
							|  |  |  | 										"var { join } = require('path');", | 
					
						
							|  |  |  | 										"", | 
					
						
							|  |  |  | 										`readFile(join(__dirname, ${path}), function(err, buffer){`, | 
					
						
							|  |  |  | 										Template.indent([ | 
					
						
							|  |  |  | 											"if (err) return reject(err);", | 
					
						
							|  |  |  | 											"", | 
					
						
							|  |  |  | 											"// Fake fetch response", | 
					
						
							|  |  |  | 											"resolve({", | 
					
						
							|  |  |  | 											Template.indent(["arrayBuffer() { return buffer; }"]), | 
					
						
							|  |  |  | 											"});" | 
					
						
							|  |  |  | 										]), | 
					
						
							|  |  |  | 										"});" | 
					
						
							|  |  |  | 									]), | 
					
						
							|  |  |  | 									"} catch (err) { reject(err); }" | 
					
						
							|  |  |  | 								]), | 
					
						
							|  |  |  | 								"})" | 
					
						
							|  |  |  | 							]); | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				compilation.hooks.runtimeRequirementInTree | 
					
						
							|  |  |  | 					.for(RuntimeGlobals.instantiateWasm) | 
					
						
							|  |  |  | 					.tap("ReadFileCompileAsyncWasmPlugin", (chunk, set) => { | 
					
						
							| 
									
										
										
										
											2020-09-09 15:23:01 +08:00
										 |  |  | 						if (!isEnabledForChunk(chunk)) return; | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | 						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, | 
					
						
							| 
									
										
										
										
											2021-08-16 15:43:50 +08:00
										 |  |  | 							new AsyncWasmLoadingRuntimeModule({ | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | 								generateLoadBinaryCode, | 
					
						
							|  |  |  | 								supportsStreaming: false | 
					
						
							|  |  |  | 							}) | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = ReadFileCompileAsyncWasmPlugin; |