Skip to content

Commit 85c9f23

Browse files
committed
Expose on window.wasm_bindgen
1 parent ffdb8a6 commit 85c9f23

File tree

1 file changed

+20
-15
lines changed
  • crates/cli-support/src

1 file changed

+20
-15
lines changed

crates/cli-support/src/js.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -231,28 +231,33 @@ impl<'a> Context<'a> {
231231
format!("var wasm;")
232232
} else if self.config.no_modules {
233233
format!("
234-
window.{module} = fetch('{module}_bg.wasm')
235-
.then(response => response.arrayBuffer())
236-
.then(buffer => WebAssembly.instantiate(buffer, {{ './{module}': __exports }}))
237-
.then(({{instance}}) => {{
238-
wasm = instance.exports;
239-
return wasm;
240-
}})
241-
.catch(error => {{
242-
console.log('Error loading wasm module `{module}`:', error);
243-
throw error;
244-
}});
234+
window.wasm_bindgen.init = function(__wasm_path) {{
235+
return fetch(__wasm_path)
236+
.then(response => response.arrayBuffer())
237+
.then(buffer => WebAssembly.instantiate(buffer, {{ './{module}': __exports }}))
238+
.then(({{instance}}) => {{
239+
wasm = instance.exports;
240+
return;
241+
}})
242+
.catch(error => {{
243+
console.log('Error loading wasm module `{module}`:', error);
244+
throw error;
245+
}});
246+
}};
245247
", module = module_name)
246248
} else {
247249
format!("import * as wasm from './{}_bg';", module_name)
248250
};
249251

250252
let js = if self.config.no_modules {
251253
format!("
252-
let wasm;
253-
const __exports = {{}};
254-
{globals}
255-
{import_wasm}
254+
(function() {{
255+
let wasm;
256+
const __exports = {{}};
257+
{globals}
258+
window.wasm_bindgen = Object.assign({{}}, __exports);
259+
{import_wasm}
260+
}})();
256261
",
257262
globals = self.globals,
258263
import_wasm = import_wasm,

0 commit comments

Comments
 (0)