Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit new URL('...', import.meta.url) for Wasm #2444

Merged
merged 1 commit into from
Feb 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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())?;
Expand Down
25 changes: 17 additions & 8 deletions crates/cli-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Output, Error> {
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)
Expand All @@ -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
}
};

Expand Down Expand Up @@ -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.
Expand Down