mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-03-16 02:00:51 +00:00
Merge pull request #1579 from ibaryshnikov/default-init-module
added default module path inside init function when target is web
This commit is contained in:
commit
a7726545ac
@ -450,6 +450,17 @@ impl<'a> Context<'a> {
|
||||
} else {
|
||||
""
|
||||
};
|
||||
|
||||
let default_module_path = match self.config.mode {
|
||||
OutputMode::Web => {
|
||||
"\
|
||||
if (typeof module === 'undefined') {
|
||||
module = import.meta.url.replace(/\\.js$/, '_bg.wasm');
|
||||
}"
|
||||
}
|
||||
_ => "",
|
||||
};
|
||||
|
||||
let ts = Self::ts_for_init_fn(mem.import.is_some());
|
||||
|
||||
// Initialize the `imports` object for all import definitions that we're
|
||||
@ -475,6 +486,7 @@ impl<'a> Context<'a> {
|
||||
let js = format!(
|
||||
"\
|
||||
function init(module{init_memory_arg}) {{
|
||||
{default_module_path}
|
||||
let result;
|
||||
const imports = {{}};
|
||||
{imports_init}
|
||||
@ -518,6 +530,7 @@ impl<'a> Context<'a> {
|
||||
}}
|
||||
",
|
||||
init_memory_arg = init_memory_arg,
|
||||
default_module_path = default_module_path,
|
||||
init_memory1 = init_memory1,
|
||||
init_memory2 = init_memory2,
|
||||
start = if needs_manual_start {
|
||||
|
@ -12,20 +12,23 @@
|
||||
// will "boot" the module and make it ready to use. Currently browsers
|
||||
// don't support natively imported WebAssembly as an ES module, but
|
||||
// eventually the manual initialization won't be required!
|
||||
import { add, default as init } from './pkg/without_a_bundler.js';
|
||||
import init, { add } from './pkg/without_a_bundler.js';
|
||||
|
||||
async function run() {
|
||||
// First up we need to actually load the wasm file, so we use the
|
||||
// default export to inform it where the wasm file is located on the
|
||||
// server, and then we wait on the returned promise to wait for the
|
||||
// wasm to be loaded.
|
||||
// It may look like this: `await init('./pkg/without_a_bundler_bg.wasm');`,
|
||||
// but there is also a handy default inside `init` function, which uses
|
||||
// `import.meta` to locate the wasm file relatively to js file
|
||||
//
|
||||
// Note that instead of a string here you can also pass in an instance
|
||||
// of `WebAssembly.Module` which allows you to compile your own module.
|
||||
// Also note that the promise, when resolved, yields the wasm module's
|
||||
// exports which is the same as importing the `*_bg` module in other
|
||||
// modes
|
||||
await init('./pkg/without_a_bundler_bg.wasm');
|
||||
await init();
|
||||
|
||||
// And afterwards we can use all the functionality defined in wasm.
|
||||
const result = add(1, 2);
|
||||
|
Loading…
x
Reference in New Issue
Block a user