Closed
Description
When a wasm_bindgen
annotated enumeration is defined in a module, any item from that module is used somewhere else in a crate, and crate was compiled in release mode, then wasm-bindgen
generates JS constant for that enumeration once for definition, plus once for every other module where an item from the same module as enum is used. This does not happen when compiled in debug mode.
Last tested on 2018-07-16, commit 1d3e8f4.
Reduced test case
Cargo.toml
[package]
name = "case"
version = "0.0.0"
[lib]
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "0.2.11"
[patch.crates-io]
wasm-bindgen = { git = "https://github.com/rustwasm/wasm-bindgen.git" }
src/lib.rs
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
pub mod module;
#[wasm_bindgen]
pub fn test() -> module::Struct {
module::Struct(0)
}
lib/module.rs
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub struct Struct(pub usize);
#[wasm_bindgen]
#[derive(Debug)]
pub enum Enum {
A,
}
When compiled in release mode wasm-bindgen
outputs the following
/* tslint:disable */
import * as wasm from './case_bg';
/**
*/
export const Enum = Object.freeze({ A:0, });
/**
* @returns {Struct}
*/
export function test() {
return Struct.__construct(wasm.test());
}
/**
*/
export const Enum = Object.freeze({ A:0, });
/**
*/
export class Struct {
// Rest cut for brevity
Note that export const Enum
appears twice. When a third module containing a function returning Struct
was added, export const Enum
appeared three times. When compiled in release mode export const Enum
appears only once, regardless of number of modules.