Skip to content

[WIP] Introduce an All target for outputting all supported module types by wasm-pack #705

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
88 changes: 88 additions & 0 deletions src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,93 @@ pub fn wasm_bindgen_build(
disable_dts: bool,
target: Target,
profile: BuildProfile,
) -> Result<(), failure::Error> {
match target {
Target::All => {
// Bundler
// NOTE: We only generate our type definitions here if we want them
let mut out_name_for_bundler = Some(format!("{}_esm", data.crate_name()));
if let Some(value) = out_name {
out_name_for_bundler = Some(format!("{}_esm", value));
}
run_wasm_bindgen(
data,
bindgen,
out_dir,
&out_name_for_bundler,
disable_dts,
Target::Bundler,
profile,
)?;

// Web
let mut out_name_for_web = Some(format!("{}_web", data.crate_name()));
if let Some(value) = out_name {
out_name_for_web = Some(format!("{}_web", value));
}
run_wasm_bindgen(
data,
bindgen,
out_dir,
&out_name_for_web,
disable_dts,
Target::Web,
profile,
)?;

// Nodejs
let mut out_name_for_nodejs = Some(format!("{}_cjs", data.crate_name()));
if let Some(value) = out_name {
out_name_for_nodejs = Some(format!("{}_cjs", value));
}
run_wasm_bindgen(
data,
bindgen,
out_dir,
&out_name_for_nodejs,
true,
Target::Nodejs,
profile,
)?;

// NoModules
let mut out_name_for_nomodules = Some(format!("{}_browser", data.crate_name()));
if let Some(value) = out_name {
out_name_for_nomodules = Some(format!("{}_browser", value));
}
run_wasm_bindgen(
data,
bindgen,
out_dir,
&out_name_for_nomodules,
disable_dts,
Target::NoModules,
profile,
)?;
}
_ => {
run_wasm_bindgen(
data,
bindgen,
out_dir,
out_name,
disable_dts,
target,
profile,
)?;
}
}
Ok(())
}

fn run_wasm_bindgen(
data: &CrateData,
bindgen: &Download,
out_dir: &Path,
out_name: &Option<String>,
disable_dts: bool,
target: Target,
profile: BuildProfile,
) -> Result<(), failure::Error> {
let release_or_debug = match profile {
BuildProfile::Release | BuildProfile::Profiling => "release",
Expand Down Expand Up @@ -115,6 +202,7 @@ fn build_target_arg_legacy(target: Target, cli_path: &PathBuf) -> Result<String,
}
}
Target::Bundler => "--browser",
_ => bail!(format!("The passed target, {}, is not supported", target)),
};
Ok(target_arg.to_string())
}
6 changes: 5 additions & 1 deletion src/command/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ pub enum Target {
/// in a browser but pollutes the global namespace and must be manually
/// instantiated.
NoModules,
/// Correspond to `--target all` where the output is all other targets
All,
Copy link
Member

@ashleygwilliams ashleygwilliams Aug 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

given @Pauan's comment and the likelihood that folks will want to customize the types of targets they want... i could imagine that instead of an All type, you have a new type that is a Targets: Vec<Target>- and for just Node, the Targets would by Vec<Nodejs>, and all would be Vec<Web, Nodejs, NoModules>

}

impl Default for Target {
Expand All @@ -69,6 +71,7 @@ impl fmt::Display for Target {
Target::Web => "web",
Target::Nodejs => "nodejs",
Target::NoModules => "no-modules",
Target::All => "all",
};
write!(f, "{}", s)
}
Expand All @@ -82,6 +85,7 @@ impl FromStr for Target {
"web" => Ok(Target::Web),
"nodejs" => Ok(Target::Nodejs),
"no-modules" => Ok(Target::NoModules),
"all" => Ok(Target::All),
_ => bail!("Unknown target: {}", s),
}
}
Expand Down Expand Up @@ -120,7 +124,7 @@ pub struct BuildOptions {
pub disable_dts: bool,

#[structopt(long = "target", short = "t", default_value = "bundler")]
/// Sets the target environment. [possible values: bundler, nodejs, web, no-modules]
/// Sets the target environment. [possible values: bundler, nodejs, web, no-modules, all]
pub target: Target,

#[structopt(long = "debug")]
Expand Down
Loading