Skip to main content

wasm_bindgen_cli/
wasm_bindgen.rs

1use anyhow::{bail, Error};
2use clap::{Parser, ValueEnum};
3use std::ffi::OsString;
4use std::path::PathBuf;
5use wasm_bindgen_cli_support::{Bindgen, EncodeInto};
6
7#[derive(Debug, Clone, ValueEnum)]
8#[clap(rename_all = "kebab-case")]
9enum Target {
10    Bundler,
11    Web,
12    Nodejs,
13    NoModules,
14    Deno,
15    ExperimentalNodejsModule,
16    Module,
17}
18
19#[derive(Debug, Parser)]
20#[command(
21    name = "wasm-bindgen",
22    version,
23    about,
24    after_help = "Additional documentation: https://wasm-bindgen.github.io/wasm-bindgen/reference/cli.html"
25)]
26struct Args {
27    input: PathBuf,
28    #[arg(
29        long,
30        value_name = "TARGET",
31        value_enum,
32        default_value_t = Target::Bundler,
33        help = "What type of output to generate",
34        group = "target-group"
35    )]
36    target: Target,
37    #[arg(long, value_name = "DIR", help = "Output directory")]
38    out_dir: PathBuf,
39    #[arg(
40        long,
41        help = "Hint that JS should only be compatible with a browser",
42        group = "target-group"
43    )]
44    browser: bool,
45    #[arg(long, help = "Don't emit a *.d.ts file", conflicts_with = "typescript")]
46    no_typescript: bool,
47    #[arg(long, help = "Don't emit imports in generated JavaScript")]
48    omit_imports: bool,
49    #[arg(
50        long,
51        value_name = "VAR",
52        help = "Set a custom output filename (Without extension. Defaults to crate name)"
53    )]
54    out_name: Option<String>,
55    #[arg(long, help = "Include otherwise-extraneous debug checks in output")]
56    debug: bool,
57    #[arg(long, help = "Don't demangle Rust symbol names")]
58    no_demangle: bool,
59    #[arg(
60        long,
61        value_name = "VAR",
62        help = "Name of the global variable to initialize"
63    )]
64    no_modules_global: Option<String>,
65    #[arg(long, help = "Remove the debugging `name` section of the file")]
66    remove_name_section: bool,
67    #[arg(long, help = "Remove the telemetry `producers` section")]
68    remove_producers_section: bool,
69    #[arg(long, help = "Keep exports synthesized by LLD")]
70    keep_lld_exports: bool,
71    #[arg(long, help = "Keep debug sections in Wasm files")]
72    keep_debug: bool,
73    #[arg(
74        long,
75        value_name = "MODE",
76        help = "Whether or not to use TextEncoder#encodeInto",
77        value_parser = ["test", "always", "never"]
78    )]
79    encode_into: Option<String>,
80    #[arg(
81        long,
82        help = "Don't add WebAssembly fallback imports in generated JavaScript"
83    )]
84    omit_default_module_path: bool,
85    #[arg(
86        long,
87        help = "Split linked modules out into their own files. Recommended if possible.\n\
88                If a bundler is used, it needs to be set up accordingly."
89    )]
90    split_linked_modules: bool,
91    #[arg(
92        long = "experimental-reset-state-function",
93        help = "Generate __wbg_reset_state function for WASM reinitialization (experimental)"
94    )]
95    generate_reset_state: bool,
96    // The options below are deprecated. They're still parsed for backwards compatibility,
97    // but we don't want to show them in `--help` to avoid distracting users.
98    #[arg(long, hide = true)]
99    #[deprecated(note = "implied default, only `--no-typescript` is needed")]
100    typescript: bool,
101    #[arg(long, hide = true, group = "target-group")]
102    #[deprecated(note = "use `Args::target` instead")]
103    nodejs: bool,
104    #[arg(long, hide = true, group = "target-group")]
105    #[deprecated(note = "use `Args::target` instead")]
106    web: bool,
107    #[arg(long, hide = true, group = "target-group")]
108    #[deprecated(note = "use `Args::target` instead")]
109    no_modules: bool,
110    #[arg(long, hide = true)]
111    #[deprecated(note = "runtime-detected")]
112    #[allow(dead_code)]
113    weak_refs: bool,
114    #[arg(long, hide = true)]
115    #[deprecated(note = "automatically inferred from the Wasm features")]
116    reference_types: bool,
117}
118
119pub fn run_cli_with_args<I, T>(args: I) -> anyhow::Result<()>
120where
121    I: IntoIterator<Item = T>,
122    T: Into<OsString> + Clone,
123{
124    let args = match Args::try_parse_from(args) {
125        Ok(a) => a,
126        Err(e) => match e.kind() {
127            // Passing --version and --help should not result in a failure.
128            clap::error::ErrorKind::DisplayHelp | clap::error::ErrorKind::DisplayVersion => {
129                print!("{e}");
130                return Ok(());
131            }
132            _ => bail!(e),
133        },
134    };
135    rmain(&args)
136}
137
138fn rmain(args: &Args) -> Result<(), Error> {
139    let mut b = Bindgen::new();
140    match &args.target {
141        Target::Bundler => b.bundler(true)?,
142        Target::Web => b.web(true)?,
143        Target::NoModules => b.no_modules(true)?,
144        Target::Nodejs => b.nodejs(true)?,
145        Target::Deno => b.deno(true)?,
146        Target::ExperimentalNodejsModule => b.nodejs_module(true)?,
147        Target::Module => b.module(true)?,
148    };
149    #[allow(deprecated)]
150    b.input_path(&args.input)
151        .nodejs(args.nodejs)?
152        .web(args.web)?
153        .browser(args.browser)?
154        .no_modules(args.no_modules)?
155        .debug(args.debug)
156        .demangle(!args.no_demangle)
157        .keep_lld_exports(args.keep_lld_exports)
158        .keep_debug(args.keep_debug)
159        .remove_name_section(args.remove_name_section)
160        .remove_producers_section(args.remove_producers_section)
161        .typescript(!args.no_typescript)
162        .omit_imports(args.omit_imports)
163        .omit_default_module_path(args.omit_default_module_path)
164        .split_linked_modules(args.split_linked_modules)
165        .reference_types(args.reference_types)
166        .reset_state_function(args.generate_reset_state);
167
168    if let Some(ref name) = args.no_modules_global {
169        b.no_modules_global(name)?;
170    }
171    if let Some(ref name) = args.out_name {
172        b.out_name(name);
173    }
174
175    if let Some(mode) = &args.encode_into {
176        let mode = match mode.as_str() {
177            "test" => EncodeInto::Test,
178            "always" => EncodeInto::Always,
179            "never" => EncodeInto::Never,
180            // clap guarantees
181            _ => unreachable!(),
182        };
183        b.encode_into(mode);
184    }
185
186    b.generate(&args.out_dir)
187}