Minify css
This commit is contained in:
parent
b7c6e8f180
commit
f7485df05b
3 changed files with 35 additions and 16 deletions
|
@ -1220,7 +1220,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "minifier"
|
name = "minifier"
|
||||||
version = "0.0.11"
|
version = "0.0.14"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"regex 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
"regex 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
@ -2426,7 +2426,7 @@ dependencies = [
|
||||||
name = "rustdoc"
|
name = "rustdoc"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"minifier 0.0.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
"minifier 0.0.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"pulldown-cmark 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
"pulldown-cmark 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"tempfile 3.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
"tempfile 3.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
@ -3263,7 +3263,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
"checksum mdbook 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "90b5a8d7e341ceee5db3882a06078d42661ddcfa2b3687319cc5da76ec4e782f"
|
"checksum mdbook 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "90b5a8d7e341ceee5db3882a06078d42661ddcfa2b3687319cc5da76ec4e782f"
|
||||||
"checksum memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "796fba70e76612589ed2ce7f45282f5af869e0fdd7cc6199fa1aa1f1d591ba9d"
|
"checksum memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "796fba70e76612589ed2ce7f45282f5af869e0fdd7cc6199fa1aa1f1d591ba9d"
|
||||||
"checksum memoffset 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0f9dc261e2b62d7a622bf416ea3c5245cdd5d9a7fcc428c0d06804dfce1775b3"
|
"checksum memoffset 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0f9dc261e2b62d7a622bf416ea3c5245cdd5d9a7fcc428c0d06804dfce1775b3"
|
||||||
"checksum minifier 0.0.11 (registry+https://github.com/rust-lang/crates.io-index)" = "26f3e36a4db1981b16567e4abfd6ddc3641bc9b950bdc868701f656bf9b74bdd"
|
"checksum minifier 0.0.14 (registry+https://github.com/rust-lang/crates.io-index)" = "78cb57f9a385530d60f2d67f6e108050b478b7a0ffd0bb9c350803e1356535dd"
|
||||||
"checksum miniz-sys 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "609ce024854aeb19a0ef7567d348aaa5a746b32fb72e336df7fcc16869d7e2b4"
|
"checksum miniz-sys 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "609ce024854aeb19a0ef7567d348aaa5a746b32fb72e336df7fcc16869d7e2b4"
|
||||||
"checksum miow 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9224c91f82b3c47cf53dcf78dfaa20d6888fbcc5d272d5f2fcdf8a697f3c987d"
|
"checksum miow 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9224c91f82b3c47cf53dcf78dfaa20d6888fbcc5d272d5f2fcdf8a697f3c987d"
|
||||||
"checksum nibble_vec 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "c8d77f3db4bce033f4d04db08079b2ef1c3d02b44e86f25d08886fafa7756ffa"
|
"checksum nibble_vec 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "c8d77f3db4bce033f4d04db08079b2ef1c3d02b44e86f25d08886fafa7756ffa"
|
||||||
|
|
|
@ -9,5 +9,5 @@ path = "lib.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
pulldown-cmark = { version = "0.1.2", default-features = false }
|
pulldown-cmark = { version = "0.1.2", default-features = false }
|
||||||
minifier = "0.0.11"
|
minifier = "0.0.14"
|
||||||
tempfile = "3"
|
tempfile = "3"
|
||||||
|
|
|
@ -42,6 +42,7 @@ use std::collections::{BTreeMap, HashSet, VecDeque};
|
||||||
use std::default::Default;
|
use std::default::Default;
|
||||||
use std::error;
|
use std::error;
|
||||||
use std::fmt::{self, Display, Formatter, Write as FmtWrite};
|
use std::fmt::{self, Display, Formatter, Write as FmtWrite};
|
||||||
|
use std::ffi::OsStr;
|
||||||
use std::fs::{self, File, OpenOptions};
|
use std::fs::{self, File, OpenOptions};
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
use std::io::{self, BufWriter, BufReader};
|
use std::io::{self, BufWriter, BufReader};
|
||||||
|
@ -756,10 +757,12 @@ fn write_shared(cx: &Context,
|
||||||
// Add all the static files. These may already exist, but we just
|
// Add all the static files. These may already exist, but we just
|
||||||
// overwrite them anyway to make sure that they're fresh and up-to-date.
|
// overwrite them anyway to make sure that they're fresh and up-to-date.
|
||||||
|
|
||||||
write(cx.dst.join(&format!("rustdoc{}.css", cx.shared.resource_suffix)),
|
write_minify(cx.dst.join(&format!("rustdoc{}.css", cx.shared.resource_suffix)),
|
||||||
include_bytes!("static/rustdoc.css"))?;
|
include_str!("static/rustdoc.css"),
|
||||||
write(cx.dst.join(&format!("settings{}.css", cx.shared.resource_suffix)),
|
enable_minification)?;
|
||||||
include_bytes!("static/settings.css"))?;
|
write_minify(cx.dst.join(&format!("settings{}.css", cx.shared.resource_suffix)),
|
||||||
|
include_str!("static/settings.css"),
|
||||||
|
enable_minification)?;
|
||||||
|
|
||||||
// To avoid "light.css" to be overwritten, we'll first run over the received themes and only
|
// To avoid "light.css" to be overwritten, we'll first run over the received themes and only
|
||||||
// then we'll run over the "official" styles.
|
// then we'll run over the "official" styles.
|
||||||
|
@ -781,11 +784,13 @@ fn write_shared(cx: &Context,
|
||||||
include_bytes!("static/brush.svg"))?;
|
include_bytes!("static/brush.svg"))?;
|
||||||
write(cx.dst.join(&format!("wheel{}.svg", cx.shared.resource_suffix)),
|
write(cx.dst.join(&format!("wheel{}.svg", cx.shared.resource_suffix)),
|
||||||
include_bytes!("static/wheel.svg"))?;
|
include_bytes!("static/wheel.svg"))?;
|
||||||
write(cx.dst.join(&format!("light{}.css", cx.shared.resource_suffix)),
|
write_minify(cx.dst.join(&format!("light{}.css", cx.shared.resource_suffix)),
|
||||||
include_bytes!("static/themes/light.css"))?;
|
include_str!("static/themes/light.css"),
|
||||||
|
enable_minification)?;
|
||||||
themes.insert("light".to_owned());
|
themes.insert("light".to_owned());
|
||||||
write(cx.dst.join(&format!("dark{}.css", cx.shared.resource_suffix)),
|
write_minify(cx.dst.join(&format!("dark{}.css", cx.shared.resource_suffix)),
|
||||||
include_bytes!("static/themes/dark.css"))?;
|
include_str!("static/themes/dark.css"),
|
||||||
|
enable_minification)?;
|
||||||
themes.insert("dark".to_owned());
|
themes.insert("dark".to_owned());
|
||||||
|
|
||||||
let mut themes: Vec<&String> = themes.iter().collect();
|
let mut themes: Vec<&String> = themes.iter().collect();
|
||||||
|
@ -857,10 +862,19 @@ themePicker.onblur = handleThemeButtonsBlur;
|
||||||
|
|
||||||
if let Some(ref css) = cx.shared.css_file_extension {
|
if let Some(ref css) = cx.shared.css_file_extension {
|
||||||
let out = cx.dst.join(&format!("theme{}.css", cx.shared.resource_suffix));
|
let out = cx.dst.join(&format!("theme{}.css", cx.shared.resource_suffix));
|
||||||
try_err!(fs::copy(css, out), css);
|
if !enable_minification {
|
||||||
|
try_err!(fs::copy(css, out), css);
|
||||||
|
} else {
|
||||||
|
let mut f = try_err!(File::open(css), css);
|
||||||
|
let mut buffer = String::with_capacity(1000);
|
||||||
|
|
||||||
|
try_err!(f.read_to_string(&mut buffer), css);
|
||||||
|
write_minify(out, &buffer, enable_minification)?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
write(cx.dst.join(&format!("normalize{}.css", cx.shared.resource_suffix)),
|
write_minify(cx.dst.join(&format!("normalize{}.css", cx.shared.resource_suffix)),
|
||||||
include_bytes!("static/normalize.css"))?;
|
include_str!("static/normalize.css"),
|
||||||
|
enable_minification)?;
|
||||||
write(cx.dst.join("FiraSans-Regular.woff"),
|
write(cx.dst.join("FiraSans-Regular.woff"),
|
||||||
include_bytes!("static/FiraSans-Regular.woff"))?;
|
include_bytes!("static/FiraSans-Regular.woff"))?;
|
||||||
write(cx.dst.join("FiraSans-Medium.woff"),
|
write(cx.dst.join("FiraSans-Medium.woff"),
|
||||||
|
@ -1051,7 +1065,12 @@ fn write(dst: PathBuf, contents: &[u8]) -> Result<(), Error> {
|
||||||
|
|
||||||
fn write_minify(dst: PathBuf, contents: &str, enable_minification: bool) -> Result<(), Error> {
|
fn write_minify(dst: PathBuf, contents: &str, enable_minification: bool) -> Result<(), Error> {
|
||||||
if enable_minification {
|
if enable_minification {
|
||||||
write(dst, minifier::js::minify(contents).as_bytes())
|
if dst.extension() == Some(&OsStr::new("css")) {
|
||||||
|
let res = try_none!(minifier::css::minify(contents).ok(), &dst);
|
||||||
|
write(dst, res.as_bytes())
|
||||||
|
} else {
|
||||||
|
write(dst, minifier::js::minify(contents).as_bytes())
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
write(dst, contents.as_bytes())
|
write(dst, contents.as_bytes())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue