From ad1778031fc15e5947ab89efb6f363baa3d482ed Mon Sep 17 00:00:00 2001 From: Liam Murphy Date: Sun, 5 Feb 2023 19:07:32 +1100 Subject: [PATCH] Mark snippets and the bundler target's main file as having side effects This is a less extreme version of #1208, which only marks snippets and the main file on the bundler target as having side effects instead of all files. This means that the shim file which contains the vast majority of the JS code is still properly marked as having no side effects, allowing bundlers to get rid of things like unused `new TextEncoder` calls which could theoretically have side effects but don't. Fixes #972. --- src/manifest/mod.rs | 6 +++--- src/manifest/npm/esmodules.rs | 2 +- tests/all/manifest.rs | 7 +++++-- tests/all/utils/manifest.rs | 8 ++------ 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/manifest/mod.rs b/src/manifest/mod.rs index 790a6c9..54f7740 100644 --- a/src/manifest/mod.rs +++ b/src/manifest/mod.rs @@ -723,10 +723,10 @@ impl CrateData { url: repo_url, }), files: data.files, - module: data.main, + module: data.main.clone(), homepage: data.homepage, types: data.dts_file, - side_effects: false, + side_effects: vec![format!("./{}", data.main), "./snippets/*".to_owned()], keywords: data.keywords, dependencies, }) @@ -758,7 +758,7 @@ impl CrateData { module: data.main, homepage: data.homepage, types: data.dts_file, - side_effects: false, + side_effects: vec!["./snippets/*".to_owned()], keywords: data.keywords, dependencies, }) diff --git a/src/manifest/npm/esmodules.rs b/src/manifest/npm/esmodules.rs index 2501a6c..7d216bc 100644 --- a/src/manifest/npm/esmodules.rs +++ b/src/manifest/npm/esmodules.rs @@ -22,7 +22,7 @@ pub struct ESModulesPackage { #[serde(skip_serializing_if = "Option::is_none")] pub types: Option, #[serde(rename = "sideEffects")] - pub side_effects: bool, + pub side_effects: Vec, #[serde(skip_serializing_if = "Option::is_none")] pub keywords: Option>, #[serde(skip_serializing_if = "Option::is_none")] diff --git a/tests/all/manifest.rs b/tests/all/manifest.rs index 48d17df..abf0e75 100644 --- a/tests/all/manifest.rs +++ b/tests/all/manifest.rs @@ -93,7 +93,10 @@ fn it_creates_a_package_json_default_path() { ); assert_eq!(pkg.module, "js_hello_world.js"); assert_eq!(pkg.types, "js_hello_world.d.ts"); - assert_eq!(pkg.side_effects, false); + assert_eq!( + pkg.side_effects, + vec!["./js_hello_world.js", "./snippets/*"] + ); let actual_files: HashSet = pkg.files.into_iter().collect(); let expected_files: HashSet = [ @@ -255,7 +258,7 @@ fn it_creates_a_package_json_with_correct_files_when_out_name_is_provided() { ); assert_eq!(pkg.module, "index.js"); assert_eq!(pkg.types, "index.d.ts"); - assert_eq!(pkg.side_effects, false); + assert_eq!(pkg.side_effects, vec!["./index.js", "./snippets/*"]); 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 3a788de..9e34d72 100644 --- a/tests/all/utils/manifest.rs +++ b/tests/all/utils/manifest.rs @@ -21,8 +21,8 @@ pub struct NpmPackage { pub browser: String, #[serde(default = "default_none")] pub types: String, - #[serde(default = "default_false", rename = "sideEffects")] - pub side_effects: bool, + #[serde(default = "Vec::new", rename = "sideEffects")] + pub side_effects: Vec, pub homepage: Option, pub keywords: Option>, pub dependencies: Option>, @@ -32,10 +32,6 @@ fn default_none() -> String { "".to_string() } -fn default_false() -> bool { - false -} - #[derive(Deserialize)] pub struct Repository { #[serde(rename = "type")]