Skip to content

Add a --target option target different environment (nodejs, browser) #125

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ pub fn cargo_install_wasm_bindgen() -> Result<(), Error> {
}
}

pub fn wasm_bindgen_build(path: &str, name: &str, disable_dts: bool) -> Result<(), Error> {
pub fn wasm_bindgen_build(
path: &str,
name: &str,
disable_dts: bool,
target: String,
) -> Result<(), Error> {
let step = format!(
"{} {}Running WASM-bindgen...",
style("[7/7]").bold().dim(),
Expand All @@ -37,18 +42,25 @@ pub fn wasm_bindgen_build(path: &str, name: &str, disable_dts: bool) -> Result<(
let pb = PBAR.message(&step);
let binary_name = name.replace("-", "_");
let wasm_path = format!("target/wasm32-unknown-unknown/release/{}.wasm", binary_name);

let dts_arg = if disable_dts == false {
"--typescript"
} else {
"--no-typescript"
};

let target_arg = match target.as_str() {
"nodejs" => "--nodejs",
_ => "--browser",
};

let output = Command::new("wasm-bindgen")
.current_dir(path)
.arg(&wasm_path)
.arg("--out-dir")
.arg("./pkg")
.arg(dts_arg)
.arg(target_arg)
.output()?;
pb.finish();
if !output.status.success() {
Expand Down
30 changes: 24 additions & 6 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,18 @@ pub enum Command {
/// 🐣 initialize a package.json based on your compiled wasm!
Init {
path: Option<String>,

#[structopt(long = "scope", short = "s")]
scope: Option<String>,

#[structopt(long = "no-typescript")]
/// By default a *.d.ts file is generated for the generated JS file, but
/// this flag will disable generating this TypeScript file.
disable_dts: bool,

#[structopt(long = "target", short = "t", default_value = "browser")]
/// Sets the target environment. [possible values: browser, nodejs]
target: String,
},

#[structopt(name = "pack")]
Expand All @@ -39,22 +47,30 @@ pub enum Command {
Login {
#[structopt(long = "registry", short = "r")]
/// Default: 'https://registry.npmjs.org/'.
/// The base URL of the npm package registry. If scope is also specified, this registry will only be used for packages with that scope. scope defaults to the scope of the project directory you're currently in, if any.
/// The base URL of the npm package registry. If scope is also
/// specified, this registry will only be used for packages with that
/// scope. scope defaults to the scope of the project directory you're
/// currently in, if any.
registry: Option<String>,

#[structopt(long = "scope", short = "s")]
/// Default: none.
/// If specified, the user and login credentials given will be associated with the specified scope.
/// If specified, the user and login credentials given will be
/// associated with the specified scope.
scope: Option<String>,

#[structopt(long = "always-auth", short = "a")]
/// If specified, save configuration indicating that all requests to the given registry should include authorization information. Useful for private registries. Can be used with --registry and / or --scope
/// If specified, save configuration indicating that all requests to the
/// given registry should include authorization information. Useful for
/// private registries. Can be used with --registry and / or --scope
always_auth: bool,

#[structopt(long = "auth-type", short = "t")]
/// Default: 'legacy'.
/// Type: 'legacy', 'sso', 'saml', 'oauth'.
/// What authentication strategy to use with adduser/login. Some npm registries (for example, npmE) might support alternative auth strategies besides classic username/password entry in legacy npm.
/// What authentication strategy to use with adduser/login. Some npm
/// registries (for example, npmE) might support alternative auth
/// strategies besides classic username/password entry in legacy npm.
auth_type: Option<String>,
},
}
Expand All @@ -67,7 +83,8 @@ pub fn run_wasm_pack(command: Command) -> result::Result<(), Error> {
path,
scope,
disable_dts,
} => init(path, scope, disable_dts),
target,
} => init(path, scope, disable_dts, target),
Command::Pack { path } => pack(path),
Command::Publish { path } => publish(path),
Command::Login {
Expand Down Expand Up @@ -114,6 +131,7 @@ fn init(
path: Option<String>,
scope: Option<String>,
disable_dts: bool,
target: String,
) -> result::Result<(), Error> {
let started = Instant::now();

Expand All @@ -126,7 +144,7 @@ fn init(
readme::copy_from_crate(&crate_path)?;
bindgen::cargo_install_wasm_bindgen()?;
let name = manifest::get_crate_name(&crate_path)?;
bindgen::wasm_bindgen_build(&crate_path, &name, disable_dts)?;
bindgen::wasm_bindgen_build(&crate_path, &name, disable_dts, target)?;
PBAR.message(&format!(
"{} Done in {}",
emoji::SPARKLE,
Expand Down