diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index 574e73f9d31..537f2107911 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -611,14 +611,14 @@ impl<'a> Context<'a> { } let default_module_path = match self.config.mode { - OutputMode::Web => { - "\ - if (typeof input === 'undefined') { - input = import.meta.url.replace(/\\.js$/, '_bg.wasm'); - }" - } - OutputMode::NoModules { .. } => { + OutputMode::Web => format!( "\ + if (typeof input === 'undefined') {{ + input = new URL('{stem}_bg.wasm', import.meta.url); + }}", + stem = self.config.stem()? + ), + OutputMode::NoModules { .. } => "\ if (typeof input === 'undefined') { let src; if (typeof document === 'undefined') { @@ -628,8 +628,8 @@ impl<'a> Context<'a> { } input = src.replace(/\\.js$/, '_bg.wasm'); }" - } - _ => "", + .to_string(), + _ => "".to_string(), }; let ts = self.ts_for_init_fn(has_memory, !default_module_path.is_empty())?; diff --git a/crates/cli-support/src/lib.rs b/crates/cli-support/src/lib.rs index bb2b4e2f3e5..26e537a763e 100755 --- a/crates/cli-support/src/lib.rs +++ b/crates/cli-support/src/lib.rs @@ -286,12 +286,23 @@ impl Bindgen { self.generate_output()?.emit(path.as_ref()) } + pub fn stem(&self) -> Result<&str, Error> { + Ok(match &self.input { + Input::None => bail!("must have an input by now"), + Input::Module(_, name) => name, + Input::Path(path) => match &self.out_name { + Some(name) => name, + None => path.file_stem().unwrap().to_str().unwrap(), + }, + }) + } + pub fn generate_output(&mut self) -> Result { - let (mut module, stem) = match self.input { + let mut module = match self.input { Input::None => bail!("must have an input by now"), - Input::Module(ref mut m, ref name) => { + Input::Module(ref mut m, _) => { let blank_module = Module::default(); - (mem::replace(m, blank_module), &name[..]) + mem::replace(m, blank_module) } Input::Path(ref path) => { let wasm = wit_text::parse_file(&path) @@ -312,11 +323,7 @@ impl Bindgen { .on_parse(wit_walrus::on_parse) .parse(&wasm) .context("failed to parse input file as wasm")?; - let stem = match &self.out_name { - Some(name) => &name, - None => path.file_stem().unwrap().to_str().unwrap(), - }; - (module, stem) + module } }; @@ -409,6 +416,8 @@ impl Bindgen { // unnecessary things here. gc_module_and_adapters(&mut module); + let stem = self.stem()?; + // We're ready for the final emission passes now. If we're in wasm // interface types mode then we execute the various passes there and // generate a valid interface typess section into the wasm module.