diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index 80f7c63f701..00795d8bf56 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -204,8 +204,18 @@ impl<'a> Context<'a> { // After all we've done, especially // `unexport_unused_internal_exports()`, we probably have a bunch of // garbage in the module that's no longer necessary, so delete - // everything that we don't actually need. + // everything that we don't actually need. Afterwards make sure we don't + // try to emit bindings for now-nonexistent imports by pruning our + // `wasm_import_definitions` set. walrus::passes::gc::run(self.module); + let remaining_imports = self + .module + .imports + .iter() + .map(|i| i.id()) + .collect::>(); + self.wasm_import_definitions + .retain(|id, _| remaining_imports.contains(id)); // Cause any future calls to `should_write_global` to panic, making sure // we don't ask for items which we can no longer emit. diff --git a/crates/cli/tests/wasm-bindgen/main.rs b/crates/cli/tests/wasm-bindgen/main.rs index b353634ff61..acec2cce813 100644 --- a/crates/cli/tests/wasm-bindgen/main.rs +++ b/crates/cli/tests/wasm-bindgen/main.rs @@ -136,3 +136,18 @@ fn works_on_empty_project() { } mod npm; + +#[test] +fn one_export_works() { + let (mut cmd, _out_dir) = Project::new("one_export_works") + .file( + "src/lib.rs", + r#" + use wasm_bindgen::prelude::*; + #[wasm_bindgen] + pub fn foo() {} + "#, + ) + .wasm_bindgen(""); + cmd.assert().success(); +}