Skip to content

Commit

Permalink
Add minification process
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed May 12, 2018
1 parent c8a3ec1 commit 4b14573
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 18 deletions.
20 changes: 15 additions & 5 deletions src/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/librustdoc/Cargo.toml
Expand Up @@ -10,3 +10,4 @@ path = "lib.rs"
[dependencies]
pulldown-cmark = { version = "0.1.2", default-features = false }
tempdir = "0.3"
minifier = "0.0.11"
36 changes: 26 additions & 10 deletions src/librustdoc/html/render.rs
Expand Up @@ -76,6 +76,8 @@ use html::item_type::ItemType;
use html::markdown::{self, Markdown, MarkdownHtml, MarkdownSummaryLine};
use html::{highlight, layout};

use minifier;

/// A pair of name and its optional document.
pub type NameDoc = (String, Option<String>);

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

write_shared(&cx, &krate, &*cache, index)?;
write_shared(&cx, &krate, &*cache, index, enable_minification)?;

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

write(cx.dst.join(&format!("main{}.js", cx.shared.resource_suffix)),
include_bytes!("static/main.js"))?;
write(cx.dst.join(&format!("settings{}.js", cx.shared.resource_suffix)),
include_bytes!("static/settings.js"))?;
write_minify(cx.dst.join(&format!("main{}.js", cx.shared.resource_suffix)),
include_str!("static/main.js"),
enable_minification)?;
write_minify(cx.dst.join(&format!("settings{}.js", cx.shared.resource_suffix)),
include_str!("static/settings.js"),
enable_minification)?;

{
let mut data = format!("var resourcesSuffix = \"{}\";\n",
cx.shared.resource_suffix).into_bytes();
data.extend_from_slice(include_bytes!("static/storage.js"));
write(cx.dst.join(&format!("storage{}.js", cx.shared.resource_suffix)), &data)?;
cx.shared.resource_suffix);
data.push_str(include_str!("static/storage.js"));
write_minify(cx.dst.join(&format!("storage{}.js", cx.shared.resource_suffix)),
&data,
enable_minification)?;
}

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

fn write_minify(dst: PathBuf, contents: &str, enable_minification: bool) -> Result<(), Error> {
if enable_minification {
write(dst, minifier::js::minify(contents).as_bytes())
} else {
write(dst, contents.as_bytes())
}
}

/// Takes a path to a source file and cleans the path to it. This canonicalizes
/// things like ".." to components which preserve the "top down" hierarchy of a
/// static HTML tree. Each component in the cleaned path will be passed as an
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/html/static/main.js
Expand Up @@ -196,7 +196,7 @@
onEach(e.getElementsByTagName('span'), function(i_e) {
removeClass(i_e, 'line-highlighted');
});
})
});
for (i = from; i <= to; ++i) {
addClass(document.getElementById(i), 'line-highlighted');
}
Expand Down Expand Up @@ -1944,7 +1944,7 @@
hasClass(next.nextElementSibling, 'docblock')))) {
insertAfter(toggle.cloneNode(true), e.childNodes[e.childNodes.length - 1]);
}
}
};
onEach(document.getElementsByClassName('method'), func);
onEach(document.getElementsByClassName('impl'), func);
onEach(document.getElementsByClassName('impl-items'), function(e) {
Expand Down
10 changes: 9 additions & 1 deletion src/librustdoc/lib.rs
Expand Up @@ -46,6 +46,7 @@ extern crate test as testing;
extern crate rustc_errors as errors;
extern crate pulldown_cmark;
extern crate tempdir;
extern crate minifier;

extern crate serialize as rustc_serialize; // used by deriving

Expand Down Expand Up @@ -297,6 +298,11 @@ pub fn opts() -> Vec<RustcOptGroup> {
"How errors and other messages are produced",
"human|json|short")
}),
unstable("disable-minification", |o| {
o.optflag("",
"disable-minification",
"Disable minification applied on JS files")
}),
]
}

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

let edition = matches.opt_str("edition").unwrap_or("2015".to_string());
let edition = match edition.parse() {
Expand Down Expand Up @@ -521,7 +528,8 @@ pub fn main_args(args: &[String]) -> isize {
css_file_extension,
renderinfo,
sort_modules_alphabetically,
themes)
themes,
enable_minification)
.expect("failed to generate documentation");
0
}
Expand Down

0 comments on commit 4b14573

Please sign in to comment.