Merge pull request #599 from ibaryshnikov/master

Added support for --out-name flag
master
ashley williams 6 years ago committed by GitHub
commit 8f952257ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      docs/src/commands/build.md
  2. 5
      src/bindgen.rs
  3. 10
      src/command/build.rs
  4. 2
      src/command/test.rs
  5. 22
      src/manifest/mod.rs
  6. 10
      tests/all/license.rs
  7. 8
      tests/all/lockfile.rs
  8. 83
      tests/all/manifest.rs

@ -32,6 +32,23 @@ wasm-pack build --out-dir out
The above command will put your build artifacts in a directory called `out`, instead
of the default `pkg`.
## Generated file names
Flag `--out-name` sets the prefix for output file names. If not provided, package name is used instead.
Usage examples, assuming our crate is named `dom`:
```
wasm-pack build
# will produce files
# dom.d.ts dom.js dom_bg.d.ts dom_bg.wasm package.json README.md
wasm-pack build --out-name index
# will produce files
# index.d.ts index.js index_bg.d.ts index_bg.wasm package.json README.md
```
## Profile
The `build` command accepts an optional profile argument: one of `--dev`,

@ -173,6 +173,7 @@ pub fn wasm_bindgen_build(
data: &CrateData,
bindgen: &Download,
out_dir: &Path,
out_name: &Option<String>,
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");

@ -28,6 +28,7 @@ pub struct Build {
pub profile: BuildProfile,
pub mode: BuildMode,
pub out_dir: PathBuf,
pub out_name: Option<String>,
pub bindgen: Option<Download>,
pub cache: Cache,
pub extra_options: Vec<String>,
@ -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<String>,
#[structopt(last = true)]
/// List of extra options to pass to `cargo build`
pub extra_options: Vec<String>,
@ -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<Self, Error> {
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,
@ -377,6 +384,7 @@ impl Build {
&self.crate_data,
self.bindgen.as_ref().unwrap(),
&self.out_dir,
&self.out_name,
self.disable_dts,
&self.target,
self.profile,

@ -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 {

@ -27,6 +27,7 @@ pub struct CrateData {
data: Metadata,
current_idx: usize,
manifest: CargoManifest,
out_name: Option<String>,
}
#[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<CrateData, Error> {
pub fn new(crate_path: &Path, out_name: Option<String>) -> Result<CrateData, Error> {
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
pub 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<String> {
&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 {

@ -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());

@ -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"),);
}

@ -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,16 +17,32 @@ 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");
}
#[test]
fn it_gets_the_default_name_prefix() {
let path = &PathBuf::from(".");
let crate_data = manifest::CrateData::new(&path, None).unwrap();
let name = crate_data.name_prefix();
assert_eq!(name, "wasm_pack");
}
#[test]
fn it_gets_the_name_prefix_passed_from_cli() {
let path = &PathBuf::from(".");
let crate_data = manifest::CrateData::new(&path, Some("index".to_owned())).unwrap();
let name = crate_data.name_prefix();
assert_eq!(name, "index");
}
#[test]
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 +51,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 +67,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 +75,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 +110,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 +138,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 +166,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 +201,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)
@ -215,11 +231,42 @@ fn it_creates_a_pkg_json_with_correct_files_on_nomodules() {
assert_eq!(actual_files, expected_files);
}
#[test]
fn it_creates_a_package_json_with_correct_files_when_out_name_is_provided() {
let fixture = fixture::js_hello_world();
let out_dir = fixture.path.join("pkg");
let crate_data = manifest::CrateData::new(&fixture.path, Some("index".to_owned())).unwrap();
wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap();
assert!(crate_data
.write_package_json(&out_dir, &None, false, &Target::Bundler)
.is_ok());
let package_json_path = &fixture.path.join("pkg").join("package.json");
fs::metadata(package_json_path).unwrap();
utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
assert_eq!(pkg.name, "js-hello-world");
assert_eq!(pkg.repository.ty, "git");
assert_eq!(
pkg.repository.url,
"https://github.com/rustwasm/wasm-pack.git"
);
assert_eq!(pkg.module, "index.js");
assert_eq!(pkg.types, "index.d.ts");
assert_eq!(pkg.side_effects, "false");
let actual_files: HashSet<String> = pkg.files.into_iter().collect();
let expected_files: HashSet<String> = ["index_bg.wasm", "index.d.ts", "index.js"]
.iter()
.map(|&s| String::from(s))
.collect();
assert_eq!(actual_files, expected_files);
}
#[test]
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 +281,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 +309,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 +341,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 +357,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 +373,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 +457,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();

Loading…
Cancel
Save