Skip to content

Commit ee0b5af

Browse files
committed
generate shorter runtime code for the imports map
1 parent cb3a0ed commit ee0b5af

1 file changed

Lines changed: 32 additions & 13 deletions

File tree

‎lib/wasm/WasmMainTemplatePlugin.js‎

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function getAllWasmModules(chunk) {
2323
}
2424

2525
function generateImportObject(module) {
26-
const waitForPromises = new Map();
26+
const waitForInstances = new Map();
2727
const properties = [];
2828
let importIndex = 0;
2929
for (const dep of module.dependencies) {
@@ -50,11 +50,8 @@ function generateImportObject(module) {
5050
const propertyName = Template.numberToIdentifer(index);
5151

5252
if (direct) {
53-
const instanceVar = `m${waitForPromises.size}`;
54-
waitForPromises.set(
55-
instanceVar,
56-
`installedWasmModules[${JSON.stringify(importedModule.id)}]`
57-
);
53+
const instanceVar = `m${waitForInstances.size}`;
54+
waitForInstances.set(instanceVar, importedModule.id);
5855
properties.push(
5956
`${JSON.stringify(propertyName)}: ${instanceVar}` +
6057
`[${JSON.stringify(usedName)}]`
@@ -83,18 +80,38 @@ function generateImportObject(module) {
8380
}
8481
}
8582

86-
if (waitForPromises.size > 0) {
87-
const promises = Array.from(waitForPromises.values()).join(", ");
83+
if (waitForInstances.size === 1) {
84+
const moduleId = Array.from(waitForInstances.values())[0];
85+
const promise = `installedWasmModules[${JSON.stringify(moduleId)}]`;
86+
const variable = Array.from(waitForInstances.keys())[0];
87+
return Template.asString([
88+
`${JSON.stringify(module.id)}: function() {`,
89+
Template.indent([
90+
`return promiseResolve().then(function() { return ${promise}; }).then(function(${variable}) {`,
91+
Template.indent([
92+
"return {",
93+
Template.indent([properties.join(",\n")]),
94+
"};"
95+
]),
96+
"});"
97+
]),
98+
"},"
99+
]);
100+
} else if (waitForInstances.size > 0) {
101+
const promises = Array.from(
102+
waitForInstances.values(),
103+
id => `installedWasmModules[${JSON.stringify(id)}]`
104+
).join(", ");
88105
const variables = Array.from(
89-
waitForPromises.keys(),
90-
(name, i) => `var ${name} = array[${i}];`
91-
).join("\n");
106+
waitForInstances.keys(),
107+
(name, i) => `${name} = array[${i}];`
108+
).join(", ");
92109
return Template.asString([
93110
`${JSON.stringify(module.id)}: function() {`,
94111
Template.indent([
95-
`return Promise.resolve().then(function() { return Promise.all([${promises}]); }).then(function(array) {`,
112+
`return promiseResolve().then(function() { return Promise.all([${promises}]); }).then(function(array) {`,
96113
Template.indent([
97-
variables,
114+
`var ${variables};`,
98115
"return {",
99116
Template.indent([properties.join(",\n")]),
100117
"};"
@@ -134,6 +151,8 @@ class WasmMainTemplatePlugin {
134151
"// object to store loaded and loading wasm modules",
135152
"var installedWasmModules = {};",
136153
"",
154+
"function promiseResolve() { return Promise.resolve(); }",
155+
"",
137156
"var wasmImportObjects = {",
138157
Template.indent(importObjects),
139158
"};"

0 commit comments

Comments
 (0)