@@ -127,8 +127,9 @@ function generateImportObject(module) {
127127}
128128
129129class WasmMainTemplatePlugin {
130- constructor ( generateLoadBinaryCode ) {
130+ constructor ( generateLoadBinaryCode , supportsStreaming ) {
131131 this . generateLoadBinaryCode = generateLoadBinaryCode ;
132+ this . supportsStreaming = supportsStreaming ;
132133 }
133134 apply ( mainTemplate ) {
134135 mainTemplate . hooks . localVars . tap (
@@ -203,29 +204,55 @@ class WasmMainTemplatePlugin {
203204 Template . indent ( [ "promises.push(installedWasmModuleData);" ] ) ,
204205 "else {" ,
205206 Template . indent ( [
206- `var importObject = Promise.resolve( wasmImportObjects[wasmModuleId]() );` ,
207+ `var importObject = wasmImportObjects[wasmModuleId]();` ,
207208 `var req = ${ this . generateLoadBinaryCode ( wasmModuleSrcPath ) } ;` ,
208- "promises.push(installedWasmModules[wasmModuleId] = importObject.then(function(importObject) {" ,
209+ "var promise;" ,
210+ this . supportsStreaming
211+ ? Template . asString ( [
212+ "if(importObject instanceof Promise && typeof WebAssembly.compileStreaming === 'function') {" ,
213+ Template . indent ( [
214+ "promise = Promise.all([WebAssembly.compileStreaming(req), importObject]).then(function(items) {" ,
215+ Template . indent ( [
216+ "return WebAssembly.instantiate(items[0], items[1]);"
217+ ] ) ,
218+ "});"
219+ ] ) ,
220+ "} else if(typeof WebAssembly.instantiateStreaming === 'function') {" ,
221+ Template . indent ( [
222+ "promise = WebAssembly.instantiateStreaming(req, importObject);"
223+ ] )
224+ ] )
225+ : Template . asString ( [
226+ "if(importObject instanceof Promise) {" ,
227+ Template . indent ( [
228+ "var bytesPromise = req.then(function(x) { return x.arrayBuffer(); });" ,
229+ "promise = Promise.all([" ,
230+ Template . indent ( [
231+ "bytesPromise.then(function(bytes) { return WebAssembly.compile(bytes); })," ,
232+ "importObject"
233+ ] ) ,
234+ "]).then(function(items) {" ,
235+ Template . indent ( [
236+ "return WebAssembly.instantiate(items[0], items[1]);"
237+ ] ) ,
238+ "});"
239+ ] )
240+ ] ) ,
241+ "} else {" ,
209242 Template . indent ( [
210- "if(typeof WebAssembly.instantiateStreaming === 'function') {" ,
243+ "var bytesPromise = req.then(function(x) { return x.arrayBuffer(); });" ,
244+ "promise = bytesPromise.then(function(bytes) {" ,
211245 Template . indent ( [
212- "return WebAssembly.instantiateStreaming(req , importObject);"
246+ "return WebAssembly.instantiate(bytes , importObject);"
213247 ] ) ,
214- "} else {" ,
215- Template . indent ( [
216- "return req.then(x => x.arrayBuffer()).then(function(bytes) {" ,
217- Template . indent ( [
218- "return WebAssembly.instantiate(bytes, importObject);"
219- ] ) ,
220- "});"
221- ] ) ,
222- "}"
248+ "});"
223249 ] ) ,
224- "}).then(function(res) {" ,
250+ "}" ,
251+ "promises.push(installedWasmModules[wasmModuleId] = promise.then(function(res) {" ,
225252 Template . indent ( [
226253 `return ${
227254 mainTemplate . requireFn
228- } .w[wasmModuleId] = res.instance;`
255+ } .w[wasmModuleId] = res.instance || res ;`
229256 ] ) ,
230257 "}));"
231258 ] ) ,
0 commit comments