Skip to content

Commit

Permalink
Merge branch 'master' into find_correct_pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
Pauan authored Jun 4, 2020
2 parents 6c0c144 + 2d0af80 commit 2dd1413
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 19 deletions.
22 changes: 17 additions & 5 deletions src/manifest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,8 @@ struct NpmData {
files: Vec<String>,
dts_file: Option<String>,
main: String,
homepage: Option<String>, // https://docs.npmjs.com/files/package.json#homepage
homepage: Option<String>, // https://docs.npmjs.com/files/package.json#homepage,
keywords: Option<Vec<String>>, // https://docs.npmjs.com/files/package.json#keywords
}

#[doc(hidden)]
Expand Down Expand Up @@ -590,7 +591,7 @@ impl CrateData {
fn npm_data(
&self,
scope: &Option<String>,
include_commonjs_shim: bool,
add_js_bg_to_package_json: bool,
disable_dts: bool,
out_dir: &Path,
) -> NpmData {
Expand All @@ -600,7 +601,7 @@ impl CrateData {
let mut files = vec![wasm_file];

files.push(js_file.clone());
if include_commonjs_shim {
if add_js_bg_to_package_json {
let js_bg_file = format!("{}_bg.js", name_prefix);
files.push(js_bg_file);
}
Expand All @@ -619,6 +620,12 @@ impl CrateData {
None
};

let keywords = if pkg.keywords.len() > 0 {
Some(pkg.keywords.clone())
} else {
None
};

if let Ok(entries) = fs::read_dir(out_dir) {
let file_names = entries
.filter_map(|e| e.ok())
Expand All @@ -637,6 +644,7 @@ impl CrateData {
files,
main: js_file,
homepage: self.manifest.package.homepage.clone(),
keywords: keywords,
}
}

Expand All @@ -650,7 +658,7 @@ impl CrateData {
}

fn to_commonjs(&self, scope: &Option<String>, disable_dts: bool, out_dir: &Path) -> NpmPackage {
let data = self.npm_data(scope, true, disable_dts, out_dir);
let data = self.npm_data(scope, false, disable_dts, out_dir);
let pkg = &self.data.packages[self.current_idx];

self.check_optional_fields();
Expand All @@ -674,6 +682,7 @@ impl CrateData {
main: data.main,
homepage: data.homepage,
types: data.dts_file,
keywords: data.keywords,
})
}

Expand All @@ -683,7 +692,7 @@ impl CrateData {
disable_dts: bool,
out_dir: &Path,
) -> NpmPackage {
let data = self.npm_data(scope, false, disable_dts, out_dir);
let data = self.npm_data(scope, true, disable_dts, out_dir);
let pkg = &self.data.packages[self.current_idx];

self.check_optional_fields();
Expand All @@ -708,6 +717,7 @@ impl CrateData {
homepage: data.homepage,
types: data.dts_file,
side_effects: false,
keywords: data.keywords,
})
}

Expand Down Expand Up @@ -737,6 +747,7 @@ impl CrateData {
homepage: data.homepage,
types: data.dts_file,
side_effects: false,
keywords: data.keywords,
})
}

Expand Down Expand Up @@ -770,6 +781,7 @@ impl CrateData {
browser: data.main,
homepage: data.homepage,
types: data.dts_file,
keywords: data.keywords,
})
}

