Skip to content

Commit 4641a5b

Browse files
Add minification for CSS and JS files
1 parent e8dff6f commit 4641a5b

File tree

8 files changed

+76
-26
lines changed

8 files changed

+76
-26
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ handlebars = "6.0"
3535
hex = "0.4.3"
3636
log = "0.4.17"
3737
memchr = "2.5.0"
38+
minifier = "0.3.6"
3839
opener = "0.8.1"
3940
pulldown-cmark = { version = "0.10.0", default-features = false, features = ["html"] } # Do not update, part of the public api.
4041
regex = "1.8.1"

src/book/init.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,18 @@ impl BookBuilder {
131131
}
132132

133133
let mut general_css = File::create(cssdir.join("general.css"))?;
134-
general_css.write_all(theme::GENERAL_CSS)?;
134+
theme::GENERAL_CSS.write_into(&mut general_css)?;
135135

136136
let mut chrome_css = File::create(cssdir.join("chrome.css"))?;
137-
chrome_css.write_all(theme::CHROME_CSS)?;
137+
theme::CHROME_CSS.write_into(&mut chrome_css)?;
138138

139139
if html_config.print.enable {
140140
let mut print_css = File::create(cssdir.join("print.css"))?;
141-
print_css.write_all(theme::PRINT_CSS)?;
141+
theme::PRINT_CSS.write_into(&mut print_css)?;
142142
}
143143

144144
let mut variables_css = File::create(cssdir.join("variables.css"))?;
145-
variables_css.write_all(theme::VARIABLES_CSS)?;
145+
theme::VARIABLES_CSS.write_into(&mut variables_css)?;
146146

