| 
									
										
										
										
											2017-11-12 01:48:29 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-12 01:48:29 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-01 01:56:32 +08:00
										 |  |  | const { WEBASSEMBLY_MODULE_TYPE_SYNC } = require("../ModuleTypeConstants"); | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | const RuntimeGlobals = require("../RuntimeGlobals"); | 
					
						
							| 
									
										
										
										
											2018-04-28 15:57:21 +08:00
										 |  |  | const Template = require("../Template"); | 
					
						
							| 
									
										
										
										
											2020-09-09 15:23:01 +08:00
										 |  |  | const WasmChunkLoadingRuntimeModule = require("../wasm-sync/WasmChunkLoadingRuntimeModule"); | 
					
						
							| 
									
										
										
										
											2017-11-12 01:48:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-26 02:31:28 +08:00
										 |  |  | /** @typedef {import("../Chunk")} Chunk */ | 
					
						
							| 
									
										
										
										
											2018-11-09 05:59:19 +08:00
										 |  |  | /** @typedef {import("../Compiler")} Compiler */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-26 02:31:28 +08:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2024-06-11 21:09:50 +08:00
										 |  |  |  * @typedef {object} ReadFileCompileWasmPluginOptions | 
					
						
							| 
									
										
										
										
											2023-05-26 02:31:28 +08:00
										 |  |  |  * @property {boolean} [mangleImports] mangle imports | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-09 15:23:01 +08:00
										 |  |  | // TODO webpack 6 remove
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | class ReadFileCompileWasmPlugin { | 
					
						
							| 
									
										
										
										
											2023-05-26 02:31:28 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2023-05-26 03:29:51 +08:00
										 |  |  | 	 * @param {ReadFileCompileWasmPluginOptions} [options] options object | 
					
						
							| 
									
										
										
										
											2023-05-26 02:31:28 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2023-05-26 03:29:51 +08:00
										 |  |  | 	constructor(options = {}) { | 
					
						
							|  |  |  | 		this.options = options; | 
					
						
							| 
									
										
										
										
											2018-06-06 05:38:29 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-09 05:59:19 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2020-04-23 16:48:36 +08:00
										 |  |  | 	 * Apply the plugin | 
					
						
							| 
									
										
										
										
											2018-11-09 05:59:19 +08:00
										 |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-11-12 01:48:29 +08:00
										 |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		compiler.hooks.thisCompilation.tap( | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 			"ReadFileCompileWasmPlugin", | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			compilation => { | 
					
						
							| 
									
										
										
										
											2020-09-09 15:23:01 +08:00
										 |  |  | 				const globalWasmLoading = compilation.outputOptions.wasmLoading; | 
					
						
							| 
									
										
										
										
											2023-05-26 02:31:28 +08:00
										 |  |  | 				/** | 
					
						
							|  |  |  | 				 * @param {Chunk} chunk chunk | 
					
						
							|  |  |  | 				 * @returns {boolean} true, when wasm loading is enabled for the chunk | 
					
						
							|  |  |  | 				 */ | 
					
						
							| 
									
										
										
										
											2020-09-09 15:23:01 +08:00
										 |  |  | 				const isEnabledForChunk = chunk => { | 
					
						
							|  |  |  | 					const options = chunk.getEntryOptions(); | 
					
						
							|  |  |  | 					const wasmLoading = | 
					
						
							| 
									
										
										
										
											2021-04-15 02:21:17 +08:00
										 |  |  | 						options && options.wasmLoading !== undefined | 
					
						
							|  |  |  | 							? options.wasmLoading | 
					
						
							|  |  |  | 							: globalWasmLoading; | 
					
						
							| 
									
										
										
										
											2020-09-09 15:23:01 +08:00
										 |  |  | 					return wasmLoading === "async-node"; | 
					
						
							|  |  |  | 				}; | 
					
						
							| 
									
										
										
										
											2023-05-26 02:31:28 +08:00
										 |  |  | 				/** | 
					
						
							|  |  |  | 				 * @param {string} path path to wasm file | 
					
						
							|  |  |  | 				 * @returns {string} generated code to load the wasm file | 
					
						
							|  |  |  | 				 */ | 
					
						
							| 
									
										
										
										
											2018-04-28 15:57:21 +08:00
										 |  |  | 				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; }"]), | 
					
						
							| 
									
										
										
										
											2018-04-28 15:57:21 +08:00
										 |  |  | 									"});" | 
					
						
							|  |  |  | 								]), | 
					
						
							|  |  |  | 								"});" | 
					
						
							|  |  |  | 							]), | 
					
						
							|  |  |  | 							"} catch (err) { reject(err); }" | 
					
						
							|  |  |  | 						]), | 
					
						
							|  |  |  | 						"})" | 
					
						
							|  |  |  | 					]); | 
					
						
							| 
									
										
										
										
											2018-03-15 02:05:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 				compilation.hooks.runtimeRequirementInTree | 
					
						
							|  |  |  | 					.for(RuntimeGlobals.ensureChunkHandlers) | 
					
						
							|  |  |  | 					.tap("ReadFileCompileWasmPlugin", (chunk, set) => { | 
					
						
							| 
									
										
										
										
											2020-09-09 15:23:01 +08:00
										 |  |  | 						if (!isEnabledForChunk(chunk)) return; | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 						const chunkGraph = compilation.chunkGraph; | 
					
						
							|  |  |  | 						if ( | 
					
						
							| 
									
										
										
										
											2019-05-24 18:30:43 +08:00
										 |  |  | 							!chunkGraph.hasModuleInGraph( | 
					
						
							|  |  |  | 								chunk, | 
					
						
							| 
									
										
										
										
											2023-04-01 01:56:32 +08:00
										 |  |  | 								m => m.type === WEBASSEMBLY_MODULE_TYPE_SYNC | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 							) | 
					
						
							|  |  |  | 						) { | 
					
						
							|  |  |  | 							return; | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						set.add(RuntimeGlobals.moduleCache); | 
					
						
							|  |  |  | 						compilation.addRuntimeModule( | 
					
						
							|  |  |  | 							chunk, | 
					
						
							| 
									
										
										
										
											2019-08-27 02:21:07 +08:00
										 |  |  | 							new WasmChunkLoadingRuntimeModule({ | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 								generateLoadBinaryCode, | 
					
						
							|  |  |  | 								supportsStreaming: false, | 
					
						
							| 
									
										
										
										
											2021-08-16 15:43:50 +08:00
										 |  |  | 								mangleImports: this.options.mangleImports, | 
					
						
							|  |  |  | 								runtimeRequirements: set | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | 							}) | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2017-11-12 01:48:29 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-23 16:37:33 +08:00
										 |  |  | module.exports = ReadFileCompileWasmPlugin; |