Expand Down
2 changes: 2 additions & 0 deletions src/manifest/npm/commonjs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ pub struct CommonJSPackage {
pub homepage: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub types: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub keywords: Option<Vec<String>>,
}
2 changes: 2 additions & 0 deletions src/manifest/npm/esmodules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ pub struct ESModulesPackage {
pub types: Option<String>,
#[serde(rename = "sideEffects")]
pub side_effects: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub keywords: Option<Vec<String>>,
}
2 changes: 2 additions & 0 deletions src/manifest/npm/nomodules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ pub struct NoModulesPackage {
pub homepage: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub types: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub keywords: Option<Vec<String>>,
}
2 changes: 1 addition & 1 deletion tests/all/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ fn build_with_and_without_wasm_bindgen_debug() {
.assert()
.success();

let contents = fs::read_to_string(fixture.path.join("pkg/whatever.js")).unwrap();
let contents = fs::read_to_string(fixture.path.join("pkg/whatever_bg.js")).unwrap();
let contains_move_assertions =
contents.contains("throw new Error('Attempt to use a moved value')");
assert_eq!(
Expand Down
90 changes: 77 additions & 13 deletions tests/all/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ fn it_creates_a_package_json_default_path() {

let actual_files: HashSet<String> = pkg.files.into_iter().collect();
let expected_files: HashSet<String> = [
"js_hello_world_bg.wasm",
"js_hello_world.d.ts",
"js_hello_world_bg.js",
"js_hello_world_bg.wasm",
"js_hello_world.js",
]
.iter()
Expand All @@ -125,8 +126,9 @@ fn it_creates_a_package_json_provided_path() {

let actual_files: HashSet<String> = pkg.files.into_iter().collect();
let expected_files: HashSet<String> = [
"js_hello_world_bg.wasm",
"js_hello_world.d.ts",
"js_hello_world_bg.js",
"js_hello_world_bg.wasm",
"js_hello_world.js",
]
.iter()
Expand All @@ -153,8 +155,9 @@ fn it_creates_a_package_json_provided_path_with_scope() {

let actual_files: HashSet<String> = pkg.files.into_iter().collect();
let expected_files: HashSet<String> = [
"js_hello_world_bg.wasm",
"js_hello_world.d.ts",
"js_hello_world_bg.js",
"js_hello_world_bg.wasm",
"js_hello_world.js",
]
.iter()
Expand Down Expand Up @@ -188,7 +191,6 @@ fn it_creates_a_pkg_json_with_correct_files_on_node() {
let actual_files: HashSet<String> = pkg.files.into_iter().collect();
let expected_files: HashSet<String> = [
"js_hello_world_bg.wasm",
"js_hello_world_bg.js",
"js_hello_world.d.ts",
"js_hello_world.js",
]
Expand Down Expand Up @@ -222,9 +224,9 @@ fn it_creates_a_pkg_json_with_correct_files_on_nomodules() {

let actual_files: HashSet<String> = pkg.files.into_iter().collect();
let expected_files: HashSet<String> = [
"js_hello_world.d.ts",
"js_hello_world_bg.wasm",
"js_hello_world.js",
"js_hello_world.d.ts",
]
.iter()
.map(|&s| String::from(s))
Expand Down Expand Up @@ -256,10 +258,11 @@ fn it_creates_a_package_json_with_correct_files_when_out_name_is_provided() {
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();
let expected_files: HashSet<String> =
["index_bg.wasm", "index_bg.js", "index.d.ts", "index.js"]
.iter()
.map(|&s| String::from(s))
.collect();
assert_eq!(actual_files, expected_files);
}

Expand Down Expand Up @@ -300,10 +303,14 @@ fn it_creates_a_package_json_with_correct_keys_when_types_are_skipped() {
assert_eq!(pkg.module, "js_hello_world.js");

let actual_files: HashSet<String> = pkg.files.into_iter().collect();
let expected_files: HashSet<String> = ["js_hello_world_bg.wasm", "js_hello_world.js"]
.iter()
.map(|&s| String::from(s))
.collect();
let expected_files: HashSet<String> = [
"js_hello_world_bg.wasm",
"js_hello_world_bg.js",
"js_hello_world.js",
]
.iter()
.map(|&s| String::from(s))
.collect();
assert_eq!(actual_files, expected_files);
}

Expand Down Expand Up @@ -369,6 +376,63 @@ fn it_sets_homepage_field_if_available_in_cargo_toml() {
assert_eq!(pkg.homepage, None);
}

#[test]
fn it_sets_keywords_field_if_available_in_cargo_toml() {
// When 'homepage' is available
let fixture = utils::fixture::Fixture::new();
fixture.hello_world_src_lib().file(
"Cargo.toml",
r#"
[package]
authors = ["The wasm-pack developers"]
description = "so awesome rust+wasm package"
license = "WTFPL"
name = "homepage-field-test"
repository = "https://github.com/rustwasm/wasm-pack.git"
version = "0.1.0"
keywords = ["wasm"]
[lib]
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "=0.2"
[dev-dependencies]
wasm-bindgen-test = "=0.2"
"#,
);

let out_dir = fixture.path.join("pkg");
let crate_data = manifest::CrateData::new(&fixture.path, None).unwrap();

wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap();
crate_data
.write_package_json(&out_dir, &None, true, Target::Bundler)
.unwrap();

let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
let keywords = pkg.keywords.clone().unwrap();
assert!(
keywords.contains(&"wasm".to_string()),
"keywords is not in files: {:?}",
keywords,
);

// When 'keywords' is unavailable
let fixture = fixture::js_hello_world();
let out_dir = fixture.path.join("pkg");
let crate_data = manifest::CrateData::new(&fixture.path, None).unwrap();

wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap();
crate_data
.write_package_json(&out_dir, &None, true, Target::Bundler)
.unwrap();

let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
assert_eq!(pkg.keywords, None);
}

#[test]
fn it_does_not_error_when_wasm_bindgen_is_declared() {
let fixture = fixture::js_hello_world();
Expand Down
1 change: 1 addition & 0 deletions tests/all/utils/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub struct NpmPackage {
#[serde(default = "default_false", rename = "sideEffects")]
pub side_effects: bool,
pub homepage: Option<String>,
pub keywords: Option<Vec<String>>,
}

fn default_none() -> String {
Expand Down

0 comments on commit 2dd1413

Please sign in to comment.