Skip to content

Commit

Permalink
Merge pull request #165 from sebadob/static-asset-precompression
Browse files Browse the repository at this point in the history
Implement precompression of static assets
  • Loading branch information
gbj authored Oct 3, 2023
2 parents 15535b2 + d3c510f commit 299ee40
Show file tree
Hide file tree
Showing 14 changed files with 196 additions and 0 deletions.
98 changes: 98 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ rust-version = "1.71"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
brotli = "3.3"
clap = { version = "4.0", features = ["derive"] }
serde = { version = "1.0", features = ["derive"] }
anyhow = "1.0"
libflate = "2"
log = "0.4"
flexi_logger = "0.25"
lightningcss = { version = "1.0.0-alpha.47", features = ["browserslist"] }
Expand Down
13 changes: 13 additions & 0 deletions src/command/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::sync::Arc;

use crate::ext::compress;
use crate::{
compile,
compile::ChangeSet,
Expand Down Expand Up @@ -46,6 +47,18 @@ pub async fn build_proj(proj: &Arc<Project>) -> Result<bool> {
if !compile::style(proj, &changes).await.await??.is_success() {
return Ok(false);
}

// it is important to do the precompression of the static files before building the
// server to make it possible to include them as assets into the binary itself
if proj.release
&& proj.precompress
&& compress::compress_static_files(proj.site.root_dir.clone().into())
.await
.is_err()
{
return Ok(false);
}

if !compile::server(proj, &changes).await.await??.is_success() {
return Ok(false);
}
Expand Down
2 changes: 2 additions & 0 deletions src/compile/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use super::server::build_cargo_server_cmd;
fn release_opts() -> Opts {
Opts {
release: true,
precompress: false, // if set to true, testing could take quite a while longer
hot_reload: false,
project: None,
verbose: 0,
Expand All @@ -21,6 +22,7 @@ fn release_opts() -> Opts {
fn dev_opts() -> Opts {
Opts {
release: false,
precompress: false,
hot_reload: false,
project: None,
verbose: 0,
Expand Down
4 changes: 4 additions & 0 deletions src/config/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ pub struct Opts {
#[arg(short, long)]
pub release: bool,

/// Precompress static assets with gzip and brotli. Applies to release builds only.
#[arg(short = 'P', long)]
pub precompress: bool,

/// Turn on partial hot-reloading. Requires rust nightly [beta]
#[arg(long)]
pub hot_reload: bool,
Expand Down
3 changes: 3 additions & 0 deletions src/config/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub struct Project {
pub style: StyleConfig,
pub watch: bool,
pub release: bool,
pub precompress: bool,
pub hot_reload: bool,
pub site: Arc<Site>,
pub end2end: Option<End2EndConfig>,
Expand All @@ -51,6 +52,7 @@ impl Debug for Project {
.field("style", &self.style)
.field("watch", &self.watch)
.field("release", &self.release)
.field("precompress", &self.precompress)
.field("hot_reload", &self.hot_reload)
.field("site", &self.site)
.field("end2end", &self.end2end)
Expand Down Expand Up @@ -89,6 +91,7 @@ impl Project {
style: StyleConfig::new(&config)?,
watch,
release: cli.release,
precompress: cli.precompress,
hot_reload: cli.hot_reload,
site: Arc::new(Site::new(&config)),
end2end: End2EndConfig::resolve(&config),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Config {
},
watch: true,
release: false,
precompress: false,
hot_reload: false,
site: Site {
addr: 127.0.0.1:3000,
Expand All @@ -87,6 +88,7 @@ Config {
],
cli: Opts {
release: false,
precompress: false,
hot_reload: false,
project: None,
features: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Config {
},
watch: true,
release: false,
precompress: false,
hot_reload: false,
site: Site {
addr: 127.0.0.1:3000,
Expand Down Expand Up @@ -123,6 +124,7 @@ Config {
},
watch: true,
release: false,
precompress: false,
hot_reload: false,
site: Site {
addr: 127.0.0.1:3000,
Expand All @@ -143,6 +145,7 @@ Config {
],
cli: Opts {
release: false,
precompress: false,
hot_reload: false,
project: None,
features: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Config {
},
watch: true,
release: false,
precompress: false,
hot_reload: false,
site: Site {
addr: 127.0.0.1:3000,
Expand All @@ -77,6 +78,7 @@ Config {
],
cli: Opts {
release: false,
precompress: false,
hot_reload: false,
project: None,
features: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Config {
},
watch: true,
release: false,
precompress: false,
hot_reload: false,
site: Site {
addr: 127.0.0.1:3000,
Expand All @@ -73,6 +74,7 @@ Config {
],
cli: Opts {
release: false,
precompress: false,
hot_reload: false,
project: Some(
"project1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Config {
},
watch: true,
release: false,
precompress: false,
hot_reload: false,
site: Site {
addr: 127.0.0.1:3000,
Expand All @@ -77,6 +78,7 @@ Config {
],
cli: Opts {
release: false,
precompress: false,
hot_reload: false,
project: Some(
"project2",
Expand Down
1 change: 1 addition & 0 deletions src/config/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use super::Config;
fn opts(project: Option<&str>) -> crate::config::Opts {
crate::config::Opts {
release: false,
precompress: false,
hot_reload: false,
project: project.map(|s| s.to_string()),
verbose: 0,
Expand Down
Loading

0 comments on commit 299ee40

Please sign in to comment.