Skip to content
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

HTML output filename #937

Merged
merged 5 commits into from
Jan 13, 2025
Merged
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
8 changes: 8 additions & 0 deletions schemas/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"dist": "dist",
"filehash": true,
"frozen": false,
"html_output": null,
"inject_scripts": true,
"locked": false,
"minify": "never",
Expand Down Expand Up @@ -144,6 +145,13 @@
"default": false,
"type": "boolean"
},
"html_output": {
"description": "The name of the output HTML file.\n\nIf not set, use the same name as the target HTML file.",
"type": [
"string",
"null"
]
},
"inject_scripts": {
"description": "Whether to inject scripts into your index file.\n\nThese values can only be provided via config file.",
"default": true,
Expand Down
30 changes: 17 additions & 13 deletions site/content/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ weight = 2
Trunk supports a layered config system. At the base, a config file can encapsulate project specific defaults, paths, ports and other config. Environment variables can be used to overwrite config file values. Lastly, CLI arguments / options take final precedence.

# Trunk.toml

Trunk supports an optional `Trunk.toml` config file. An example config file is included [in the Trunk repo](https://github.com/trunk-rs/trunk/blob/main/Trunk.toml), and shows all available config options along with their default values. By default, Trunk will look for a `Trunk.toml` config file in the current working directory. Trunk supports the global `--config` option to specify an alternative location for the file.

Note that any relative paths declared in a `Trunk.toml` file will be treated as being relative to the `Trunk.toml` file itself.
Expand All @@ -31,7 +32,6 @@ features early.
**NOTE:** Versions prior do `0.19.0-alpha.2` currently do not support this
check, and so they will silently ignore such an error for now.


```toml
trunk-version = "^0.20.1"
```
Expand All @@ -44,17 +44,18 @@ generation of the assets.

```toml
[build]
target = "index.html" # The index HTML file to drive the bundling process.
release = false # Build in release mode.
dist = "dist" # The output dir for all final assets.
public_url = "/" # The public URL from which assets are to be served.
filehash = true # Whether to include hash values in the output file names.
inject_scripts = true # Whether to inject scripts (and module preloads) into the finalized output.
offline = false # Run without network access
frozen = false # Require Cargo.lock and cache are up to date
locked = false # Require Cargo.lock is up to date
minify = "never" # Control minification: can be one of: never, on_release, always
no_sri = false # Allow disabling sub-resource integrity (SRI)
target = "index.html" # The index HTML file to drive the bundling process.
html_output = "index.html" # The name of the output HTML file.
release = false # Build in release mode.
dist = "dist" # The output dir for all final assets.
public_url = "/" # The public URL from which assets are to be served.
filehash = true # Whether to include hash values in the output file names.
inject_scripts = true # Whether to inject scripts (and module preloads) into the finalized output.
offline = false # Run without network access
frozen = false # Require Cargo.lock and cache are up to date
locked = false # Require Cargo.lock is up to date
minify = "never" # Control minification: can be one of: never, on_release, always
no_sri = false # Allow disabling sub-resource integrity (SRI)
```

## Watch section
Expand Down Expand Up @@ -136,18 +137,22 @@ command_arguments = [] # Arguments to pass to command
```

# Environment Variables

Trunk environment variables mirror the `Trunk.toml` config schema. All Trunk environment variables have the following 3 part form `TRUNK_<SECTION>_<ITEM>`, where `TRUNK_` is the required prefix, `<SECTION>` is one of the `Trunk.toml` sections, and `<ITEM>` is a specific configuration item from the corresponding section. E.G., `TRUNK_SERVE_PORT=80` will cause `trunk serve` to listen on port `80`. The equivalent CLI invocation would be `trunk serve --port=80`.

In addition, there is the variable `TRUNK_SKIP_VERSION_CHECK` which allows to control the update check (if that is)
compiled into the version of trunk.

# CLI Arguments & Options

The final configuration layer is the CLI itself. Any arguments / options provided on the CLI will take final precedence over any other config layer.

# Proxy

Trunk ships with a built-in proxy which can be enabled when running `trunk serve`. There are two ways to configure the proxy, each discussed below. All Trunk proxies will transparently pass along the request body, headers, and query parameters to the proxy backend.

## Proxy CLI Flags

The `trunk serve` command accepts two proxy related flags.

`--proxy-backend` specifies the URL of the backend server to which requests should be proxied. The URI segment of the given URL will be used as the path on the Trunk server to handle proxy requests. E.G., `trunk serve --proxy-backend=http://localhost:9000/api/` will proxy any requests received on the path `/api/` to the server listening at `http://localhost:9000/api/`. Further path segments or query parameters will be seamlessly passed along.
Expand All @@ -159,4 +164,3 @@ The `trunk serve` command accepts two proxy related flags.
`--proxy-no-sytem-proxy` bypasses the system proxy when contacting the proxy backend.

`--proxy-ws` specifies that the proxy is for a WebSocket endpoint.

6 changes: 6 additions & 0 deletions src/cmd/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ pub struct Build {
/// The index HTML file to drive the bundling process
pub target: Option<PathBuf>,

/// The name of the output HTML file.
#[arg(long, env = "TRUNK_BUILD_HTML_OUTPUT")]
pub html_output: Option<String>,

/// Build in release mode
#[arg(long, env = "TRUNK_BUILD_RELEASE")]
#[arg(default_missing_value="true", num_args=0..=1)]
Expand Down Expand Up @@ -123,6 +127,7 @@ impl Build {
let Self {
core,
target,
html_output,
release,
cargo_profile,
dist,
Expand All @@ -144,6 +149,7 @@ impl Build {
} = self;

config.build.target = target.unwrap_or(config.build.target);
config.build.html_output = html_output.or(config.build.html_output);
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);
Expand Down
6 changes: 6 additions & 0 deletions src/config/models/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ pub struct Build {
#[serde(default = "default::target")]
pub target: PathBuf,

/// The name of the output HTML file.
///
/// If not set, use the same name as the target HTML file.
pub html_output: Option<String>,

/// Build in release mode [default: false]
#[serde(default)]
pub release: bool,
Expand Down Expand Up @@ -189,6 +194,7 @@ impl Default for Build {
fn default() -> Self {
Self {
target: default::target(),
html_output: None,
release: false,
cargo_profile: None,
dist: default::dist(),
Expand Down
14 changes: 14 additions & 0 deletions src/config/rt/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ pub struct RtcBuild {
pub core: RtcCore,
/// The index HTML file to drive the bundling process.
pub target: PathBuf,
/// The name of the output HTML file.
pub html_output_filename: String,
/// The parent directory of the target index HTML file.
pub target_parent: PathBuf,
/// Build in release mode.
Expand Down Expand Up @@ -132,6 +134,15 @@ impl RtcBuild {
)
})?;

let html_output_filename = match build.html_output {
Some(html_output) => html_output,
None => target
.file_name()
.context("target path isn't a file path")?
.to_string_lossy()
.into_owned(),
};

// Get the target HTML's parent dir, falling back to OS specific root, as that is the only
// time when no parent could be determined.
let target_parent = target
Expand Down Expand Up @@ -178,6 +189,7 @@ impl RtcBuild {
Ok(Self {
core,
target,
html_output_filename,
target_parent,
release: build.release,
cargo_profile: build.cargo_profile,
Expand Down Expand Up @@ -210,6 +222,7 @@ impl RtcBuild {
#[cfg(test)]
pub async fn new_test(tmpdir: &std::path::Path) -> anyhow::Result<Self> {
let target = tmpdir.join("index.html");
let html_output_filename = String::from("index.html");
let target_parent = tmpdir.to_path_buf();
let final_dist = tmpdir.join("dist");
let staging_dist = final_dist.join(".stage");
Expand All @@ -219,6 +232,7 @@ impl RtcBuild {
Ok(Self {
core: RtcCore::new_test(tmpdir),
target,
html_output_filename,
target_parent,
release: false,
cargo_profile: None,
Expand Down
9 changes: 6 additions & 3 deletions src/pipelines/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,12 @@ impl HtmlPipeline {
false => target_html.into_inner(),
};

fs::write(self.cfg.staging_dist.join("index.html"), &output_html)
.await
.context("error writing finalized HTML output")?;
fs::write(
self.cfg.staging_dist.join(&self.cfg.html_output_filename),
&output_html,
)
.await
.context("error writing finalized HTML output")?;

// Spawn and wait on post-build hooks.
wait_hooks(spawn_hooks(self.cfg.clone(), PipelineStage::PostBuild)).await?;
Expand Down