diff --git a/docs/src/commands/build.md b/docs/src/commands/build.md index 00ab2e9..8058e18 100644 --- a/docs/src/commands/build.md +++ b/docs/src/commands/build.md @@ -32,6 +32,16 @@ wasm-pack build --out-dir out The above command will put your build artifacts in a directory called `out`, instead of the default `pkg`. +## Output file names + +Set output file names. Defaults to package name. + +``` +wasm-pack build --out-name index +``` + +This command will build the package with file names like `index.js`, `index.d.ts`, and so on. + ## Profile The `build` command accepts an optional profile argument: one of `--dev`, diff --git a/src/bindgen.rs b/src/bindgen.rs index bf30d78..9caf1f3 100644 --- a/src/bindgen.rs +++ b/src/bindgen.rs @@ -173,6 +173,7 @@ pub fn wasm_bindgen_build( data: &CrateData, bindgen: &Download, out_dir: &Path, + out_name: &Option, disable_dts: bool, target: &Target, profile: BuildProfile, @@ -210,6 +211,10 @@ pub fn wasm_bindgen_build( .arg(dts_arg) .arg(target_arg); + if let Some(value) = out_name { + cmd.arg("--out-name").arg(value); + } + let profile = data.configured_profile(profile); if profile.wasm_bindgen_debug_js_glue() { cmd.arg("--debug"); diff --git a/src/command/build.rs b/src/command/build.rs index 924ef8e..af1926b 100644 --- a/src/command/build.rs +++ b/src/command/build.rs @@ -28,6 +28,7 @@ pub struct Build { pub profile: BuildProfile, pub mode: BuildMode, pub out_dir: PathBuf, + pub out_name: Option, pub bindgen: Option, pub cache: Cache, pub extra_options: Vec, @@ -159,6 +160,10 @@ pub struct BuildOptions { /// Sets the output directory with a relative path. pub out_dir: String, + #[structopt(long = "out-name")] + /// Sets the output file names. Defaults to package name. + pub out_name: Option, + #[structopt(last = true)] /// List of extra options to pass to `cargo build` pub extra_options: Vec, @@ -177,6 +182,7 @@ impl Default for BuildOptions { release: false, profiling: false, out_dir: String::new(), + out_name: None, extra_options: Vec::new(), } } @@ -188,7 +194,7 @@ impl Build { /// Construct a build command from the given options. pub fn try_from_opts(build_opts: BuildOptions) -> Result { let crate_path = set_crate_path(build_opts.path)?; - let crate_data = manifest::CrateData::new(&crate_path)?; + let crate_data = manifest::CrateData::new(&crate_path, build_opts.out_name.clone())?; let out_dir = crate_path.join(PathBuf::from(build_opts.out_dir)); let dev = build_opts.dev || build_opts.debug; @@ -210,6 +216,7 @@ impl Build { profile, mode: build_opts.mode, out_dir, + out_name: build_opts.out_name, bindgen: None, cache: cache::get_wasm_pack_cache()?, extra_options: build_opts.extra_options, @@ -388,6 +395,7 @@ impl Build { &self.crate_data, self.bindgen.as_ref().unwrap(), &self.out_dir, + &self.out_name, self.disable_dts, &self.target, self.profile, diff --git a/src/command/test.rs b/src/command/test.rs index 67436aa..78d8e78 100644 --- a/src/command/test.rs +++ b/src/command/test.rs @@ -120,7 +120,7 @@ impl Test { } = test_opts; let crate_path = set_crate_path(path)?; - let crate_data = manifest::CrateData::new(&crate_path)?; + let crate_data = manifest::CrateData::new(&crate_path, None)?; let any_browser = chrome || firefox || safari; if !node && !any_browser { diff --git a/src/manifest/mod.rs b/src/manifest/mod.rs index 617af86..3dfb612 100644 --- a/src/manifest/mod.rs +++ b/src/manifest/mod.rs @@ -27,6 +27,7 @@ pub struct CrateData { data: Metadata, current_idx: usize, manifest: CargoManifest, + out_name: Option, } #[doc(hidden)] @@ -214,7 +215,7 @@ pub struct ManifestAndUnsedKeys { impl CrateData { /// Reads all metadata for the crate whose manifest is inside the directory /// specified by `path`. - pub fn new(crate_path: &Path) -> Result { + pub fn new(crate_path: &Path, out_name: Option) -> Result { let manifest_path = crate_path.join("Cargo.toml"); if !manifest_path.is_file() { bail!( @@ -241,6 +242,7 @@ impl CrateData { data, manifest, current_idx, + out_name, }); fn error_chain_to_failure(err: cargo_metadata::Error) -> Error { @@ -346,6 +348,14 @@ impl CrateData { } } + /// Get the prefix for output file names + fn name_prefix(&self) -> String { + match &self.out_name { + Some(value) => value.clone(), + None => self.crate_name(), + } + } + /// Get the license for the crate at the given path. pub fn crate_license(&self) -> &Option { &self.manifest.package.license @@ -396,14 +406,14 @@ impl CrateData { disable_dts: bool, out_dir: &Path, ) -> NpmData { - let crate_name = self.crate_name(); - let wasm_file = format!("{}_bg.wasm", crate_name); - let js_file = format!("{}.js", crate_name); + let name_prefix = self.name_prefix(); + let wasm_file = format!("{}_bg.wasm", name_prefix); + let js_file = format!("{}.js", name_prefix); let mut files = vec![wasm_file]; files.push(js_file.clone()); if include_commonjs_shim { - let js_bg_file = format!("{}_bg.js", crate_name); + let js_bg_file = format!("{}_bg.js", name_prefix); files.push(js_bg_file.to_string()); } @@ -414,7 +424,7 @@ impl CrateData { }; let dts_file = if !disable_dts { - let file = format!("{}.d.ts", crate_name); + let file = format!("{}.d.ts", name_prefix); files.push(file.to_string()); Some(file) } else { diff --git a/tests/all/license.rs b/tests/all/license.rs index f56f650..cffa799 100644 --- a/tests/all/license.rs +++ b/tests/all/license.rs @@ -12,7 +12,7 @@ fn it_copies_a_license_default_path() { let fixture = fixture::single_license(); let out_dir = fixture.path.join("pkg"); fs::create_dir(&out_dir).expect("should create pkg directory OK"); - let crate_data = CrateData::new(&fixture.path); + let crate_data = CrateData::new(&fixture.path, None); assert!(license::copy_from_crate(&crate_data.unwrap(), &fixture.path, &out_dir).is_ok()); @@ -37,7 +37,7 @@ fn it_copies_a_license_provided_path() { let fixture = fixture::single_license(); let out_dir = fixture.path.join("pkg"); fs::create_dir(&out_dir).expect("should create pkg directory OK"); - let crate_data = CrateData::new(&fixture.path); + let crate_data = CrateData::new(&fixture.path, None); assert!(license::copy_from_crate(&crate_data.unwrap(), &fixture.path, &out_dir).is_ok()); let crate_license_path = fixture.path.join("LICENSE"); @@ -60,7 +60,7 @@ fn it_copies_all_licenses_default_path() { let fixture = fixture::dual_license(); let out_dir = fixture.path.join("pkg"); fs::create_dir(&out_dir).expect("should create pkg directory OK"); - let crate_data = CrateData::new(&fixture.path); + let crate_data = CrateData::new(&fixture.path, None); assert!(license::copy_from_crate(&crate_data.unwrap(), &fixture.path, &out_dir).is_ok()); @@ -95,7 +95,7 @@ fn it_copies_all_licenses_provided_path() { let fixture = fixture::dual_license(); let out_dir = fixture.path.join("pkg"); fs::create_dir(&out_dir).expect("should create pkg directory OK"); - let crate_data = CrateData::new(&fixture.path); + let crate_data = CrateData::new(&fixture.path, None); assert!(license::copy_from_crate(&crate_data.unwrap(), &fixture.path, &out_dir).is_ok()); @@ -131,7 +131,7 @@ fn it_copies_a_non_standard_license_provided_path() { let fixture = fixture::non_standard_license(license_file); let out_dir = fixture.path.join("pkg"); fs::create_dir(&out_dir).expect("should create pkg directory OK"); - let crate_data = CrateData::new(&fixture.path); + let crate_data = CrateData::new(&fixture.path, None); assert!(license::copy_from_crate(&crate_data.unwrap(), &fixture.path, &out_dir).is_ok()); diff --git a/tests/all/lockfile.rs b/tests/all/lockfile.rs index 87e4434..1a42ce8 100644 --- a/tests/all/lockfile.rs +++ b/tests/all/lockfile.rs @@ -6,7 +6,7 @@ use wasm_pack::manifest::CrateData; fn it_gets_wasm_bindgen_version() { let fixture = fixture::js_hello_world(); fixture.cargo_check(); - let data = CrateData::new(&fixture.path).unwrap(); + let data = CrateData::new(&fixture.path, None).unwrap(); let lock = Lockfile::new(&data).unwrap(); assert_eq!(lock.wasm_bindgen_version(), Some("0.2.37"),); } @@ -15,7 +15,7 @@ fn it_gets_wasm_bindgen_version() { fn it_gets_wasm_bindgen_test_version() { let fixture = fixture::wbg_test_node(); fixture.cargo_check(); - let data = CrateData::new(&fixture.path).unwrap(); + let data = CrateData::new(&fixture.path, None).unwrap(); let lock = Lockfile::new(&data).unwrap(); assert_eq!(lock.wasm_bindgen_test_version(), Some("0.2.37"),); } @@ -60,7 +60,7 @@ fn it_gets_wasm_bindgen_version_in_crate_inside_workspace() { "#, ); fixture.cargo_check(); - let data = CrateData::new(&fixture.path.join("blah")).unwrap(); + let data = CrateData::new(&fixture.path.join("blah"), None).unwrap(); let lock = Lockfile::new(&data).unwrap(); assert_eq!(lock.wasm_bindgen_version(), Some("0.2.37"),); } @@ -128,7 +128,7 @@ fn it_gets_wasm_bindgen_version_from_dependencies() { "#, ); fixture.cargo_check(); - let data = CrateData::new(&fixture.path.join("parent")).unwrap(); + let data = CrateData::new(&fixture.path.join("parent"), None).unwrap(); let lock = Lockfile::new(&data).unwrap(); assert_eq!(lock.wasm_bindgen_version(), Some("0.2.37"),); } diff --git a/tests/all/manifest.rs b/tests/all/manifest.rs index a68cf27..31ef3ab 100644 --- a/tests/all/manifest.rs +++ b/tests/all/manifest.rs @@ -9,7 +9,7 @@ use wasm_pack::{self, license, manifest}; #[test] fn it_gets_the_crate_name_default_path() { let path = &PathBuf::from("."); - let crate_data = manifest::CrateData::new(&path).unwrap(); + let crate_data = manifest::CrateData::new(&path, None).unwrap(); let name = crate_data.crate_name(); assert_eq!(name, "wasm_pack"); } @@ -17,7 +17,7 @@ fn it_gets_the_crate_name_default_path() { #[test] fn it_gets_the_crate_name_provided_path() { let fixture = fixture::js_hello_world(); - let crate_data = manifest::CrateData::new(&fixture.path).unwrap(); + let crate_data = manifest::CrateData::new(&fixture.path, None).unwrap(); assert_eq!(crate_data.crate_name(), "js_hello_world"); } @@ -26,7 +26,7 @@ fn it_checks_has_cdylib_default_path() { let fixture = fixture::no_cdylib(); // Ensure that there is a `Cargo.lock`. fixture.cargo_check(); - let crate_data = manifest::CrateData::new(&fixture.path).unwrap(); + let crate_data = manifest::CrateData::new(&fixture.path, None).unwrap(); assert!(crate_data.check_crate_config().is_err()); } @@ -35,14 +35,14 @@ fn it_checks_has_cdylib_provided_path() { let fixture = fixture::js_hello_world(); // Ensure that there is a `Cargo.lock`. fixture.cargo_check(); - let crate_data = manifest::CrateData::new(&fixture.path).unwrap(); + let crate_data = manifest::CrateData::new(&fixture.path, None).unwrap(); crate_data.check_crate_config().unwrap(); } #[test] fn it_checks_has_cdylib_wrong_crate_type() { let fixture = fixture::bad_cargo_toml(); - let crate_data = manifest::CrateData::new(&fixture.path).unwrap(); + let crate_data = manifest::CrateData::new(&fixture.path, None).unwrap(); assert!(crate_data.check_crate_config().is_err()); } @@ -51,7 +51,7 @@ fn it_recognizes_a_map_during_depcheck() { let fixture = fixture::serde_feature(); // Ensure that there is a `Cargo.lock`. fixture.cargo_check(); - let crate_data = manifest::CrateData::new(&fixture.path).unwrap(); + let crate_data = manifest::CrateData::new(&fixture.path, None).unwrap(); crate_data.check_crate_config().unwrap(); } @@ -59,7 +59,7 @@ fn it_recognizes_a_map_during_depcheck() { fn it_creates_a_package_json_default_path() { let fixture = fixture::js_hello_world(); let out_dir = fixture.path.join("pkg"); - let crate_data = manifest::CrateData::new(&fixture.path).unwrap(); + let crate_data = manifest::CrateData::new(&fixture.path, None).unwrap(); wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap(); assert!(crate_data .write_package_json(&out_dir, &None, false, &Target::Bundler) @@ -94,7 +94,7 @@ fn it_creates_a_package_json_default_path() { fn it_creates_a_package_json_provided_path() { let fixture = fixture::js_hello_world(); let out_dir = fixture.path.join("pkg"); - let crate_data = manifest::CrateData::new(&fixture.path).unwrap(); + let crate_data = manifest::CrateData::new(&fixture.path, None).unwrap(); wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap(); assert!(crate_data .write_package_json(&out_dir, &None, false, &Target::Bundler) @@ -122,7 +122,7 @@ fn it_creates_a_package_json_provided_path() { fn it_creates_a_package_json_provided_path_with_scope() { let fixture = fixture::js_hello_world(); let out_dir = fixture.path.join("pkg"); - let crate_data = manifest::CrateData::new(&fixture.path).unwrap(); + let crate_data = manifest::CrateData::new(&fixture.path, None).unwrap(); wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap(); assert!(crate_data .write_package_json(&out_dir, &Some("test".to_string()), false, &Target::Bundler,) @@ -150,7 +150,7 @@ fn it_creates_a_package_json_provided_path_with_scope() { fn it_creates_a_pkg_json_with_correct_files_on_node() { let fixture = fixture::js_hello_world(); let out_dir = fixture.path.join("pkg"); - let crate_data = manifest::CrateData::new(&fixture.path).unwrap(); + let crate_data = manifest::CrateData::new(&fixture.path, None).unwrap(); wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap(); assert!(crate_data .write_package_json(&out_dir, &None, false, &Target::Nodejs) @@ -185,7 +185,7 @@ fn it_creates_a_pkg_json_with_correct_files_on_node() { fn it_creates_a_pkg_json_with_correct_files_on_nomodules() { let fixture = fixture::js_hello_world(); let out_dir = fixture.path.join("pkg"); - let crate_data = manifest::CrateData::new(&fixture.path).unwrap(); + let crate_data = manifest::CrateData::new(&fixture.path, None).unwrap(); wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap(); assert!(crate_data .write_package_json(&out_dir, &None, false, &Target::NoModules) @@ -219,7 +219,7 @@ fn it_creates_a_pkg_json_with_correct_files_on_nomodules() { fn it_creates_a_pkg_json_in_out_dir() { let fixture = fixture::js_hello_world(); let out_dir = fixture.path.join("./custom/out"); - let crate_data = manifest::CrateData::new(&fixture.path).unwrap(); + let crate_data = manifest::CrateData::new(&fixture.path, None).unwrap(); wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap(); assert!(crate_data .write_package_json(&out_dir, &None, false, &Target::Bundler) @@ -234,7 +234,7 @@ fn it_creates_a_pkg_json_in_out_dir() { fn it_creates_a_package_json_with_correct_keys_when_types_are_skipped() { let fixture = fixture::js_hello_world(); let out_dir = fixture.path.join("pkg"); - let crate_data = manifest::CrateData::new(&fixture.path).unwrap(); + let crate_data = manifest::CrateData::new(&fixture.path, None).unwrap(); wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap(); assert!(crate_data .write_package_json(&out_dir, &None, true, &Target::Bundler) @@ -262,7 +262,7 @@ fn it_creates_a_package_json_with_correct_keys_when_types_are_skipped() { #[test] fn it_errors_when_wasm_bindgen_is_not_declared() { let fixture = fixture::bad_cargo_toml(); - let crate_data = manifest::CrateData::new(&fixture.path).unwrap(); + let crate_data = manifest::CrateData::new(&fixture.path, None).unwrap(); assert!(crate_data.check_crate_config().is_err()); } @@ -294,7 +294,7 @@ fn it_sets_homepage_field_if_available_in_cargo_toml() { ); let out_dir = fixture.path.join("pkg"); - let crate_data = manifest::CrateData::new(&fixture.path).unwrap(); + let crate_data = manifest::CrateData::new(&fixture.path, None).unwrap(); wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap(); crate_data @@ -310,7 +310,7 @@ fn it_sets_homepage_field_if_available_in_cargo_toml() { // When 'homepage' is unavailable let fixture = fixture::js_hello_world(); let out_dir = fixture.path.join("pkg"); - let crate_data = manifest::CrateData::new(&fixture.path).unwrap(); + let crate_data = manifest::CrateData::new(&fixture.path, None).unwrap(); wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap(); crate_data @@ -326,7 +326,7 @@ fn it_does_not_error_when_wasm_bindgen_is_declared() { let fixture = fixture::js_hello_world(); // Ensure that there is a `Cargo.lock`. fixture.cargo_check(); - let crate_data = manifest::CrateData::new(&fixture.path).unwrap(); + let crate_data = manifest::CrateData::new(&fixture.path, None).unwrap(); crate_data.check_crate_config().unwrap(); } @@ -410,7 +410,7 @@ fn it_lists_license_files_in_files_field_of_package_json() { let fixture = fixture::dual_license(); let out_dir = fixture.path.join("pkg"); - let crate_data = manifest::CrateData::new(&fixture.path).unwrap(); + let crate_data = manifest::CrateData::new(&fixture.path, None).unwrap(); wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap(); license::copy_from_crate(&crate_data, &fixture.path, &out_dir).unwrap();