Don't generate JS bindings for unused imports

If a JS import's shim isn't actually imported that means that somewhere along
the way it was optimized out or it was never used in the first place! In that
case we can skip generation of the JS bindings for it as it's not needed.
This commit is contained in:
Alex Crichton 2018-06-29 15:52:31 -07:00
parent e93dc34f2f
commit e06255fba5
2 changed files with 44 additions and 0 deletions

View File

@ -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,

View File

@ -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);
}