diff --git a/docs/src/cargo-toml-configuration.md b/docs/src/cargo-toml-configuration.md index 637680ef..c92c45d0 100644 --- a/docs/src/cargo-toml-configuration.md +++ b/docs/src/cargo-toml-configuration.md @@ -3,8 +3,8 @@ `wasm-pack` can be configured via the `package.metadata.wasm-pack` key in `Cargo.toml`. Every option has a default, and is not required. -There are three profiles: `dev`, `profiling`, and `release`. These correspond to -the `--dev`, `--profiling`, and `--release` flags passed to `wasm-pack build`. +There are three built-in profiles: `dev`, `profiling`, and `release`. These correspond to +the `--dev`, `--profiling`, and `--release` flags passed to `wasm-pack build`. `wasm_pack` can also be invoked with the `--profile [some custom profile]` option to use a custom profile name. This will also pass that profile to the cargo invocation. The available configuration options and their default values are shown below: @@ -44,6 +44,7 @@ debug-js-glue = false demangle-name-section = true dwarf-debug-info = false omit-default-module-path = false +split_linked_modules = false # `wasm-opt` is on by default in for the release profile, but it can be # disabled by setting it to `false` @@ -55,4 +56,16 @@ debug-js-glue = false demangle-name-section = true dwarf-debug-info = false omit-default-module-path = false +split_linked_modules = false + +# `custom_profile_name` can be anything and must match the value in the `--profile` option when invoking `wasm-pack`. +[package.metadata.wasm-pack.profile.custom_profile_name] +wasm-opt = ['-O'] + +[package.metadata.wasm-pack.profile.custom_profile_name.wasm-bindgen] +debug-js-glue = false +demangle-name-section = true +dwarf-debug-info = false +omit-default-module-path = false +split_linked_modules = false ``` diff --git a/src/manifest/mod.rs b/src/manifest/mod.rs index bf715a81..5348e5f4 100644 --- a/src/manifest/mod.rs +++ b/src/manifest/mod.rs @@ -60,53 +60,81 @@ struct CargoMetadata { wasm_pack: CargoWasmPack, } -#[derive(Default, Deserialize)] +#[derive(Deserialize)] struct CargoWasmPack { - #[serde(default)] - profile: CargoWasmPackProfiles, + #[serde(default, deserialize_with = "CargoWasmPack::deserialize_profiles")] + profile: HashMap, } -#[derive(Deserialize)] -struct CargoWasmPackProfiles { - #[serde( - default = "CargoWasmPackProfile::default_dev", - deserialize_with = "CargoWasmPackProfile::deserialize_dev" - )] - dev: CargoWasmPackProfile, - - #[serde( - default = "CargoWasmPackProfile::default_release", - deserialize_with = "CargoWasmPackProfile::deserialize_release" - )] - release: CargoWasmPackProfile, - - #[serde( - default = "CargoWasmPackProfile::default_profiling", - deserialize_with = "CargoWasmPackProfile::deserialize_profiling" - )] - profiling: CargoWasmPackProfile, - - #[serde( - default = "CargoWasmPackProfile::default_custom", - deserialize_with = "CargoWasmPackProfile::deserialize_custom" - )] - custom: CargoWasmPackProfile, +impl Default for CargoWasmPack { + fn default() -> Self { + let mut profile = HashMap::new(); + profile.insert("dev".to_owned(), CargoWasmPackProfile::default_dev()); + profile.insert( + "release".to_owned(), + CargoWasmPackProfile::default_release(), + ); + profile.insert( + "profiling".to_owned(), + CargoWasmPackProfile::default_profiling(), + ); + profile.insert("custom".to_owned(), CargoWasmPackProfile::default_custom()); + CargoWasmPack { profile } + } } -impl Default for CargoWasmPackProfiles { - fn default() -> CargoWasmPackProfiles { - CargoWasmPackProfiles { - dev: CargoWasmPackProfile::default_dev(), - release: CargoWasmPackProfile::default_release(), - profiling: CargoWasmPackProfile::default_profiling(), - custom: CargoWasmPackProfile::default_custom(), +impl CargoWasmPack { + fn deserialize_profiles<'de, D>( + deserializer: D, + ) -> Result, D::Error> + where + D: serde::Deserializer<'de>, + { + let mut profiles = >::deserialize(deserializer)?; + let found_deprecated_custom = profiles.contains_key("custom"); + if found_deprecated_custom { + PBAR.warn( + "The `custom` profile is deprecated. Please use the name of the current profile instead.", + ); + } + + profiles + .entry("dev".to_owned()) + .or_default() + .update_with_defaults(&CargoWasmPackProfile::default_dev()); + profiles + .entry("release".to_owned()) + .or_default() + .update_with_defaults(&CargoWasmPackProfile::default_release()); + profiles + .entry("profiling".to_owned()) + .or_default() + .update_with_defaults(&CargoWasmPackProfile::default_profiling()); + profiles + .entry("custom".to_owned()) + .or_default() + .update_with_defaults(&CargoWasmPackProfile::default_custom()); + + for (profile_name, profile) in profiles.iter_mut() { + if !matches!( + profile_name.as_str(), + "dev" | "release" | "profiling" | "custom" + ) { + if found_deprecated_custom { + PBAR.warn(&format!( + "Since the `{profile_name}` profile was explicitly defined, the values provided in the `custom` profile will not be used." + )); + } + profile.update_with_defaults(&CargoWasmPackProfile::default_custom()); + }; } + Ok(profiles) } } /// This is where configuration goes for wasm-bindgen, wasm-opt, wasm-snip, or /// anything else that wasm-pack runs. -#[derive(Default, Deserialize)] +#[derive(Clone, Default, Deserialize)] pub struct CargoWasmPackProfile { #[serde(default, rename = "wasm-bindgen")] wasm_bindgen: CargoWasmPackProfileWasmBindgen, @@ -114,7 +142,7 @@ pub struct CargoWasmPackProfile { wasm_opt: Option, } -#[derive(Default, Deserialize)] +#[derive(Clone, Default, Deserialize)] struct CargoWasmPackProfileWasmBindgen { #[serde(default, rename = "debug-js-glue")] debug_js_glue: Option, @@ -331,42 +359,6 @@ impl CargoWasmPackProfile { } } - fn deserialize_dev<'de, D>(deserializer: D) -> Result - where - D: serde::Deserializer<'de>, - { - let mut profile = >::deserialize(deserializer)?.unwrap_or_default(); - profile.update_with_defaults(&Self::default_dev()); - Ok(profile) - } - - fn deserialize_release<'de, D>(deserializer: D) -> Result - where - D: serde::Deserializer<'de>, - { - let mut profile = >::deserialize(deserializer)?.unwrap_or_default(); - profile.update_with_defaults(&Self::default_release()); - Ok(profile) - } - - fn deserialize_profiling<'de, D>(deserializer: D) -> Result - where - D: serde::Deserializer<'de>, - { - let mut profile = >::deserialize(deserializer)?.unwrap_or_default(); - profile.update_with_defaults(&Self::default_profiling()); - Ok(profile) - } - - fn deserialize_custom<'de, D>(deserializer: D) -> Result - where - D: serde::Deserializer<'de>, - { - let mut profile = >::deserialize(deserializer)?.unwrap_or_default(); - profile.update_with_defaults(&Self::default_custom()); - Ok(profile) - } - fn update_with_defaults(&mut self, defaults: &Self) { macro_rules! d { ( $( $path:ident ).* ) => { @@ -527,12 +519,27 @@ impl CrateData { /// Get the configured profile. pub fn configured_profile(&self, profile: BuildProfile) -> &CargoWasmPackProfile { - match profile { - BuildProfile::Dev => &self.manifest.package.metadata.wasm_pack.profile.dev, - BuildProfile::Profiling => &self.manifest.package.metadata.wasm_pack.profile.profiling, - BuildProfile::Release => &self.manifest.package.metadata.wasm_pack.profile.release, - BuildProfile::Custom(_) => &self.manifest.package.metadata.wasm_pack.profile.custom, - } + let profile_name = match profile { + BuildProfile::Dev => "dev", + BuildProfile::Profiling => "profiling", + BuildProfile::Release => "release", + BuildProfile::Custom(ref name) => name, + }; + self.manifest + .package + .metadata + .wasm_pack + .profile + .get(profile_name) + .or_else(|| { + self.manifest + .package + .metadata + .wasm_pack + .profile + .get("custom") + }) + .unwrap() } /// Check that the crate the given path is properly configured. diff --git a/tests/all/manifest.rs b/tests/all/manifest.rs index 8d4af2df..3553d5e9 100644 --- a/tests/all/manifest.rs +++ b/tests/all/manifest.rs @@ -552,9 +552,9 @@ fn parse_crate_data_returns_unused_keys_in_cargo_toml() { [dependencies] wasm-bindgen = "0.2" - # Note: production is not valid. - [package.metadata.wasm-pack.profile.production.wasm-bindgen] - debug-js-glue = true + # Note: underscore instead of hyphen is not valid. + [package.metadata.wasm-pack.profile.release.wasm-bindgen] + debug_js_glue = true "#, ) .hello_world_src_lib() @@ -565,7 +565,7 @@ fn parse_crate_data_returns_unused_keys_in_cargo_toml() { .assert() .success() .stderr(predicates::str::contains(format!( - "[WARN]: {} \"package.metadata.wasm-pack.profile.production\" is an unknown key and will \ + "[WARN]: {} \"package.metadata.wasm-pack.profile.release.wasm-bindgen.debug_js_glue\" is an unknown key and will \ be ignored. Please check your Cargo.toml.", emoji::WARN ))); diff --git a/tests/all/utils/fixture.rs b/tests/all/utils/fixture.rs index 7f089480..04e91771 100644 --- a/tests/all/utils/fixture.rs +++ b/tests/all/utils/fixture.rs @@ -163,13 +163,13 @@ impl Fixture { pub fn cargo_toml_with_custom_profile(&self, name: &str, profile_name: &str) -> &Self { self.file( "Cargo.toml", - &format!( + format!( r#" [package] authors = ["The wasm-pack developers"] description = "so awesome rust+wasm package" license = "WTFPL" - name = "{}" + name = "{name}" repository = "https://github.com/rustwasm/wasm-pack.git" version = "0.1.0" @@ -187,12 +187,14 @@ impl Fixture { [dev-dependencies] wasm-bindgen-test = "0.3" - [profile.{}] + [profile.{profile_name}] inherits = "release" opt-level = 'z' lto = true - "#, - name, profile_name + + [package.metadata.wasm-pack.profile.{profile_name}] + wasm-opt = ["-Oz"] + "# ), ) }