Skip to content

Commit 17e7904

Browse files
committed
Emit new URL('...', import.meta.url) for Wasm
Lots of bundlers now support `new URL('...path...', import.meta.url)` as an explicit signal to specify assets that should be bundled alongside JS. This is already supported by default in Webpack v5, Parcel v2,Vite, Snowpack and WMR, and available via a plugin - https://modern-web.dev/docs/building/rollup-plugin-import-meta-assets/ - in Rollup as well. Emitting such pattern by default would allow all those bundlers to automatically include Wasm and generate a correct URL for `--target web` builds without any work from the user's side.
1 parent d6228e6 commit 17e7904

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

crates/cli-support/src/js/mod.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -611,14 +611,13 @@ impl<'a> Context<'a> {
611611
}
612612

613613
let default_module_path = match self.config.mode {
614-
OutputMode::Web => {
615-
"\
616-
if (typeof input === 'undefined') {
617-
input = import.meta.url.replace(/\\.js$/, '_bg.wasm');
618-
}"
619-
}
620-
OutputMode::NoModules { .. } => {
614+
OutputMode::Web => format!(
621615
"\
616+
if (typeof input === 'undefined') {{
617+
input = new URL('{stem}_bg.wasm', import.meta.url);
618+
}}"
619+
, stem = self.config.stem()?),
620+
OutputMode::NoModules { .. } => "\
622621
if (typeof input === 'undefined') {
623622
let src;
624623
if (typeof document === 'undefined') {
@@ -627,9 +626,8 @@ impl<'a> Context<'a> {
627626
src = document.currentScript.src;
628627
}
629628
input = src.replace(/\\.js$/, '_bg.wasm');
630-
}"
631-
}
632-
_ => "",
629+
}".to_string(),
630+
_ => "".to_string(),
633631
};
634632

635633
let ts = self.ts_for_init_fn(has_memory, !default_module_path.is_empty())?;

crates/cli-support/src/lib.rs

+17-8
Original file line numberDiff line numberDiff line change
@@ -286,12 +286,23 @@ impl Bindgen {
286286
self.generate_output()?.emit(path.as_ref())
287287
}
288288

289+
pub fn stem(&self) -> Result<&str, Error> {
290+
Ok(match &self.input {
291+
Input::None => bail!("must have an input by now"),
292+
Input::Module(_, name) => name,
293+
Input::Path(path) => match &self.out_name {
294+
Some(name) => name,
295+
None => path.file_stem().unwrap().to_str().unwrap(),
296+
},
297+
})
298+
}
299+
289300
pub fn generate_output(&mut self) -> Result<Output, Error> {
290-
let (mut module, stem) = match self.input {
301+
let mut module = match self.input {
291302
Input::None => bail!("must have an input by now"),
292-
Input::Module(ref mut m, ref name) => {
303+
Input::Module(ref mut m, _) => {
293304
let blank_module = Module::default();
294-
(mem::replace(m, blank_module), &name[..])
305+
mem::replace(m, blank_module)
295306
}
296307
Input::Path(ref path) => {
297308
let wasm = wit_text::parse_file(&path)
@@ -312,11 +323,7 @@ impl Bindgen {
312323
.on_parse(wit_walrus::on_parse)
313324
.parse(&wasm)
314325
.context("failed to parse input file as wasm")?;
315-
let stem = match &self.out_name {
316-
Some(name) => &name,
317-
None => path.file_stem().unwrap().to_str().unwrap(),
318-
};
319-
(module, stem)
326+
module
320327
}
321328
};
322329

@@ -409,6 +416,8 @@ impl Bindgen {
409416
// unnecessary things here.
410417
gc_module_and_adapters(&mut module);
411418

419+
let stem = self.stem()?;
420+
412421
// We're ready for the final emission passes now. If we're in wasm
413422
// interface types mode then we execute the various passes there and
414423
// generate a valid interface typess section into the wasm module.

0 commit comments

Comments
 (0)