Skip to content

Commit e7541ca

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

File tree

9 files changed

+67
-36
lines changed

9 files changed

+67
-36
lines changed

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: 31 additions & 32 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,30 @@ 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 = Theme {
115+
index: INDEX.to_owned(),
116+
head: HEAD.to_owned(),
117+
redirect: REDIRECT.to_owned(),
118+
header: HEADER.to_owned(),
119+
toc_js: TOC_JS.to_owned(),
120+
toc_html: TOC_HTML.to_owned(),
121+
chrome_css: CHROME_CSS.minified(minification),
122+
general_css: GENERAL_CSS.minified(minification),
123+
print_css: PRINT_CSS.minified(minification),
124+
variables_css: VARIABLES_CSS.minified(minification),
125+
fonts_css: None,
126+
font_files: Vec::new(),
127+
favicon_png: Some(FAVICON_PNG.to_owned()),
128+
favicon_svg: Some(FAVICON_SVG.to_owned()),
129+
js: JS.minified(minification),
130+
highlight_css: HIGHLIGHT_CSS.minified(minification),
131+
tomorrow_night_css: TOMORROW_NIGHT_CSS.minified(minification),
132+
ayu_highlight_css: AYU_HIGHLIGHT_CSS.minified(minification),
133+
highlight_js: HIGHLIGHT_JS.to_owned(),
134+
clipboard_js: CLIPBOARD_JS.to_owned(),
135+
};
110136

111137
// If the theme directory doesn't exist there's no point continuing...
112138
if !theme_dir.exists() || !theme_dir.is_dir() {
@@ -204,33 +230,6 @@ impl Theme {
204230
}
205231
}
206232

207-
impl Default for Theme {
208-
fn default() -> Theme {
209-
Theme {
210-
index: INDEX.to_owned(),
211-
head: HEAD.to_owned(),
212-
redirect: REDIRECT.to_owned(),
213-
header: HEADER.to_owned(),
214-
toc_js: TOC_JS.to_owned(),
215-
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(),
220-
fonts_css: None,
221-
font_files: Vec::new(),
222-
favicon_png: Some(FAVICON_PNG.to_owned()),
223-
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(),
228-
highlight_js: HIGHLIGHT_JS.to_owned(),
229-
clipboard_js: CLIPBOARD_JS.to_owned(),
230-
}
231-
}
232-
}
233-
234233
/// Checks if a file exists, if so, the destination buffer will be filled with
235234
/// its contents.
236235
fn load_file_contents<P: AsRef<Path>>(filename: P, dest: &mut Vec<u8>) -> Result<()> {
@@ -329,13 +328,13 @@ mod tests {
329328
fn favicon_override() {
330329
let temp = TempFileBuilder::new().prefix("mdbook-").tempdir().unwrap();
331330
fs::write(temp.path().join("favicon.png"), "1234").unwrap();
332-
let got = Theme::new(temp.path());
331+
let got = Theme::new(temp.path(), false);
333332
assert_eq!(got.favicon_png.as_ref().unwrap(), b"1234");
334333
assert_eq!(got.favicon_svg, None);
335334

336335
let temp = TempFileBuilder::new().prefix("mdbook-").tempdir().unwrap();
337336
fs::write(temp.path().join("favicon.svg"), "4567").unwrap();
338-
let got = Theme::new(temp.path());
337+
let got = Theme::new(temp.path(), false);
339338
assert_eq!(got.favicon_png, None);
340339
assert_eq!(got.favicon_svg.as_ref().unwrap(), b"4567");
341340
}

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 ✓");

0 commit comments

Comments
 (0)