Skip to content

Commit 4702ee9

Browse files
committed
Use compile/compileStreaming when importObject is a Promise
1 parent 8187393 commit 4702ee9

4 files changed

Lines changed: 50 additions & 20 deletions

File tree

‎lib/node/ReadFileCompileWasmTemplatePlugin.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ class ReadFileCompileWasmTemplatePlugin {
3939
"})"
4040
]);
4141

42-
new WasmMainTemplatePlugin(generateLoadBinaryCode).apply(
43-
compilation.mainTemplate
42+
const plugin = new WasmMainTemplatePlugin(
43+
generateLoadBinaryCode,
44+
false
4445
);
46+
plugin.apply(compilation.mainTemplate);
4547
}
4648
);
4749
}

‎lib/wasm/WasmMainTemplatePlugin.js‎

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,9 @@ function generateImportObject(module) {
127127
}
128128

129129
class 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
]),

‎lib/wasm/WebAssemblyJavascriptGenerator.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class WebAssemblyJavascriptGenerator extends Generator {
8282
[
8383
'"use strict";',
8484
"// Instantiate WebAssembly module",
85-
"var instance = __webpack_require__.w[module.i]",
85+
"var instance = __webpack_require__.w[module.i];",
8686

8787
// this must be before import for circular dependencies
8888
"// export exports from WebAssembly module",

‎lib/web/FetchCompileWasmTemplatePlugin.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ class FetchCompileWasmTemplatePlugin {
1515
const generateLoadBinaryCode = path =>
1616
`fetch(${mainTemplate.requireFn}.p + ${path})`;
1717

18-
new WasmMainTemplatePlugin(generateLoadBinaryCode).apply(mainTemplate);
18+
const plugin = new WasmMainTemplatePlugin(generateLoadBinaryCode, true);
19+
plugin.apply(mainTemplate);
1920
}
2021
);
2122
}

0 commit comments

Comments
 (0)