Skip to content

Commit

Permalink
feat: Initial support for cargo profiles
Browse files Browse the repository at this point in the history
This isn't the complete "trunk profile" implementation, but maybe a
short term solution in the right direction.

This allows setting a cargo profile on multiple levels. The idea is to
allow influencing the cargo profile only. The trunk release flag still
works as before. However you can now choose a `--cargo-profile` from
the command line, from the Trunk.toml, from the `index.html` data
attributes. In that order of preference.
  • Loading branch information
ctron committed Sep 16, 2024
1 parent 2352cde commit b585670
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 7 deletions.
4 changes: 4 additions & 0 deletions Trunk.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ trunk-version = "*"
target = "index.html"
# Build in release mode.
release = false
# Use a custom cargo profile
# cargo_profile = ""
# The output dir for all final assets.
dist = "dist"
# The public URL from which assets are to be served.
Expand All @@ -26,6 +28,8 @@ locked = false
minify = "never" # can be one of: never, on_release, always
# Allow disabling sub-resource integrity (SRI)
no_sri = false
# An optional cargo profile to use
# cargo_profile = "release.trunk"

[watch]
# Paths to watch. The `build.target`'s parent folder is watched by default.
Expand Down
3 changes: 3 additions & 0 deletions guide/src/assets/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ This will typically look like: `<link data-trunk rel="{type}" href="{path}" ..ot
- `data-wasm-import-name`: (optional) the name of the global variable where the functions imported from WASM will be available (under the `window` object). Defaults to `wasmBindings` (which makes them available via `window.wasmBindings.<functionName>`).
- `data-target-path`: (optional) Path where the output is placed inside the dist dir. If not present, the directory is placed in the dist root. The path must be a relative path without `..`.
- `data-initializer`: (optional) Path to the (module) JavaScript file of the [initializer](../advanced/initializer.md).
- `data-cargo-profile`: (optional) A cargo profile to use, instead of the default, for both release or dev mode.
- `data-cargo-profile-release`: (optional) A cargo profile to use, instead of the default, for the release mode. Overrides the `data-cargo-profile` setting.
- `data-cargo-profile-dev`: (optional) A cargo profile to use, instead of the default, for the dev mode. Overrides the `data-cargo-profile` setting.

### sass/scss

Expand Down
9 changes: 9 additions & 0 deletions schemas/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"accept_invalid_certs": false,
"all_features": false,
"allow_self_closing_script": false,
"cargo_profile": null,
"dist": "dist",
"filehash": true,
"frozen": false,
Expand Down Expand Up @@ -106,6 +107,14 @@
"default": false,
"type": "boolean"
},
"cargo_profile": {
"description": "Cargo profile to use. Conflicts with `release`.",
"default": null,
"type": [
"string",
"null"
]
},
"dist": {
"description": "The output dir for all final assets",
"default": "dist",
Expand Down
6 changes: 6 additions & 0 deletions src/cmd/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ pub struct Build {
#[arg(default_missing_value="true", num_args=0..=1)]
pub release: Option<bool>,

/// Cargo profile to use for building.
#[arg(long, env = "TRUNK_BUILD_CARGO_PROFILE")]
pub cargo_profile: Option<String>,

/// The output dir for all final assets
#[arg(short, long, env = "TRUNK_BUILD_DIST")]
pub dist: Option<PathBuf>,
Expand Down Expand Up @@ -122,6 +126,7 @@ impl Build {
core,
target,
release,
cargo_profile,
dist,
offline,
frozen,
Expand All @@ -142,6 +147,7 @@ impl Build {

config.build.target = target.unwrap_or(config.build.target);
config.build.release = release.unwrap_or(config.build.release);
config.build.cargo_profile = cargo_profile.or(config.build.cargo_profile);
config.build.dist = dist.unwrap_or(config.build.dist);
config.build.offline = offline.unwrap_or(config.build.offline);
config.build.frozen = frozen.unwrap_or(config.build.frozen);
Expand Down
5 changes: 5 additions & 0 deletions src/config/models/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ pub struct Build {
#[serde(default)]
pub release: bool,

/// Cargo profile to use. Conflicts with `release`.
#[serde(default)]
pub cargo_profile: Option<String>,

/// The output dir for all final assets
#[serde(default = "default::dist")]
pub dist: PathBuf,
Expand Down Expand Up @@ -184,6 +188,7 @@ impl Default for Build {
Self {
target: default::target(),
release: false,
cargo_profile: None,
dist: default::dist(),
offline: false,
frozen: false,
Expand Down
4 changes: 4 additions & 0 deletions src/config/rt/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub struct RtcBuild {
pub target_parent: PathBuf,
/// Build in release mode.
pub release: bool,
/// Cargo profile to use instead of the default selection.
pub cargo_profile: Option<String>,
/// Build without network access
pub offline: bool,
/// Require Cargo.lock and cache are up to date
Expand Down Expand Up @@ -173,6 +175,7 @@ impl RtcBuild {
target,
target_parent,
release: build.release,
cargo_profile: build.cargo_profile,
public_url,
filehash: build.filehash,
staging_dist,
Expand Down Expand Up @@ -211,6 +214,7 @@ impl RtcBuild {
target,
target_parent,
release: false,
cargo_profile: None,
public_url: Default::default(),
filehash: true,
final_dist,
Expand Down
35 changes: 28 additions & 7 deletions src/pipelines/rust/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ pub struct RustApp {
cfg: Arc<RtcBuild>,
/// Skip building
skip_build: bool,
/// Cargo profile to use
cargo_profile: Option<String>,
/// The configuration of the features passed to cargo.
cargo_features: Features,
/// Is this module main or a worker?
Expand Down Expand Up @@ -186,10 +188,6 @@ impl RustApp {
let id = Some(id);
let name = bin.clone().unwrap_or_else(|| manifest.package.name.clone());

let data_features = attrs.get("data-cargo-features").map(|val| val.to_string());
let data_all_features = attrs.contains_key("data-cargo-all-features");
let data_no_default_features = attrs.contains_key("data-cargo-no-default-features");

let loader_shim = attrs.contains_key("data-loader-shim");
if loader_shim {
ensure!(
Expand All @@ -198,14 +196,28 @@ impl RustApp {
);
}

// cargo profile

let cargo_profile = match cfg.release {
true => attrs.get("data-cargo-profile-dev"),
false => attrs.get("data-cargo-profile-release"),
}
.or_else(|| attrs.get("data-cargo-profile"))
.or_else(|| cfg.cargo_profile.as_ref())
.cloned();

// cargo features

let data_features = attrs.get("data-cargo-features").map(|val| val.to_string());
let data_all_features = attrs.contains_key("data-cargo-all-features");
let data_no_default_features = attrs.contains_key("data-cargo-no-default-features");

// Highlander-rule: There can be only one (prohibits contradicting arguments):
ensure!(
!(data_all_features && (data_no_default_features || data_features.is_some())),
"Cannot combine --all-features with --no-default-features and/or --features"
);

let skip_build = attrs.contains_key("data-trunk-skip");

let cargo_features = if data_all_features {
Features::All
} else if data_no_default_features || data_features.is_some() {
Expand All @@ -219,6 +231,10 @@ impl RustApp {
cfg.cargo_features.clone()
};

// skip

let skip_build = attrs.contains_key("data-trunk-skip");

// bindings

let import_bindings = !attrs.contains_key("data-wasm-no-import");
Expand Down Expand Up @@ -246,6 +262,7 @@ impl RustApp {
id,
cfg,
skip_build,
cargo_profile,
cargo_features,
manifest,
ignore_chan,
Expand Down Expand Up @@ -294,6 +311,7 @@ impl RustApp {
id: None,
skip_build: false,
cargo_features: cfg.cargo_features.clone(),
cargo_profile: None,
cfg,
manifest,
ignore_chan,
Expand Down Expand Up @@ -365,7 +383,10 @@ impl RustApp {
"--manifest-path",
&self.manifest.manifest_path,
];
if self.cfg.release {
if let Some(profile) = &self.cargo_profile {
args.push("--profile");
args.push(profile);
} else if self.cfg.release {
args.push("--release");
}
if self.cfg.offline {
Expand Down

0 comments on commit b585670

Please sign in to comment.