Skip to content
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

fix: declare ES module in package.json #1061

Merged
merged 6 commits into from Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/manifest/mod.rs
Expand Up @@ -722,6 +722,7 @@ impl CrateData {

NpmPackage::ESModulesPackage(ESModulesPackage {
name: data.name,
ty: "module".into(),
collaborators: pkg.authors.clone(),
description: self.pkg().description.clone(),
version: pkg.version.to_string(),
Expand All @@ -731,7 +732,7 @@ impl CrateData {
url: repo_url,
}),
files: data.files,
module: data.main.clone(),
main: data.main.clone(),
homepage: data.homepage,
types: data.dts_file,
side_effects: vec![format!("./{}", data.main), "./snippets/*".to_owned()],
Expand All @@ -754,6 +755,7 @@ impl CrateData {

NpmPackage::ESModulesPackage(ESModulesPackage {
name: data.name,
ty: "module".into(),
collaborators: pkg.authors.clone(),
description: self.pkg().description.clone(),
version: pkg.version.to_string(),
Expand All @@ -763,7 +765,7 @@ impl CrateData {
url: repo_url,
}),
files: data.files,
module: data.main,
main: data.main,
homepage: data.homepage,
types: data.dts_file,
side_effects: vec!["./snippets/*".to_owned()],
Expand Down
4 changes: 3 additions & 1 deletion src/manifest/npm/esmodules.rs
Expand Up @@ -5,6 +5,8 @@ use crate::manifest::npm::repository::Repository;
#[derive(Serialize)]
pub struct ESModulesPackage {
pub name: String,
#[serde(rename = "type")]
pub ty: String,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub collaborators: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand All @@ -16,7 +18,7 @@ pub struct ESModulesPackage {
pub repository: Option<Repository>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub files: Vec<String>,
pub module: String,
pub main: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub homepage: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down
17 changes: 11 additions & 6 deletions tests/all/manifest.rs
Expand Up @@ -86,12 +86,13 @@ fn it_creates_a_package_json_default_path() {
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.ty, "module");
assert_eq!(pkg.repository.ty, "git");
assert_eq!(
pkg.repository.url,
"https://github.com/rustwasm/wasm-pack.git"
);
assert_eq!(pkg.module, "js_hello_world.js");
assert_eq!(pkg.main, "js_hello_world.js");
assert_eq!(pkg.types, "js_hello_world.d.ts");
assert_eq!(
pkg.side_effects,
Expand Down Expand Up @@ -125,7 +126,8 @@ fn it_creates_a_package_json_provided_path() {
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.module, "js_hello_world.js");
assert_eq!(pkg.ty, "module");
assert_eq!(pkg.main, "js_hello_world.js");

let actual_files: HashSet<String> = pkg.files.into_iter().collect();
let expected_files: HashSet<String> = [
Expand Down Expand Up @@ -154,7 +156,8 @@ fn it_creates_a_package_json_provided_path_with_scope() {
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, "@test/js-hello-world");
assert_eq!(pkg.module, "js_hello_world.js");
assert_eq!(pkg.ty, "module");
assert_eq!(pkg.main, "js_hello_world.js");

let actual_files: HashSet<String> = pkg.files.into_iter().collect();
let expected_files: HashSet<String> = [
Expand Down Expand Up @@ -251,12 +254,13 @@ fn it_creates_a_package_json_with_correct_files_when_out_name_is_provided() {
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.ty, "module");
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.main, "index.js");
assert_eq!(pkg.types, "index.d.ts");
assert_eq!(pkg.side_effects, vec!["./index.js", "./snippets/*"]);

Expand Down Expand Up @@ -298,12 +302,13 @@ fn it_creates_a_package_json_with_correct_keys_when_types_are_skipped() {
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.ty, "module");
assert_eq!(pkg.repository.ty, "git");
assert_eq!(
pkg.repository.url,
"https://github.com/rustwasm/wasm-pack.git"
);
assert_eq!(pkg.module, "js_hello_world.js");
assert_eq!(pkg.main, "js_hello_world.js");

let actual_files: HashSet<String> = pkg.files.into_iter().collect();
let expected_files: HashSet<String> = [
Expand Down Expand Up @@ -344,7 +349,7 @@ fn it_creates_a_package_json_with_npm_dependencies_provided_by_wasm_bindgen() {
pkg.repository.url,
"https://github.com/rustwasm/wasm-pack.git"
);
assert_eq!(pkg.module, "js_hello_world.js");
assert_eq!(pkg.main, "js_hello_world.js");

let actual_files: HashSet<String> = pkg.files.into_iter().collect();
let expected_files: HashSet<String> = [
Expand Down
2 changes: 2 additions & 0 deletions tests/all/utils/manifest.rs
Expand Up @@ -8,6 +8,8 @@ use serde_json;
#[derive(Deserialize)]
pub struct NpmPackage {
pub name: String,
#[serde(default = "default_none", rename = "type")]
pub ty: String,
pub description: String,
pub version: String,
pub license: String,
Expand Down