diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index e3f11466..3a42fd2d 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -1778,6 +1778,10 @@ impl<'a, 'b> SubContext<'a, 'b> { info: &shared::Import, import: &shared::ImportFunction, ) -> Result<(), Error> { + if !self.cx.wasm_import_needed(&import.shim) { + return Ok(()) + } + let descriptor = match self.cx.describe(&import.shim) { None => return Ok(()), Some(d) => d, diff --git a/tests/all/imports.rs b/tests/all/imports.rs index bc09154b..334dc700 100644 --- a/tests/all/imports.rs +++ b/tests/all/imports.rs @@ -1,3 +1,6 @@ +use std::fs::File; +use std::io::Read; + use super::project; #[test] @@ -668,3 +671,40 @@ fn custom_type() { "#) .test(); } + +#[test] +fn unused_imports_not_generated() { + project() + .debug(false) + .file("src/lib.rs", r#" + #![feature(proc_macro, wasm_custom_section, wasm_import_module)] + + extern crate wasm_bindgen; + + use wasm_bindgen::prelude::*; + + #[wasm_bindgen] + extern { + pub fn foo(); + } + + #[wasm_bindgen] + pub fn run() { + } + "#) + .file("test.ts", r#" + import { run } from "./out"; + + export function test() { + run(); + } + "#) + .test(); + + let out = ::root().join("out.js"); + let mut contents = String::new(); + File::open(&out).unwrap() + .read_to_string(&mut contents).unwrap(); + assert!(contents.contains("run"), "didn't find `run` in {}", contents); + assert!(!contents.contains("foo"), "found `foo` in {}", contents); +}