Skip to content

Commit 4b14573

Browse files
Add minification process
1 parent c8a3ec1 commit 4b14573

File tree

5 files changed

+53
-18
lines changed

5 files changed

+53
-18
lines changed

src/Cargo.lock

+15-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/librustdoc/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ path = "lib.rs"
1010
[dependencies]
1111
pulldown-cmark = { version = "0.1.2", default-features = false }
1212
tempdir = "0.3"
13+
minifier = "0.0.11"

src/librustdoc/html/render.rs

+26-10
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ use html::item_type::ItemType;
7676
use html::markdown::{self, Markdown, MarkdownHtml, MarkdownSummaryLine};
7777
use html::{highlight, layout};
7878

79+
use minifier;
80+
7981
/// A pair of name and its optional document.
8082
pub type NameDoc = (String, Option<String>);
8183

@@ -509,7 +511,8 @@ pub fn run(mut krate: clean::Crate,
509511
css_file_extension: Option<PathBuf>,
510512
renderinfo: RenderInfo,
511513
sort_modules_alphabetically: bool,
512-
themes: Vec<PathBuf>) -> Result<(), Error> {
514+
themes: Vec<PathBuf>,
515+
enable_minification: bool) -> Result<(), Error> {
513516
let src_root = match krate.src {
514517
FileName::Real(ref p) => match p.parent() {
515518
Some(p) => p.to_path_buf(),
@@ -661,7 +664,7 @@ pub fn run(mut krate: clean::Crate,
661664
CACHE_KEY.with(|v| *v.borrow_mut() = cache.clone());
662665
CURRENT_LOCATION_KEY.with(|s| s.borrow_mut().clear());
663666

664-
write_shared(&cx, &krate, &*cache, index)?;
667+
write_shared(&cx, &krate, &*cache, index, enable_minification)?;
665668

666669
// And finally render the whole crate's documentation
667670
cx.krate(krate)
@@ -740,7 +743,8 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
740743
fn write_shared(cx: &Context,
741744
krate: &clean::Crate,
742745
cache: &Cache,
743-
search_index: String) -> Result<(), Error> {
746+
search_index: String,
747+
enable_minification: bool) -> Result<(), Error> {
744748
// Write out the shared files. Note that these are shared among all rustdoc
745749
// docs placed in the output directory, so this needs to be a synchronized
746750
// operation with respect to all other rustdocs running around.
@@ -814,16 +818,20 @@ themePicker.onclick = function() {{
814818
.join(",")).as_bytes(),
815819
)?;
816820

817-
write(cx.dst.join(&format!("main{}.js", cx.shared.resource_suffix)),
818-
include_bytes!("static/main.js"))?;
819-
write(cx.dst.join(&format!("settings{}.js", cx.shared.resource_suffix)),
820-
include_bytes!("static/settings.js"))?;
821+
write_minify(cx.dst.join(&format!("main{}.js", cx.shared.resource_suffix)),
822+
include_str!("static/main.js"),
823+
enable_minification)?;
824+
write_minify(cx.dst.join(&format!("settings{}.js", cx.shared.resource_suffix)),
825+
include_str!("static/settings.js"),
826+
enable_minification)?;
821827

822828
{
823829
let mut data = format!("var resourcesSuffix = \"{}\";\n",
824-
cx.shared.resource_suffix).into_bytes();
825-
data.extend_from_slice(include_bytes!("static/storage.js"));
826-
write(cx.dst.join(&format!("storage{}.js", cx.shared.resource_suffix)), &data)?;
830+
cx.shared.resource_suffix);
831+
data.push_str(include_str!("static/storage.js"));
832+
write_minify(cx.dst.join(&format!("storage{}.js", cx.shared.resource_suffix)),
833+
&data,
834+
enable_minification)?;
827835
}
828836

829837
if let Some(ref css) = cx.shared.css_file_extension {
@@ -1020,6 +1028,14 @@ fn write(dst: PathBuf, contents: &[u8]) -> Result<(), Error> {
10201028
Ok(try_err!(fs::write(&dst, contents), &dst))
10211029
}
10221030

1031+
fn write_minify(dst: PathBuf, contents: &str, enable_minification: bool) -> Result<(), Error> {
1032+
if enable_minification {
1033+
write(dst, minifier::js::minify(contents).as_bytes())
1034+
} else {
1035+
write(dst, contents.as_bytes())
1036+
}
1037+
}
1038+
10231039
/// Takes a path to a source file and cleans the path to it. This canonicalizes
10241040
/// things like ".." to components which preserve the "top down" hierarchy of a
10251041
/// static HTML tree. Each component in the cleaned path will be passed as an

src/librustdoc/html/static/main.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@
196196
onEach(e.getElementsByTagName('span'), function(i_e) {
197197
removeClass(i_e, 'line-highlighted');
198198
});
199-
})
199+
});
200200
for (i = from; i <= to; ++i) {
201201
addClass(document.getElementById(i), 'line-highlighted');
202202
}
@@ -1944,7 +1944,7 @@
19441944
hasClass(next.nextElementSibling, 'docblock')))) {
19451945
insertAfter(toggle.cloneNode(true), e.childNodes[e.childNodes.length - 1]);
19461946
}
1947-
}
1947+
};
19481948
onEach(document.getElementsByClassName('method'), func);
19491949
onEach(document.getElementsByClassName('impl'), func);
19501950
onEach(document.getElementsByClassName('impl-items'), function(e) {

src/librustdoc/lib.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ extern crate test as testing;
4646
extern crate rustc_errors as errors;
4747
extern crate pulldown_cmark;
4848
extern crate tempdir;
49+
extern crate minifier;
4950

5051
extern crate serialize as rustc_serialize; // used by deriving
5152

@@ -297,6 +298,11 @@ pub fn opts() -> Vec<RustcOptGroup> {
297298
"How errors and other messages are produced",
298299
"human|json|short")
299300
}),
301+
unstable("disable-minification", |o| {
302+
o.optflag("",
303+
"disable-minification",
304+
"Disable minification applied on JS files")
305+
}),
300306
]
301307
}
302308

@@ -478,6 +484,7 @@ pub fn main_args(args: &[String]) -> isize {
478484
let linker = matches.opt_str("linker").map(PathBuf::from);
479485
let sort_modules_alphabetically = !matches.opt_present("sort-modules-by-appearance");
480486
let resource_suffix = matches.opt_str("resource-suffix");
487+
let enable_minification = !matches.opt_present("disable-minification");
481488

482489
let edition = matches.opt_str("edition").unwrap_or("2015".to_string());
483490
let edition = match edition.parse() {
@@ -521,7 +528,8 @@ pub fn main_args(args: &[String]) -> isize {
521528
css_file_extension,
522529
renderinfo,
523530
sort_modules_alphabetically,
524-
themes)
531+
themes,
532+
enable_minification)
525533
.expect("failed to generate documentation");
526534
0
527535
}

0 commit comments

Comments
 (0)