Repository: rustwasm/wasm-pack (rust)

Issue:
Add type = "module" in package.json to support nodejs in bundler mode
## 💡 Feature description

When you build using the default "bundler" mode, it can *almost* run directly from nodejs, using nodejs's built in ESM support.

Unfortunately, the created package.json file is missing `"type": "module"`. Adding this to the created package.json not only fixes some bundler issues, but also makes nodejs understand the created package too:

foo.js:

```javascript
import {greet} from 'my_module'
greet()
```

```
$ node --experimental-wasm-modules foo.js
(node:414396) ExperimentalWarning: Importing Web Assembly modules is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
Hello world!
```

Pull request description:
fix: declare ES module in package.json
In bundler mode, generate `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.js (when run with `--experimental-vm-modules --experimental-wasm-modules`), while it remains also palatable to webpack as illustrated in https://github.com/webpack/webpack/pull/14313 (where the `pkg` subfolder is generated with `wasm-pack` built with this change). This resolves the headache of using a wasm-pack-built package in a library that one needs to both run directly in Node and include in a webpack build.

Fixes #1039

- [x] You have the latest version of `rustfmt` installed
```bash
$ rustup component add rustfmt
```
- [x] You ran `cargo fmt` on the code base before submitting
- [x] You reference which issue is being closed in the PR text


Patch:
diff --git a/src/manifest/mod.rs b/src/manifest/mod.rs
index 5557553..e93b0d9 100644
--- a/src/manifest/mod.rs
+++ b/src/manifest/mod.rs
@@ -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(),
@@ -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()],
@@ -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(),
@@ -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()],
diff --git a/src/manifest/npm/esmodules.rs b/src/manifest/npm/esmodules.rs
index 7d216bc..0ee0af9 100644
--- a/src/manifest/npm/esmodules.rs
+++ b/src/manifest/npm/esmodules.rs
@@ -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")]
@@ -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")]


Test patch:
diff --git a/tests/all/manifest.rs b/tests/all/manifest.rs
index 27d2496..8d4af2d 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,
@@ -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> = [
@@ -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> = [
@@ -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/*"]);
 
@@ -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> = [
@@ -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> = [
diff --git a/tests/all/utils/manifest.rs b/tests/all/utils/manifest.rs
index 9e34d72..237ade4 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,