Skip to content

Commit

Permalink
Merge pull request #599 from ibaryshnikov/master
Browse files Browse the repository at this point in the history
Added support for --out-name flag
  • Loading branch information
ashleygwilliams committed Mar 30, 2019
2 parents 2947414 + 92b80f6 commit 8f95225
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 35 deletions.
17 changes: 17 additions & 0 deletions docs/src/commands/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
5 changes: 5 additions & 0 deletions src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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");
Expand Down
10 changes: 9 additions & 1 deletion src/command/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down Expand Up @@ -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>,
Expand All @@ -177,6 +182,7 @@ impl Default for BuildOptions {
release: false,
profiling: false,
out_dir: String::new(),
out_name: None,
extra_options: Vec::new(),
}
}
Expand All @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/command/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 16 additions & 6 deletions src/manifest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub struct CrateData {
data: Metadata,
current_idx: usize,
manifest: CargoManifest,
out_name: Option<String>,
}

#[doc(hidden)]
Expand Down Expand Up @@ -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!(
Expand All @@ -241,6 +242,7 @@ impl CrateData {
data,
manifest,
current_idx,
out_name,
});

fn error_chain_to_failure(err: cargo_metadata::Error) -> Error {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());
}

Expand All @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions tests/all/license.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

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

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

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

Expand Down
8 changes: 4 additions & 4 deletions tests/all/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),);
}
Expand All @@ -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"),);
}
Expand Down Expand Up @@ -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"),);
}
Expand Down Expand Up @@ -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"),);
}
Loading

0 comments on commit 8f95225

Please sign in to comment.