From 4641a5b9a4434fed307ab092dd9a56d1bdb3fb0b Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 12 Jun 2025 18:32:19 +0200 Subject: [PATCH 1/2] Add minification for CSS and JS files --- Cargo.lock | 10 +++ Cargo.toml | 1 + src/book/init.rs | 12 ++-- src/front-end/mod.rs | 64 +++++++++++++++----- src/front-end/searcher/mod.rs | 4 +- src/renderer/html_handlebars/hbs_renderer.rs | 5 +- src/renderer/html_handlebars/search.rs | 2 +- src/renderer/html_handlebars/static_files.rs | 4 ++ 8 files changed, 76 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6d287160c6..2e2c378804 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1251,6 +1251,7 @@ dependencies = [ "ignore", "log", "memchr", + "minifier", "notify", "notify-debouncer-mini", "opener", @@ -1305,6 +1306,15 @@ dependencies = [ "unicase", ] +[[package]] +name = "minifier" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14f1541610994bba178cb36757e102d06a52a2d9612aa6d34c64b3b377c5d943" +dependencies = [ + "clap", +] + [[package]] name = "miniz_oxide" version = "0.8.8" diff --git a/Cargo.toml b/Cargo.toml index 4413e1dfdd..b6125cab18 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,6 +35,7 @@ handlebars = "6.0" hex = "0.4.3" log = "0.4.17" memchr = "2.5.0" +minifier = "0.3.6" opener = "0.8.1" pulldown-cmark = { version = "0.10.0", default-features = false, features = ["html"] } # Do not update, part of the public api. regex = "1.8.1" diff --git a/src/book/init.rs b/src/book/init.rs index faca1d09aa..7408edf953 100644 --- a/src/book/init.rs +++ b/src/book/init.rs @@ -131,18 +131,18 @@ impl BookBuilder { } let mut general_css = File::create(cssdir.join("general.css"))?; - general_css.write_all(theme::GENERAL_CSS)?; + theme::GENERAL_CSS.write_into(&mut general_css)?; let mut chrome_css = File::create(cssdir.join("chrome.css"))?; - chrome_css.write_all(theme::CHROME_CSS)?; + theme::CHROME_CSS.write_into(&mut chrome_css)?; if html_config.print.enable { let mut print_css = File::create(cssdir.join("print.css"))?; - print_css.write_all(theme::PRINT_CSS)?; + theme::PRINT_CSS.write_into(&mut print_css)?; } let mut variables_css = File::create(cssdir.join("variables.css"))?; - variables_css.write_all(theme::VARIABLES_CSS)?; + theme::VARIABLES_CSS.write_into(&mut variables_css)?; let mut favicon = File::create(themedir.join("favicon.png"))?; favicon.write_all(theme::FAVICON_PNG)?; @@ -151,10 +151,10 @@ impl BookBuilder { favicon.write_all(theme::FAVICON_SVG)?; let mut js = File::create(themedir.join("book.js"))?; - js.write_all(theme::JS)?; + theme::JS.write_into(&mut js)?; let mut highlight_css = File::create(themedir.join("highlight.css"))?; - highlight_css.write_all(theme::HIGHLIGHT_CSS)?; + theme::HIGHLIGHT_CSS.write_into(&mut highlight_css)?; let mut highlight_js = File::create(themedir.join("highlight.js"))?; highlight_js.write_all(theme::HIGHLIGHT_JS)?; diff --git a/src/front-end/mod.rs b/src/front-end/mod.rs index 8fd09fc2c8..f6cffce18e 100644 --- a/src/front-end/mod.rs +++ b/src/front-end/mod.rs @@ -19,17 +19,24 @@ pub static REDIRECT: &[u8] = include_bytes!("templates/redirect.hbs"); pub static HEADER: &[u8] = include_bytes!("templates/header.hbs"); pub static TOC_JS: &[u8] = include_bytes!("templates/toc.js.hbs"); pub static TOC_HTML: &[u8] = include_bytes!("templates/toc.html.hbs"); -pub static CHROME_CSS: &[u8] = include_bytes!("css/chrome.css"); -pub static GENERAL_CSS: &[u8] = include_bytes!("css/general.css"); -pub static PRINT_CSS: &[u8] = include_bytes!("css/print.css"); -pub static VARIABLES_CSS: &[u8] = include_bytes!("css/variables.css"); +pub static CHROME_CSS: ContentToMinify<'static> = + ContentToMinify::CSS(include_str!("css/chrome.css")); +pub static GENERAL_CSS: ContentToMinify<'static> = + ContentToMinify::CSS(include_str!("css/general.css")); +pub static PRINT_CSS: ContentToMinify<'static> = + ContentToMinify::CSS(include_str!("css/print.css")); +pub static VARIABLES_CSS: ContentToMinify<'static> = + ContentToMinify::CSS(include_str!("css/variables.css")); pub static FAVICON_PNG: &[u8] = include_bytes!("images/favicon.png"); pub static FAVICON_SVG: &[u8] = include_bytes!("images/favicon.svg"); -pub static JS: &[u8] = include_bytes!("js/book.js"); +pub static JS: ContentToMinify<'static> = ContentToMinify::JS(include_str!("js/book.js")); pub static HIGHLIGHT_JS: &[u8] = include_bytes!("js/highlight.js"); -pub static TOMORROW_NIGHT_CSS: &[u8] = include_bytes!("css/tomorrow-night.css"); -pub static HIGHLIGHT_CSS: &[u8] = include_bytes!("css/highlight.css"); -pub static AYU_HIGHLIGHT_CSS: &[u8] = include_bytes!("css/ayu-highlight.css"); +pub static TOMORROW_NIGHT_CSS: ContentToMinify<'static> = + ContentToMinify::CSS(include_str!("css/tomorrow-night.css")); +pub static HIGHLIGHT_CSS: ContentToMinify<'static> = + ContentToMinify::CSS(include_str!("css/highlight.css")); +pub static AYU_HIGHLIGHT_CSS: ContentToMinify<'static> = + ContentToMinify::CSS(include_str!("css/ayu-highlight.css")); pub static CLIPBOARD_JS: &[u8] = include_bytes!("js/clipboard.min.js"); pub static FONT_AWESOME: &[u8] = include_bytes!("css/font-awesome.min.css"); 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. pub static FONT_AWESOME_WOFF2: &[u8] = include_bytes!("fonts/fontawesome-webfont.woff2"); pub static FONT_AWESOME_OTF: &[u8] = include_bytes!("fonts/FontAwesome.otf"); +#[derive(Clone, Copy)] +pub enum ContentToMinify<'a> { + CSS(&'a str), + JS(&'a str), +} + +impl<'a> ContentToMinify<'a> { + pub fn minified(self) -> Vec { + let mut out = Vec::new(); + self.write_into(&mut out).unwrap(); + out + } + + pub fn write_into(self, out: &mut W) -> std::io::Result<()> { + match self { + Self::CSS(data) => match minifier::css::minify(data) { + Ok(data) => return data.write(out), + Err(_) => out.write(data.as_bytes())?, + }, + Self::JS(data) => return minifier::js::minify(data).write(out), + }; + Ok(()) + } +} + /// The `Theme` struct should be used instead of the static variables because /// the `new()` method will look if the user has a theme directory in their /// source folder and use the users theme instead of the default. @@ -181,18 +213,18 @@ impl Default for Theme { header: HEADER.to_owned(), toc_js: TOC_JS.to_owned(), toc_html: TOC_HTML.to_owned(), - chrome_css: CHROME_CSS.to_owned(), - general_css: GENERAL_CSS.to_owned(), - print_css: PRINT_CSS.to_owned(), - variables_css: VARIABLES_CSS.to_owned(), + chrome_css: CHROME_CSS.minified(), + general_css: GENERAL_CSS.minified(), + print_css: PRINT_CSS.minified(), + variables_css: VARIABLES_CSS.minified(), fonts_css: None, font_files: Vec::new(), favicon_png: Some(FAVICON_PNG.to_owned()), favicon_svg: Some(FAVICON_SVG.to_owned()), - js: JS.to_owned(), - highlight_css: HIGHLIGHT_CSS.to_owned(), - tomorrow_night_css: TOMORROW_NIGHT_CSS.to_owned(), - ayu_highlight_css: AYU_HIGHLIGHT_CSS.to_owned(), + js: JS.minified(), + highlight_css: HIGHLIGHT_CSS.minified(), + tomorrow_night_css: TOMORROW_NIGHT_CSS.minified(), + ayu_highlight_css: AYU_HIGHLIGHT_CSS.minified(), highlight_js: HIGHLIGHT_JS.to_owned(), clipboard_js: CLIPBOARD_JS.to_owned(), } diff --git a/src/front-end/searcher/mod.rs b/src/front-end/searcher/mod.rs index d5029db16f..473079b160 100644 --- a/src/front-end/searcher/mod.rs +++ b/src/front-end/searcher/mod.rs @@ -1,6 +1,8 @@ //! Theme dependencies for in-browser search. Not included in mdbook when //! the "search" cargo feature is disabled. -pub static JS: &[u8] = include_bytes!("searcher.js"); +use crate::theme::ContentToMinify; + +pub static JS: ContentToMinify<'static> = ContentToMinify::JS(include_str!("searcher.js")); pub static MARK_JS: &[u8] = include_bytes!("mark.min.js"); pub static ELASTICLUNR_JS: &[u8] = include_bytes!("elasticlunr.min.js"); diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index a144b32b57..96cdd0d389 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -4,7 +4,7 @@ use crate::errors::*; use crate::renderer::html_handlebars::helpers; use crate::renderer::html_handlebars::StaticFiles; use crate::renderer::{RenderContext, Renderer}; -use crate::theme::{self, Theme}; +use crate::theme::{self, ContentToMinify, Theme}; use crate::utils; use std::borrow::Cow; @@ -396,7 +396,8 @@ impl Renderer for HtmlHandlebars { debug!("Render toc js"); { let rendered_toc = handlebars.render("toc_js", &data)?; - static_files.add_builtin("toc.js", rendered_toc.as_bytes()); + let rendered_toc = ContentToMinify::JS(&rendered_toc); + static_files.add_owned_builtin("toc.js", rendered_toc.minified()); debug!("Creating toc.js ✓"); } diff --git a/src/renderer/html_handlebars/search.rs b/src/renderer/html_handlebars/search.rs index 01ef44ac7a..8ccb59a4bf 100644 --- a/src/renderer/html_handlebars/search.rs +++ b/src/renderer/html_handlebars/search.rs @@ -74,7 +74,7 @@ pub fn create_files( ) .as_bytes(), ); - static_files.add_builtin("searcher.js", searcher::JS); + static_files.add_owned_builtin("searcher.js", searcher::JS.minified()); static_files.add_builtin("mark.min.js", searcher::MARK_JS); static_files.add_builtin("elasticlunr.min.js", searcher::ELASTICLUNR_JS); debug!("Copying search files ✓"); diff --git a/src/renderer/html_handlebars/static_files.rs b/src/renderer/html_handlebars/static_files.rs index e1531f4209..394f6eff87 100644 --- a/src/renderer/html_handlebars/static_files.rs +++ b/src/renderer/html_handlebars/static_files.rs @@ -159,6 +159,10 @@ impl StaticFiles { } pub fn add_builtin(&mut self, filename: &str, data: &[u8]) { + self.add_owned_builtin(filename, data.to_owned()); + } + + pub fn add_owned_builtin(&mut self, filename: &str, data: Vec) { self.static_files.push(StaticFile::Builtin { filename: filename.to_owned(), data: data.to_owned(), From 3ef1c65f309eed75760c0a9b5816a61b5f8c9391 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 25 Jun 2025 17:35:13 +0200 Subject: [PATCH 2/2] Add `--disable-minification` option --- src/book/mod.rs | 6 +++ src/cmd/build.rs | 4 ++ src/cmd/command_prelude.rs | 9 +++++ src/cmd/serve.rs | 4 ++ src/cmd/test.rs | 4 ++ src/cmd/watch.rs | 4 ++ src/config.rs | 5 +++ src/front-end/mod.rs | 42 +++++++++++--------- src/renderer/html_handlebars/hbs_renderer.rs | 7 ++-- src/renderer/html_handlebars/search.rs | 3 +- tests/testsuite/book_test.rs | 3 +- tests/testsuite/init.rs | 1 + tests/testsuite/renderer.rs | 7 ++++ 13 files changed, 75 insertions(+), 24 deletions(-) diff --git a/src/book/mod.rs b/src/book/mod.rs index da88767a8d..4922f5599a 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -201,6 +201,12 @@ impl MDBook { Ok(()) } + /// This method is only used by mdbook's tests. + #[doc(hidden)] + pub fn disable_minification(&mut self) { + self.config.build.minification = false; + } + /// Run preprocessors and return the final book. pub fn preprocess_book(&self, renderer: &dyn Renderer) -> Result<(Book, PreprocessorContext)> { let preprocess_ctx = PreprocessorContext::new( diff --git a/src/cmd/build.rs b/src/cmd/build.rs index e40e5c0c72..1df1509cea 100644 --- a/src/cmd/build.rs +++ b/src/cmd/build.rs @@ -10,6 +10,7 @@ pub fn make_subcommand() -> Command { .about("Builds a book from its markdown files") .arg_dest_dir() .arg_root_dir() + .arg_disable_minification() .arg_open() } @@ -21,6 +22,9 @@ pub fn execute(args: &ArgMatches) -> Result<()> { if let Some(dest_dir) = args.get_one::("dest-dir") { book.config.build.build_dir = dest_dir.into(); } + if let Some(true) = args.get_one::("disable-minification") { + book.config.build.minification = false; + } book.build()?; diff --git a/src/cmd/command_prelude.rs b/src/cmd/command_prelude.rs index 3719942598..1cf339dc94 100644 --- a/src/cmd/command_prelude.rs +++ b/src/cmd/command_prelude.rs @@ -37,6 +37,15 @@ pub trait CommandExt: Sized { self._arg(arg!(-o --open "Opens the compiled book in a web browser")) } + fn arg_disable_minification(self) -> Self { + self._arg( + Arg::new("disable-minification") + .long("disable-minification") + .action(clap::ArgAction::SetTrue) + .help("Disable CSS and JS minification"), + ) + } + #[cfg(any(feature = "watch", feature = "serve"))] fn arg_watcher(self) -> Self { #[cfg(feature = "watch")] diff --git a/src/cmd/serve.rs b/src/cmd/serve.rs index 7b1ccab6eb..925ef2b070 100644 --- a/src/cmd/serve.rs +++ b/src/cmd/serve.rs @@ -42,6 +42,7 @@ pub fn make_subcommand() -> Command { .help("Port to use for HTTP connections"), ) .arg_open() + .arg_disable_minification() .arg_watcher() } @@ -63,6 +64,9 @@ pub fn execute(args: &ArgMatches) -> Result<()> { if let Some(dest_dir) = args.get_one::("dest-dir") { book.config.build.build_dir = dest_dir.into(); } + if let Some(true) = args.get_one::("disable-minification") { + book.config.build.minification = false; + } // Override site-url for local serving of the 404 file book.config.set("output.html.site-url", "/").unwrap(); }; diff --git a/src/cmd/test.rs b/src/cmd/test.rs index d41e9ef9eb..0d3619470a 100644 --- a/src/cmd/test.rs +++ b/src/cmd/test.rs @@ -32,6 +32,7 @@ pub fn make_subcommand() -> Command { search path when building tests", ), ) + .arg_disable_minification() } // test command implementation @@ -49,6 +50,9 @@ pub fn execute(args: &ArgMatches) -> Result<()> { if let Some(dest_dir) = args.get_one::("dest-dir") { book.config.build.build_dir = dest_dir.to_path_buf(); } + if let Some(true) = args.get_one::("disable-minification") { + book.config.build.minification = false; + } match chapter { Some(_) => book.test_chapter(library_paths, chapter), None => book.test(library_paths), diff --git a/src/cmd/watch.rs b/src/cmd/watch.rs index 7adb2bbb58..20b93f09ba 100644 --- a/src/cmd/watch.rs +++ b/src/cmd/watch.rs @@ -14,6 +14,7 @@ pub fn make_subcommand() -> Command { .arg_dest_dir() .arg_root_dir() .arg_open() + .arg_disable_minification() .arg_watcher() } @@ -41,6 +42,9 @@ pub fn execute(args: &ArgMatches) -> Result<()> { if let Some(dest_dir) = args.get_one::("dest-dir") { book.config.build.build_dir = dest_dir.into(); } + if let Some(true) = args.get_one::("disable-minification") { + book.config.build.minification = false; + } }; update_config(&mut book); diff --git a/src/config.rs b/src/config.rs index 7ef8bcef12..bdd505ed4e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -496,6 +496,8 @@ pub struct BuildConfig { pub use_default_preprocessors: bool, /// Extra directories to trigger rebuild when watching/serving pub extra_watch_dirs: Vec, + /// Whether or not JS and CSS files are minified. + pub minification: bool, } impl Default for BuildConfig { @@ -505,6 +507,7 @@ impl Default for BuildConfig { create_missing: true, use_default_preprocessors: true, extra_watch_dirs: Vec::new(), + minification: true, } } } @@ -872,6 +875,7 @@ mod tests { create_missing: false, use_default_preprocessors: true, extra_watch_dirs: Vec::new(), + minification: true, }; let rust_should_be = RustConfig { edition: None }; let playground_should_be = Playground { @@ -1083,6 +1087,7 @@ mod tests { create_missing: true, use_default_preprocessors: true, extra_watch_dirs: Vec::new(), + minification: true, }; let html_should_be = HtmlConfig { diff --git a/src/front-end/mod.rs b/src/front-end/mod.rs index f6cffce18e..c4f1f8a1cb 100644 --- a/src/front-end/mod.rs +++ b/src/front-end/mod.rs @@ -53,7 +53,12 @@ pub enum ContentToMinify<'a> { } impl<'a> ContentToMinify<'a> { - pub fn minified(self) -> Vec { + /// If `minification` is false, it simply returns the inner data converted into a `Vec`. + pub fn minified(self, minification: bool) -> Vec { + if !minification { + let (Self::CSS(data) | Self::JS(data)) = self; + return data.as_bytes().to_owned(); + } let mut out = Vec::new(); self.write_into(&mut out).unwrap(); out @@ -104,9 +109,9 @@ pub struct Theme { impl Theme { /// Creates a `Theme` from the given `theme_dir`. /// If a file is found in the theme dir, it will override the default version. - pub fn new>(theme_dir: P) -> Self { + pub fn new>(theme_dir: P, minification: bool) -> Self { let theme_dir = theme_dir.as_ref(); - let mut theme = Theme::default(); + let mut theme = Self::new_with_set_fields(minification); // If the theme directory doesn't exist there's no point continuing... if !theme_dir.exists() || !theme_dir.is_dir() { @@ -202,10 +207,8 @@ impl Theme { theme } -} -impl Default for Theme { - fn default() -> Theme { + fn new_with_set_fields(minification: bool) -> Self { Theme { index: INDEX.to_owned(), head: HEAD.to_owned(), @@ -213,18 +216,18 @@ impl Default for Theme { header: HEADER.to_owned(), toc_js: TOC_JS.to_owned(), toc_html: TOC_HTML.to_owned(), - chrome_css: CHROME_CSS.minified(), - general_css: GENERAL_CSS.minified(), - print_css: PRINT_CSS.minified(), - variables_css: VARIABLES_CSS.minified(), + chrome_css: CHROME_CSS.minified(minification), + general_css: GENERAL_CSS.minified(minification), + print_css: PRINT_CSS.minified(minification), + variables_css: VARIABLES_CSS.minified(minification), fonts_css: None, font_files: Vec::new(), favicon_png: Some(FAVICON_PNG.to_owned()), favicon_svg: Some(FAVICON_SVG.to_owned()), - js: JS.minified(), - highlight_css: HIGHLIGHT_CSS.minified(), - tomorrow_night_css: TOMORROW_NIGHT_CSS.minified(), - ayu_highlight_css: AYU_HIGHLIGHT_CSS.minified(), + js: JS.minified(minification), + highlight_css: HIGHLIGHT_CSS.minified(minification), + tomorrow_night_css: TOMORROW_NIGHT_CSS.minified(minification), + ayu_highlight_css: AYU_HIGHLIGHT_CSS.minified(minification), highlight_js: HIGHLIGHT_JS.to_owned(), clipboard_js: CLIPBOARD_JS.to_owned(), } @@ -258,8 +261,9 @@ mod tests { let non_existent = PathBuf::from("/non/existent/directory/"); assert!(!non_existent.exists()); - let should_be = Theme::default(); - let got = Theme::new(&non_existent); + let minification = false; + let should_be = Theme::new_with_set_fields(minification); + let got = Theme::new(&non_existent, minification); assert_eq!(got, should_be); } @@ -297,7 +301,7 @@ mod tests { File::create(&temp.path().join(file)).unwrap(); } - let got = Theme::new(temp.path()); + let got = Theme::new(temp.path(), false); let empty = Theme { index: Vec::new(), @@ -329,13 +333,13 @@ mod tests { fn favicon_override() { let temp = TempFileBuilder::new().prefix("mdbook-").tempdir().unwrap(); fs::write(temp.path().join("favicon.png"), "1234").unwrap(); - let got = Theme::new(temp.path()); + let got = Theme::new(temp.path(), false); assert_eq!(got.favicon_png.as_ref().unwrap(), b"1234"); assert_eq!(got.favicon_svg, None); let temp = TempFileBuilder::new().prefix("mdbook-").tempdir().unwrap(); fs::write(temp.path().join("favicon.svg"), "4567").unwrap(); - let got = Theme::new(temp.path()); + let got = Theme::new(temp.path(), false); assert_eq!(got.favicon_png, None); assert_eq!(got.favicon_svg.as_ref().unwrap(), b"4567"); } diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 96cdd0d389..ff9507a3cd 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -330,6 +330,7 @@ impl Renderer for HtmlHandlebars { let destination = &ctx.destination; let book = &ctx.book; let build_dir = ctx.root.join(&ctx.config.build.build_dir); + let minification = ctx.config.build.minification; if destination.exists() { utils::fs::remove_dir_content(destination) @@ -350,7 +351,7 @@ impl Renderer for HtmlHandlebars { None => ctx.root.join("theme"), }; - let theme = theme::Theme::new(theme_dir); + let theme = theme::Theme::new(theme_dir, minification); debug!("Register the index handlebars template"); handlebars.register_template_string("index", String::from_utf8(theme.index.clone())?)?; @@ -389,7 +390,7 @@ impl Renderer for HtmlHandlebars { let default = crate::config::Search::default(); let search = html_config.search.as_ref().unwrap_or(&default); if search.enable { - super::search::create_files(&search, &mut static_files, &book)?; + super::search::create_files(&search, &mut static_files, &book, minification)?; } } @@ -397,7 +398,7 @@ impl Renderer for HtmlHandlebars { { let rendered_toc = handlebars.render("toc_js", &data)?; let rendered_toc = ContentToMinify::JS(&rendered_toc); - static_files.add_owned_builtin("toc.js", rendered_toc.minified()); + static_files.add_owned_builtin("toc.js", rendered_toc.minified(minification)); debug!("Creating toc.js ✓"); } diff --git a/src/renderer/html_handlebars/search.rs b/src/renderer/html_handlebars/search.rs index 8ccb59a4bf..5236226db7 100644 --- a/src/renderer/html_handlebars/search.rs +++ b/src/renderer/html_handlebars/search.rs @@ -31,6 +31,7 @@ pub fn create_files( search_config: &Search, static_files: &mut StaticFiles, book: &Book, + minification: bool, ) -> Result<()> { let mut index = IndexBuilder::new() .add_field_with_tokenizer("title", Box::new(&tokenize)) @@ -74,7 +75,7 @@ pub fn create_files( ) .as_bytes(), ); - static_files.add_owned_builtin("searcher.js", searcher::JS.minified()); + static_files.add_owned_builtin("searcher.js", searcher::JS.minified(minification)); static_files.add_builtin("mark.min.js", searcher::MARK_JS); static_files.add_builtin("elasticlunr.min.js", searcher::ELASTICLUNR_JS); debug!("Copying search files ✓"); diff --git a/tests/testsuite/book_test.rs b/tests/testsuite/book_test.rs index 427c38d18a..1e39af480c 100644 --- a/tests/testsuite/book_test.rs +++ b/tests/testsuite/book_test.rs @@ -205,7 +205,8 @@ impl BookTest { /// Builds the book in the temp directory. pub fn build(&mut self) -> &mut Self { - let book = self.load_book(); + let mut book = self.load_book(); + book.disable_minification(); book.build() .unwrap_or_else(|e| panic!("book failed to build: {e:?}")); self.built = true; diff --git a/tests/testsuite/init.rs b/tests/testsuite/init.rs index e5989eb62c..73a63c108e 100644 --- a/tests/testsuite/init.rs +++ b/tests/testsuite/init.rs @@ -185,6 +185,7 @@ src = "in" build-dir = "out" create-missing = true extra-watch-dirs = [] +minification = true use-default-preprocessors = true "#]], diff --git a/tests/testsuite/renderer.rs b/tests/testsuite/renderer.rs index 1e1624474f..d7047681be 100644 --- a/tests/testsuite/renderer.rs +++ b/tests/testsuite/renderer.rs @@ -190,6 +190,13 @@ fn backends_receive_render_context_via_stdin() { "language": "en", "src": "src" }, + "build": { + "build-dir": "book", + "create-missing": true, + "extra-watch-dirs": [], + "minification": false, + "use-default-preprocessors": true + }, "output": { "cat-to-file": { "command": "./cat-to-file"