Skip to content

Use the same profile name for both Cargo and wasm-pack #1489

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions docs/src/cargo-toml-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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`
Expand All @@ -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
```
165 changes: 86 additions & 79 deletions src/manifest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,61 +60,89 @@ 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<String, CargoWasmPackProfile>,
}

#[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<HashMap<String, CargoWasmPackProfile>, D::Error>
where
D: serde::Deserializer<'de>,
{
let mut profiles = <HashMap<String, CargoWasmPackProfile>>::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,
#[serde(default, rename = "wasm-opt")]
wasm_opt: Option<CargoWasmPackProfileWasmOpt>,
}

#[derive(Default, Deserialize)]
#[derive(Clone, Default, Deserialize)]
struct CargoWasmPackProfileWasmBindgen {
#[serde(default, rename = "debug-js-glue")]
debug_js_glue: Option<bool>,
Expand Down Expand Up @@ -331,42 +359,6 @@ impl CargoWasmPackProfile {
}
}

fn deserialize_dev<'de, D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let mut profile = <Option<Self>>::deserialize(deserializer)?.unwrap_or_default();
profile.update_with_defaults(&Self::default_dev());
Ok(profile)
}

fn deserialize_release<'de, D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let mut profile = <Option<Self>>::deserialize(deserializer)?.unwrap_or_default();
profile.update_with_defaults(&Self::default_release());
Ok(profile)
}

fn deserialize_profiling<'de, D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let mut profile = <Option<Self>>::deserialize(deserializer)?.unwrap_or_default();
profile.update_with_defaults(&Self::default_profiling());
Ok(profile)
}

fn deserialize_custom<'de, D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let mut profile = <Option<Self>>::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 ).* ) => {
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions tests/all/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
)));
Expand Down
12 changes: 7 additions & 5 deletions tests/all/utils/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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"]
"#
),
)
}
Expand Down