147147
let mut favicon = File::create(themedir.join("favicon.png"))?;
148148
favicon.write_all(theme::FAVICON_PNG)?;
@@ -151,10 +151,10 @@ impl BookBuilder {
151151
favicon.write_all(theme::FAVICON_SVG)?;
152152

153153
let mut js = File::create(themedir.join("book.js"))?;
154-
js.write_all(theme::JS)?;
154+
theme::JS.write_into(&mut js)?;
155155

156156
let mut highlight_css = File::create(themedir.join("highlight.css"))?;
157-
highlight_css.write_all(theme::HIGHLIGHT_CSS)?;
157+
theme::HIGHLIGHT_CSS.write_into(&mut highlight_css)?;
158158

159159
let mut highlight_js = File::create(themedir.join("highlight.js"))?;
160160
highlight_js.write_all(theme::HIGHLIGHT_JS)?;

src/front-end/mod.rs

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,24 @@ pub static REDIRECT: &[u8] = include_bytes!("templates/redirect.hbs");
1919
pub static HEADER: &[u8] = include_bytes!("templates/header.hbs");
2020
pub static TOC_JS: &[u8] = include_bytes!("templates/toc.js.hbs");
2121
pub static TOC_HTML: &[u8] = include_bytes!("templates/toc.html.hbs");
22-
pub static CHROME_CSS: &[u8] = include_bytes!("css/chrome.css");
23-
pub static GENERAL_CSS: &[u8] = include_bytes!("css/general.css");
24-
pub static PRINT_CSS: &[u8] = include_bytes!("css/print.css");
25-
pub static VARIABLES_CSS: &[u8] = include_bytes!("css/variables.css");
22+
pub static CHROME_CSS: ContentToMinify<'static> =
23+
ContentToMinify::CSS(include_str!("css/chrome.css"));
24+
pub static GENERAL_CSS: ContentToMinify<'static> =
25+
ContentToMinify::CSS(include_str!("css/general.css"));
26+
pub static PRINT_CSS: ContentToMinify<'static> =
27+
ContentToMinify::CSS(include_str!("css/print.css"));
28+
pub static VARIABLES_CSS: ContentToMinify<'static> =
29+
ContentToMinify::CSS(include_str!("css/variables.css"));
2630
pub static FAVICON_PNG: &[u8] = include_bytes!("images/favicon.png");
2731
pub static FAVICON_SVG: &[u8] = include_bytes!("images/favicon.svg");
28-
pub static JS: &[u8] = include_bytes!("js/book.js");
32+
pub static JS: ContentToMinify<'static> = ContentToMinify::JS(include_str!("js/book.js"));
2933
pub static HIGHLIGHT_JS: &[u8] = include_bytes!("js/highlight.js");
30-
pub static TOMORROW_NIGHT_CSS: &[u8] = include_bytes!("css/tomorrow-night.css");
31-
pub static HIGHLIGHT_CSS: &[u8] = include_bytes!("css/highlight.css");
32-
pub static AYU_HIGHLIGHT_CSS: &[u8] = include_bytes!("css/ayu-highlight.css");
34+
pub static TOMORROW_NIGHT_CSS: ContentToMinify<'static> =
35+
ContentToMinify::CSS(include_str!("css/tomorrow-night.css"));
36+
pub static HIGHLIGHT_CSS: ContentToMinify<'static> =
37+
ContentToMinify::CSS(include_str!("css/highlight.css"));
38+
pub static AYU_HIGHLIGHT_CSS: ContentToMinify<'static> =
39+
ContentToMinify::CSS(include_str!("css/ayu-highlight.css"));
3340
pub static CLIPBOARD_JS: &[u8] = include_bytes!("js/clipboard.min.js");
3441
pub static FONT_AWESOME: &[u8] = include_bytes!("css/font-awesome.min.css");
3542
pub static FONT_AWESOME_EOT: &[u8] = include_bytes!("fonts/fontawesome-webfont.eot");
@@ -39,6 +46,31 @@ pub static FONT_AWESOME_WOFF: &[u8] = include_bytes!("fonts/fontawesome-webfont.
3946
pub static FONT_AWESOME_WOFF2: &[u8] = include_bytes!("fonts/fontawesome-webfont.woff2");
4047
pub static FONT_AWESOME_OTF: &[u8] = include_bytes!("fonts/FontAwesome.otf");
4148

49+
#[derive(Clone, Copy)]
50+
pub enum ContentToMinify<'a> {
51+
CSS(&'a str),
52+
JS(&'a str),
53+
}
54+
55+
impl<'a> ContentToMinify<'a> {
56+
pub fn minified(self) -> Vec<u8> {
57+
let mut out = Vec::new();
58+
self.write_into(&mut out).unwrap();
59+
out
60+
}
61+
62+
pub fn write_into<W: std::io::Write>(self, out: &mut W) -> std::io::Result<()> {
63+
match self {
64+
Self::CSS(data) => match minifier::css::minify(data) {
65+
Ok(data) => return data.write(out),
66+
Err(_) => out.write(data.as_bytes())?,
67+
},
68+
Self::JS(data) => return minifier::js::minify(data).write(out),
69+
};
70+
Ok(())
71+
}
72+
}
73+
4274
/// The `Theme` struct should be used instead of the static variables because
4375
/// the `new()` method will look if the user has a theme directory in their
4476
/// source folder and use the users theme instead of the default.
@@ -181,18 +213,18 @@ impl Default for Theme {
181213
header: HEADER.to_owned(),
182214
toc_js: TOC_JS.to_owned(),
183215
toc_html: TOC_HTML.to_owned(),
184-
chrome_css: CHROME_CSS.to_owned(),
185-
general_css: GENERAL_CSS.to_owned(),
186-
print_css: PRINT_CSS.to_owned(),
187-
variables_css: VARIABLES_CSS.to_owned(),
216+
chrome_css: CHROME_CSS.minified(),
217+
general_css: GENERAL_CSS.minified(),
218+
print_css: PRINT_CSS.minified(),
219+
variables_css: VARIABLES_CSS.minified(),
188220
fonts_css: None,
189221
font_files: Vec::new(),
190222
favicon_png: Some(FAVICON_PNG.to_owned()),
191223
favicon_svg: Some(FAVICON_SVG.to_owned()),
192-
js: JS.to_owned(),
193-
highlight_css: HIGHLIGHT_CSS.to_owned(),
194-
tomorrow_night_css: TOMORROW_NIGHT_CSS.to_owned(),
195-
ayu_highlight_css: AYU_HIGHLIGHT_CSS.to_owned(),
224+
js: JS.minified(),
225+
highlight_css: HIGHLIGHT_CSS.minified(),
226+
tomorrow_night_css: TOMORROW_NIGHT_CSS.minified(),
227+
ayu_highlight_css: AYU_HIGHLIGHT_CSS.minified(),
196228
highlight_js: HIGHLIGHT_JS.to_owned(),
197229
clipboard_js: CLIPBOARD_JS.to_owned(),
198230
}

src/front-end/searcher/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Theme dependencies for in-browser search. Not included in mdbook when
22
//! the "search" cargo feature is disabled.
33
4-
pub static JS: &[u8] = include_bytes!("searcher.js");
4+
use crate::theme::ContentToMinify;
5+
6+
pub static JS: ContentToMinify<'static> = ContentToMinify::JS(include_str!("searcher.js"));
57
pub static MARK_JS: &[u8] = include_bytes!("mark.min.js");
68
pub static ELASTICLUNR_JS: &[u8] = include_bytes!("elasticlunr.min.js");

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::errors::*;
44
use crate::renderer::html_handlebars::helpers;
55
use crate::renderer::html_handlebars::StaticFiles;
66
use crate::renderer::{RenderContext, Renderer};
7-
use crate::theme::{self, Theme};
7+
use crate::theme::{self, ContentToMinify, Theme};
88
use crate::utils;
99

1010
use std::borrow::Cow;
@@ -396,7 +396,8 @@ impl Renderer for HtmlHandlebars {
396396
debug!("Render toc js");
397397
{
398398
let rendered_toc = handlebars.render("toc_js", &data)?;
399-
static_files.add_builtin("toc.js", rendered_toc.as_bytes());
399+
let rendered_toc = ContentToMinify::JS(&rendered_toc);
400+
static_files.add_owned_builtin("toc.js", rendered_toc.minified());
400401
debug!("Creating toc.js ✓");
401402
}
402403

src/renderer/html_handlebars/search.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub fn create_files(
7474
)
7575
.as_bytes(),
7676
);
77-
static_files.add_builtin("searcher.js", searcher::JS);
77+
static_files.add_owned_builtin("searcher.js", searcher::JS.minified());
7878
static_files.add_builtin("mark.min.js", searcher::MARK_JS);
7979
static_files.add_builtin("elasticlunr.min.js", searcher::ELASTICLUNR_JS);
8080
debug!("Copying search files ✓");

src/renderer/html_handlebars/static_files.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ impl StaticFiles {
159159
}
160160

161161
pub fn add_builtin(&mut self, filename: &str, data: &[u8]) {
162+
self.add_owned_builtin(filename, data.to_owned());
163+
}
164+
165+
pub fn add_owned_builtin(&mut self, filename: &str, data: Vec<u8>) {
162166
self.static_files.push(StaticFile::Builtin {
163167
filename: filename.to_owned(),
164168
data: data.to_owned(),

0 commit comments

Comments
 (0)