From 063ee29a0c61d1c2c6c00cd206a5cb4169b31e60 Mon Sep 17 00:00:00 2001 From: Chrislearn Young Date: Fri, 1 Dec 2023 08:52:55 +0800 Subject: [PATCH] Add brotli and zstd support --- crates/cli/Cargo.toml | 50 ++++++++++++++++----------------- crates/cli/src/compile/front.rs | 26 ++++++++++++++--- 2 files changed, 47 insertions(+), 29 deletions(-) diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index a2bed31..4d0abe9 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -12,43 +12,43 @@ rust-version = { workspace = true } [[bin]] path = "src/main.rs" name = "glory" - # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -clap = { version = "4.0", features = ["derive"] } -serde = { workspace = true, features = ["derive"] } +ansi_term = "0.12" anyhow = { workspace = true } -log = "0.4" +async-trait = "0.1.72" +brotli = { version = "3.4", features = ["default"] } +bytes = "1.4" +camino = "1.1" +cargo_metadata = { version = "0.18", features = ["builder"] } +clap = { version = "4.0", features = ["derive"] } +derive_more = "0.99" +dirs = "5.0" +dotenvy = "0.15" +dunce = "1.0" +flate2 = "1.0" flexi_logger = "0.27" +# glory-hot-reload = { git = "https://github.com/glory-rs/glory", version = "0.4.8" } +glory-hot-reload = { workspace = true } +itertools = "0.11" lightningcss = { version = "1.0.0-alpha.51", features = ["browserslist"] } -salvo = { workspace = true, features = ["websocket"] } -tokio = { version = "1.4", default-features = false, features = ["full"] } +log = "0.4" # not using notify 5.0 because it uses Crossbeam which has an issue with tokio notify = "4" once_cell ={ workspace = true } -which = "5.0" -cargo_metadata = { version = "0.18", features = ["builder"] } +reqwest = { version = "0.11", features = ["blocking", "__tls", "default-tls", "native-tls-crate", "json"], default-features = false } +salvo = { workspace = true, features = ["websocket"] } +seahash = "4.1" +semver = "1.0.18" +serde = { workspace = true, features = ["derive"] } serde_json = "1.0" +tar = "0.4" +tokio = { version = "1.4", default-features = false, features = ["full"] } wasm-bindgen-cli-support = "0.2" -ansi_term = "0.12" - -seahash = "4.1" -reqwest = { version = "0.11", features = ["blocking", "__tls", "default-tls", "native-tls-crate", "json"], default-features = false } -dirs = "5.0" -camino = "1.1" -dotenvy = "0.15" -itertools = "0.11" -derive_more = "0.99" -flate2 = "1.0" +which = "5.0" zip = { version = "0.6", default-features = false, features = ["deflate"] } -tar = "0.4" -dunce = "1.0" -bytes = "1.4" -# glory-hot-reload = { git = "https://github.com/glory-rs/glory", version = "0.4.8" } -glory-hot-reload = { workspace = true } -semver = "1.0.18" -async-trait = "0.1.72" +zstd = { version = "0.13", features = ["default"] } [dev-dependencies] insta = { version = "1.31.0", features = ["yaml"] } diff --git a/crates/cli/src/compile/front.rs b/crates/cli/src/compile/front.rs index 1bcaaaf..498b1c9 100644 --- a/crates/cli/src/compile/front.rs +++ b/crates/cli/src/compile/front.rs @@ -1,6 +1,14 @@ use std::collections::HashMap; +use std::fs::File; +use std::io::Write; use std::sync::Arc; +use brotli::CompressorWriter; +use camino::{Utf8Path, Utf8PathBuf}; +use tokio::process::Child; +use tokio::{process::Command, sync::broadcast, task::JoinHandle}; +use wasm_bindgen_cli_support::Bindgen; + use super::ChangeSet; use crate::config::Project; use crate::ext::fs; @@ -14,10 +22,6 @@ use crate::{ }, logger::GRAY, }; -use camino::{Utf8Path, Utf8PathBuf}; -use tokio::process::Child; -use tokio::{process::Command, sync::broadcast, task::JoinHandle}; -use wasm_bindgen_cli_support::Bindgen; pub async fn front(proj: &Arc, changes: &ChangeSet) -> JoinHandle>> { let proj = proj.clone(); @@ -100,6 +104,20 @@ async fn bindgen(proj: &Project) -> Result> { CommandResult::Failure(_) => return Ok(Outcome::Failed), _ => {} } + let data = fs::read(&wasm_file.dest).await?; + + let br_file = File::create(format!("{}.br", wasm_file.dest.as_str()))?; + let mut br_writer = CompressorWriter::new( + br_file, + 32 * 1024, // 32 KiB buffer + 11, // BROTLI_PARAM_QUALITY + 22, // BROTLI_PARAM_LGWIN + ); + br_writer.write_all(&data)?; + + let zstd_data = zstd::encode_all(&*data, 21)?; + let mut zstd_file = File::create(format!("{}.br", wasm_file.dest.as_str()))?; + zstd_file.write_all(&zstd_data)?; } let mut js_changed = false;