@@ -76,6 +76,8 @@ use html::item_type::ItemType;
76
76
use html:: markdown:: { self , Markdown , MarkdownHtml , MarkdownSummaryLine } ;
77
77
use html:: { highlight, layout} ;
78
78
79
+ use minifier;
80
+
79
81
/// A pair of name and its optional document.
80
82
pub type NameDoc = ( String , Option < String > ) ;
81
83
@@ -509,7 +511,8 @@ pub fn run(mut krate: clean::Crate,
509
511
css_file_extension : Option < PathBuf > ,
510
512
renderinfo : RenderInfo ,
511
513
sort_modules_alphabetically : bool ,
512
- themes : Vec < PathBuf > ) -> Result < ( ) , Error > {
514
+ themes : Vec < PathBuf > ,
515
+ enable_minification : bool ) -> Result < ( ) , Error > {
513
516
let src_root = match krate. src {
514
517
FileName :: Real ( ref p) => match p. parent ( ) {
515
518
Some ( p) => p. to_path_buf ( ) ,
@@ -661,7 +664,7 @@ pub fn run(mut krate: clean::Crate,
661
664
CACHE_KEY . with ( |v| * v. borrow_mut ( ) = cache. clone ( ) ) ;
662
665
CURRENT_LOCATION_KEY . with ( |s| s. borrow_mut ( ) . clear ( ) ) ;
663
666
664
- write_shared ( & cx, & krate, & * cache, index) ?;
667
+ write_shared ( & cx, & krate, & * cache, index, enable_minification ) ?;
665
668
666
669
// And finally render the whole crate's documentation
667
670
cx. krate ( krate)
@@ -740,7 +743,8 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
740
743
fn write_shared ( cx : & Context ,
741
744
krate : & clean:: Crate ,
742
745
cache : & Cache ,
743
- search_index : String ) -> Result < ( ) , Error > {
746
+ search_index : String ,
747
+ enable_minification : bool ) -> Result < ( ) , Error > {
744
748
// Write out the shared files. Note that these are shared among all rustdoc
745
749
// docs placed in the output directory, so this needs to be a synchronized
746
750
// operation with respect to all other rustdocs running around.
@@ -814,16 +818,20 @@ themePicker.onclick = function() {{
814
818
. join( "," ) ) . as_bytes ( ) ,
815
819
) ?;
816
820
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) ?;
821
827
822
828
{
823
829
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) ?;
827
835
}
828
836
829
837
if let Some ( ref css) = cx. shared . css_file_extension {
@@ -1020,6 +1028,14 @@ fn write(dst: PathBuf, contents: &[u8]) -> Result<(), Error> {
1020
1028
Ok ( try_err ! ( fs:: write( & dst, contents) , & dst) )
1021
1029
}
1022
1030
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
+
1023
1039
/// Takes a path to a source file and cleans the path to it. This canonicalizes
1024
1040
/// things like ".." to components which preserve the "top down" hierarchy of a
1025
1041
/// static HTML tree. Each component in the cleaned path will be passed as an
0 commit comments