Skip to content

Commit 3ef1c65

Browse files
Add --disable-minification option
1 parent 4641a5b commit 3ef1c65

File tree

13 files changed

+75
-24
lines changed

13 files changed

+75
-24
lines changed

src/book/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,12 @@ impl MDBook {
201201
Ok(())
202202
}
203203

204+
/// This method is only used by mdbook's tests.
205+
#[doc(hidden)]
206+
pub fn disable_minification(&mut self) {
207+
self.config.build.minification = false;
208+
}
209+
204210
/// Run preprocessors and return the final book.
205211
pub fn preprocess_book(&self, renderer: &dyn Renderer) -> Result<(Book, PreprocessorContext)> {
206212
let preprocess_ctx = PreprocessorContext::new(

src/cmd/build.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub fn make_subcommand() -> Command {
1010
.about("Builds a book from its markdown files")
1111
.arg_dest_dir()
1212
.arg_root_dir()
13+
.arg_disable_minification()
1314
.arg_open()
1415
}
1516

@@ -21,6 +22,9 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
2122
if let Some(dest_dir) = args.get_one::<PathBuf>("dest-dir") {
2223
book.config.build.build_dir = dest_dir.into();
2324
}
25+
if let Some(true) = args.get_one::<bool>("disable-minification") {
26+
book.config.build.minification = false;
27+
}
2428

2529
book.build()?;
2630

src/cmd/command_prelude.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ pub trait CommandExt: Sized {
3737
self._arg(arg!(-o --open "Opens the compiled book in a web browser"))
3838
}
3939

40+
fn arg_disable_minification(self) -> Self {
41+
self._arg(
42+
Arg::new("disable-minification")
43+
.long("disable-minification")
44+
.action(clap::ArgAction::SetTrue)
45+
.help("Disable CSS and JS minification"),
46+
)
47+
}
48+
4049
#[cfg(any(feature = "watch", feature = "serve"))]
4150
fn arg_watcher(self) -> Self {
4251
#[cfg(feature = "watch")]

src/cmd/serve.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub fn make_subcommand() -> Command {
4242
.help("Port to use for HTTP connections"),
4343
)
4444
.arg_open()
45+
.arg_disable_minification()
4546
.arg_watcher()
4647
}
4748

@@ -63,6 +64,9 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
6364
if let Some(dest_dir) = args.get_one::<PathBuf>("dest-dir") {
6465
book.config.build.build_dir = dest_dir.into();
6566
}
67+
if let Some(true) = args.get_one::<bool>("disable-minification") {
68+
book.config.build.minification = false;
69+
}
6670
// Override site-url for local serving of the 404 file
6771
book.config.set("output.html.site-url", "/").unwrap();
6872
};

src/cmd/test.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub fn make_subcommand() -> Command {
3232
search path when building tests",
3333
),
3434
)
35+
.arg_disable_minification()
3536
}
3637

3738
// test command implementation
@@ -49,6 +50,9 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
4950
if let Some(dest_dir) = args.get_one::<PathBuf>("dest-dir") {
5051
book.config.build.build_dir = dest_dir.to_path_buf();
5152
}
53+
if let Some(true) = args.get_one::<bool>("disable-minification") {
54+
book.config.build.minification = false;
55+
}
5256
match chapter {
5357
Some(_) => book.test_chapter(library_paths, chapter),
5458
None => book.test(library_paths),

src/cmd/watch.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub fn make_subcommand() -> Command {
1414
.arg_dest_dir()
1515
.arg_root_dir()
1616
.arg_open()
17+
.arg_disable_minification()
1718
.arg_watcher()
1819
}
1920

@@ -41,6 +42,9 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
4142
if let Some(dest_dir) = args.get_one::<PathBuf>("dest-dir") {
4243
book.config.build.build_dir = dest_dir.into();
4344
}
45+
if let Some(true) = args.get_one::<bool>("disable-minification") {
46+
book.config.build.minification = false;
47+
}
4448
};
4549
update_config(&mut book);
4650

src/config.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,8 @@ pub struct BuildConfig {
496496
pub use_default_preprocessors: bool,
497497
/// Extra directories to trigger rebuild when watching/serving
498498
pub extra_watch_dirs: Vec<PathBuf>,
499+
/// Whether or not JS and CSS files are minified.
500+
pub minification: bool,
499501
}
500502

