From c5298f10a2b2afd744aacf4437b9c85f273e1a1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gunnlaugur=20=C3=9E=C3=B3r=20Briem?= Date: Thu, 23 Sep 2021 18:51:27 +0000 Subject: [PATCH 1/4] fix: declare ES module in package.json In bundler mode, generate the package.json with "type": "module" and use the "main" attribute instead of the "module" attribute. This change makes the built ES module palatable to node in --experimental-wasm-modules mode as well as to webpack as illustrated in https://github.com/webpack/webpack/pull/14313 --- src/manifest/mod.rs | 6 ++++-- src/manifest/npm/esmodules.rs | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/manifest/mod.rs b/src/manifest/mod.rs index 898de421..8af8b94b 100644 --- a/src/manifest/mod.rs +++ b/src/manifest/mod.rs @@ -699,6 +699,7 @@ impl CrateData { NpmPackage::ESModulesPackage(ESModulesPackage { name: data.name, + type_: "module".into(), collaborators: pkg.authors.clone(), description: self.manifest.package.description.clone(), version: pkg.version.to_string(), @@ -713,7 +714,7 @@ impl CrateData { url: repo_url, }), files: data.files, - module: data.main, + main: data.main, homepage: data.homepage, types: data.dts_file, side_effects: false, @@ -729,6 +730,7 @@ impl CrateData { NpmPackage::ESModulesPackage(ESModulesPackage { name: data.name, + type_: "module".into(), collaborators: pkg.authors.clone(), description: self.manifest.package.description.clone(), version: pkg.version.to_string(), @@ -743,7 +745,7 @@ impl CrateData { url: repo_url, }), files: data.files, - module: data.main, + main: data.main, homepage: data.homepage, types: data.dts_file, side_effects: false, diff --git a/src/manifest/npm/esmodules.rs b/src/manifest/npm/esmodules.rs index 640a9269..2aa69c87 100644 --- a/src/manifest/npm/esmodules.rs +++ b/src/manifest/npm/esmodules.rs @@ -3,6 +3,8 @@ use manifest::npm::repository::Repository; #[derive(Serialize)] pub struct ESModulesPackage { pub name: String, + #[serde(rename = "type")] + pub type_: String, #[serde(skip_serializing_if = "Vec::is_empty")] pub collaborators: Vec, #[serde(skip_serializing_if = "Option::is_none")] @@ -14,7 +16,7 @@ pub struct ESModulesPackage { pub repository: Option, #[serde(skip_serializing_if = "Vec::is_empty")] pub files: Vec, - pub module: String, + pub main: String, #[serde(skip_serializing_if = "Option::is_none")] pub homepage: Option, #[serde(skip_serializing_if = "Option::is_none")] From 384238d554f9b525f578ce867b86af00c01e19b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gunnlaugur=20=C3=9E=C3=B3r=20Briem?= Date: Fri, 24 Sep 2021 11:37:05 +0000 Subject: [PATCH 2/4] test: update tests to conform to this change --- tests/all/manifest.rs | 15 ++++++++++----- tests/all/utils/manifest.rs | 2 ++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/tests/all/manifest.rs b/tests/all/manifest.rs index c25c3e50..f7cf57af 100644 --- a/tests/all/manifest.rs +++ b/tests/all/manifest.rs @@ -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, false); @@ -122,7 +123,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 = pkg.files.into_iter().collect(); let expected_files: HashSet = [ @@ -151,7 +153,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 = pkg.files.into_iter().collect(); let expected_files: HashSet = [ @@ -248,12 +251,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, false); @@ -295,12 +299,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 = pkg.files.into_iter().collect(); let expected_files: HashSet = [ diff --git a/tests/all/utils/manifest.rs b/tests/all/utils/manifest.rs index c261bc17..da5d2eaa 100644 --- a/tests/all/utils/manifest.rs +++ b/tests/all/utils/manifest.rs @@ -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, From 70be9d3ddebf8adc17f05d907bac9a7b9f1ba4d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gunnlaugur=20=C3=9E=C3=B3r=20Briem?= Date: Fri, 24 Sep 2021 12:54:02 +0000 Subject: [PATCH 3/4] refactor: rename type_ to ty in ESModulesPackage I noticed that this specific evasion of the "type" keyword is already used in utils::manifest::NpmPackage in the tests, so just match that. --- src/manifest/mod.rs | 4 ++-- src/manifest/npm/esmodules.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/manifest/mod.rs b/src/manifest/mod.rs index 8af8b94b..3bd9249f 100644 --- a/src/manifest/mod.rs +++ b/src/manifest/mod.rs @@ -699,7 +699,7 @@ impl CrateData { NpmPackage::ESModulesPackage(ESModulesPackage { name: data.name, - type_: "module".into(), + ty: "module".into(), collaborators: pkg.authors.clone(), description: self.manifest.package.description.clone(), version: pkg.version.to_string(), @@ -730,7 +730,7 @@ impl CrateData { NpmPackage::ESModulesPackage(ESModulesPackage { name: data.name, - type_: "module".into(), + ty: "module".into(), collaborators: pkg.authors.clone(), description: self.manifest.package.description.clone(), version: pkg.version.to_string(), diff --git a/src/manifest/npm/esmodules.rs b/src/manifest/npm/esmodules.rs index 2aa69c87..68d2174a 100644 --- a/src/manifest/npm/esmodules.rs +++ b/src/manifest/npm/esmodules.rs @@ -4,7 +4,7 @@ use manifest::npm::repository::Repository; pub struct ESModulesPackage { pub name: String, #[serde(rename = "type")] - pub type_: String, + pub ty: String, #[serde(skip_serializing_if = "Vec::is_empty")] pub collaborators: Vec, #[serde(skip_serializing_if = "Option::is_none")] From 4ca0f02daeba73e2415a5528b42193f87b7ed8ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gunnlaugur=20=C3=9E=C3=B3r=20Briem?= Date: Fri, 8 Oct 2021 13:32:03 +0000 Subject: [PATCH 4/4] test: update new test to expect main instead of module Update test case that landed after this PR was created, in https://github.com/rustwasm/wasm-pack/pull/1061, to expect main attribute in package.json instead of module attribute. --- tests/all/manifest.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/all/manifest.rs b/tests/all/manifest.rs index 78fdc6a5..671cbb34 100644 --- a/tests/all/manifest.rs +++ b/tests/all/manifest.rs @@ -346,7 +346,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 = pkg.files.into_iter().collect(); let expected_files: HashSet = [