Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/static-asset-precompression' int…
Browse files Browse the repository at this point in the history
…o static-asset-precompression
  • Loading branch information
Sebastian Dobe committed Aug 8, 2023
2 parents 7320878 + 3cd44cc commit d3c510f
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 7 deletions.
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
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
14 changes: 7 additions & 7 deletions src/ext/compress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ use tokio::time::Instant;

pub async fn compress_static_files(path: PathBuf) -> Result<()> {
let start = Instant::now();

tokio::task::spawn_blocking(move || compress_dir_all(path)).await??;

log::info!(
"Precompression of static files finished after {} ms",
start.elapsed().as_millis()
);

Ok(())
}

Expand All @@ -31,29 +32,28 @@ fn compress_dir_all(path: PathBuf) -> Result<()> {
let metadata = fs::metadata(&path)?;

if metadata.is_dir() {
compress_dir_all(path)?
compress_dir_all(path)?;
} else {
let pstr = path.as_os_str().to_str().unwrap();
let pstr = path.to_str().unwrap_or_default();
if pstr.ends_with(".gz") || pstr.ends_with(".br") {
// skip all files that are already compressed
continue;
}

let file = fs::read(&path)?;
let pstr = path.to_str();

// gzip
let mut encoder = gzip::Encoder::new(Vec::new())?;
encoder.write_all(file.as_ref())?;
let encoded_data = encoder.finish().into_result()?;
let path_gz = format!("{}.gz", pstr.unwrap());
let path_gz = format!("{}.gz", pstr);
fs::write(path_gz, encoded_data)?;

// brotli
let path_br = format!("{}.br", pstr.unwrap());
let path_br = format!("{}.br", pstr);
let mut output = File::create(path_br)?;
let mut reader = BufReader::new(file.as_slice());
brotli::BrotliCompress(&mut reader, &mut output, &brotli_params).unwrap();
brotli::BrotliCompress(&mut reader, &mut output, &brotli_params)?;
}
}

Expand Down

0 comments on commit d3c510f

Please sign in to comment.