501503
impl Default for BuildConfig {
@@ -505,6 +507,7 @@ impl Default for BuildConfig {
505507
create_missing: true,
506508
use_default_preprocessors: true,
507509
extra_watch_dirs: Vec::new(),
510+
minification: true,
508511
}
509512
}
510513
}
@@ -872,6 +875,7 @@ mod tests {
872875
create_missing: false,
873876
use_default_preprocessors: true,
874877
extra_watch_dirs: Vec::new(),
878+
minification: true,
875879
};
876880
let rust_should_be = RustConfig { edition: None };
877881
let playground_should_be = Playground {
@@ -1083,6 +1087,7 @@ mod tests {
10831087
create_missing: true,
10841088
use_default_preprocessors: true,
10851089
extra_watch_dirs: Vec::new(),
1090+
minification: true,
10861091
};
10871092

10881093
let html_should_be = HtmlConfig {

src/front-end/mod.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ pub enum ContentToMinify<'a> {
5353
}
5454

5555
impl<'a> ContentToMinify<'a> {
56-
pub fn minified(self) -> Vec<u8> {
56+
/// If `minification` is false, it simply returns the inner data converted into a `Vec`.
57+
pub fn minified(self, minification: bool) -> Vec<u8> {
58+
if !minification {
59+
let (Self::CSS(data) | Self::JS(data)) = self;
60+
return data.as_bytes().to_owned();
61+
}
5762
let mut out = Vec::new();
5863
self.write_into(&mut out).unwrap();
5964
out
@@ -104,9 +109,9 @@ pub struct Theme {
104109
impl Theme {
105110
/// Creates a `Theme` from the given `theme_dir`.
106111
/// If a file is found in the theme dir, it will override the default version.
107-
pub fn new<P: AsRef<Path>>(theme_dir: P) -> Self {
112+
pub fn new<P: AsRef<Path>>(theme_dir: P, minification: bool) -> Self {
108113
let theme_dir = theme_dir.as_ref();
109-
let mut theme = Theme::default();
114+
let mut theme = Self::new_with_set_fields(minification);
110115

111116
// If the theme directory doesn't exist there's no point continuing...
112117
if !theme_dir.exists() || !theme_dir.is_dir() {
@@ -202,29 +207,27 @@ impl Theme {
202207

203208
theme
204209
}
205-
}
206210

207-
impl Default for Theme {
208-
fn default() -> Theme {
211+
fn new_with_set_fields(minification: bool) -> Self {
209212
Theme {
210213
index: INDEX.to_owned(),
211214
head: HEAD.to_owned(),
212215
redirect: REDIRECT.to_owned(),
213216
header: HEADER.to_owned(),
214217
toc_js: TOC_JS.to_owned(),
215218
toc_html: TOC_HTML.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(),
219+
chrome_css: CHROME_CSS.minified(minification),
220+
general_css: GENERAL_CSS.minified(minification),
221+
print_css: PRINT_CSS.minified(minification),
222+
variables_css: VARIABLES_CSS.minified(minification),
220223
fonts_css: None,
221224
font_files: Vec::new(),
222225
favicon_png: Some(FAVICON_PNG.to_owned()),
223226
favicon_svg: Some(FAVICON_SVG.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(),
227+
js: JS.minified(minification),
228+
highlight_css: HIGHLIGHT_CSS.minified(minification),
229+
tomorrow_night_css: TOMORROW_NIGHT_CSS.minified(minification),
230+
ayu_highlight_css: AYU_HIGHLIGHT_CSS.minified(minification),
228231
highlight_js: HIGHLIGHT_JS.to_owned(),
229232
clipboard_js: CLIPBOARD_JS.to_owned(),
230233
}
@@ -258,8 +261,9 @@ mod tests {
258261
let non_existent = PathBuf::from("/non/existent/directory/");
259262
assert!(!non_existent.exists());
260263

261-
let should_be = Theme::default();
262-
let got = Theme::new(&non_existent);
264+
let minification = false;
265+
let should_be = Theme::new_with_set_fields(minification);
266+
let got = Theme::new(&non_existent, minification);
263267

264268
assert_eq!(got, should_be);
265269
}
@@ -297,7 +301,7 @@ mod tests {
297301
File::create(&temp.path().join(file)).unwrap();
298302
}
299303

300-
let got = Theme::new(temp.path());
304+
let got = Theme::new(temp.path(), false);
301305

302306
let empty = Theme {
303307
index: Vec::new(),
@@ -329,13 +333,13 @@ mod tests {
329333
fn favicon_override() {
330334
let temp = TempFileBuilder::new().prefix("mdbook-").tempdir().unwrap();
331335
fs::write(temp.path().join("favicon.png"), "1234").unwrap();
332-
let got = Theme::new(temp.path());
336+
let got = Theme::new(temp.path(), false);
333337
assert_eq!(got.favicon_png.as_ref().unwrap(), b"1234");
334338
assert_eq!(got.favicon_svg, None);
335339

336340
let temp = TempFileBuilder::new().prefix("mdbook-").tempdir().unwrap();
337341
fs::write(temp.path().join("favicon.svg"), "4567").unwrap();
338-
let got = Theme::new(temp.path());
342+
let got = Theme::new(temp.path(), false);
339343
assert_eq!(got.favicon_png, None);
340344
assert_eq!(got.favicon_svg.as_ref().unwrap(), b"4567");
341345
}

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ impl Renderer for HtmlHandlebars {
330330
let destination = &ctx.destination;
331331
let book = &ctx.book;
332332
let build_dir = ctx.root.join(&ctx.config.build.build_dir);
333+
let minification = ctx.config.build.minification;
333334

334335
if destination.exists() {
335336
utils::fs::remove_dir_content(destination)
@@ -350,7 +351,7 @@ impl Renderer for HtmlHandlebars {
350351
None => ctx.root.join("theme"),
351352
};
352353

353-
let theme = theme::Theme::new(theme_dir);
354+
let theme = theme::Theme::new(theme_dir, minification);
354355

355356
debug!("Register the index handlebars template");
356357
handlebars.register_template_string("index", String::from_utf8(theme.index.clone())?)?;
@@ -389,15 +390,15 @@ impl Renderer for HtmlHandlebars {
389390
let default = crate::config::Search::default();
390391
let search = html_config.search.as_ref().unwrap_or(&default);
391392
if search.enable {
392-
super::search::create_files(&search, &mut static_files, &book)?;
393+
super::search::create_files(&search, &mut static_files, &book, minification)?;
393394
}
394395
}
395396

396397
debug!("Render toc js");
397398
{
398399
let rendered_toc = handlebars.render("toc_js", &data)?;
399400
let rendered_toc = ContentToMinify::JS(&rendered_toc);
400-
static_files.add_owned_builtin("toc.js", rendered_toc.minified());
401+
static_files.add_owned_builtin("toc.js", rendered_toc.minified(minification));
401402
debug!("Creating toc.js ✓");
402403
}
403404

src/renderer/html_handlebars/search.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub fn create_files(
3131
search_config: &Search,
3232
static_files: &mut StaticFiles,
3333
book: &Book,
34+
minification: bool,
3435
) -> Result<()> {
3536
let mut index = IndexBuilder::new()
3637
.add_field_with_tokenizer("title", Box::new(&tokenize))
@@ -74,7 +75,7 @@ pub fn create_files(
7475
)
7576
.as_bytes(),
7677
);
77-
static_files.add_owned_builtin("searcher.js", searcher::JS.minified());
78+
static_files.add_owned_builtin("searcher.js", searcher::JS.minified(minification));
7879
static_files.add_builtin("mark.min.js", searcher::MARK_JS);
7980
static_files.add_builtin("elasticlunr.min.js", searcher::ELASTICLUNR_JS);
8081
debug!("Copying search files ✓");

tests/testsuite/book_test.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,8 @@ impl BookTest {
205205

206206
/// Builds the book in the temp directory.
207207
pub fn build(&mut self) -> &mut Self {
208-
let book = self.load_book();
208+
let mut book = self.load_book();
209+
book.disable_minification();
209210
book.build()
210211
.unwrap_or_else(|e| panic!("book failed to build: {e:?}"));
211212
self.built = true;

tests/testsuite/init.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ src = "in"
185185
build-dir = "out"
186186
create-missing = true
187187
extra-watch-dirs = []
188+
minification = true
188189
use-default-preprocessors = true
189190
190191
"#]],

tests/testsuite/renderer.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,13 @@ fn backends_receive_render_context_via_stdin() {
190190
"language": "en",
191191
"src": "src"
192192
},
193+
"build": {
194+
"build-dir": "book",
195+
"create-missing": true,
196+
"extra-watch-dirs": [],
197+
"minification": false,
198+
"use-default-preprocessors": true
199+
},
193200
"output": {
194201
"cat-to-file": {
195202
"command": "./cat-to-file"

0 commit comments

Comments
 (0)