macray / htmlcompressor

Automatically exported from code.google.com/p/htmlcompressor
Apache License 2.0
0 stars 0 forks source link

when I run the closure compress from htmlcompressor it rename the tag value, checked, etc #39

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. The script is compressed but, rename the tags value, checked, etc
2. If I put the same javascript in a file and use closure compress, it maintain 
the tags name.
3. The compilation_level is ADVANCED_OPTIMIZATIONS.

The version of htmlcompressor is 1.3, and the closure is 1.2.

What is the expected output? What do you see instead?

  o("i18").value = parseInt(c[3], 10);
  o("i13").value = parseInt(c[4], 10);

and do:
   o("i18").a=parseInt(c[3],10);
   o("i13").a=parseInt(c[4],10);

The OS version is Ubuntu 10.04

Please provide any additional information below.

Original issue reported on code.google.com by juanschi...@gmail.com on 25 Apr 2011 at 3:14

GoogleCodeExporter commented 9 years ago
As it says in the docs using ADVANCED_OPTIMIZATIONS is not recommended because 
it is  most likely will break javascript. If you are using command line 
compressor then this optimization level should be almost always avoided.

The only way of using this optimization level properly would be to run HTML 
compressor from Java and fine tune ClosureJavaScriptCompressor configuration to 
your specific needs by providing a list of externs and setting proper 
compilation options.

If you don't want to go through all this then please use SIMPLE_OPTIMIZATIONS 
level instead.

Please see:
http://code.google.com/closure/compiler/docs/api-tutorial3.html
http://htmlcompressor.googlecode.com/svn/trunk/doc/com/googlecode/htmlcompressor
/compressor/ClosureJavaScriptCompressor.html

Original comment by serg472@gmail.com on 25 Apr 2011 at 5:40

GoogleCodeExporter commented 9 years ago
Hi serg472,

thanks for answering, can you explain me how is passed the script of and html 
page to the closure compressor?

Best Regards

Original comment by juanschi...@gmail.com on 25 Apr 2011 at 7:24

GoogleCodeExporter commented 9 years ago
Each inline <script> tag is compressed individually and independently using 
closure or yui compressors. 

Compressing inline javascript is kind of a bad idea to begin with. The better 
solution would be to move as much of your inline javascript into external js 
file as possible and then compress that file instead (using closure or 
whatever). It will make javascript compressor more efficient as it sees the 
whole picture, your javascript will be cached by the browser, and it will make 
html compression much faster as compressing inline js takes the longest time by 
far.

If you feel the need of advanced closure compilation then I think it is a good 
sign that you need to move your js into an external file.

Original comment by serg472@gmail.com on 25 Apr 2011 at 7:42

GoogleCodeExporter commented 9 years ago
Thanks, and I will try to explain my need: I use the html compressor and 
closure to do static compression, because my web server run in CPU firmware 
with lower count of RAM, and only one thread, and if I low the files requested 
the global performance is improved. 

When the htmlcompressor found a <string> tag, put the string in a file an pass 
to the closure, or there are a for to pass like a line command. 

Original comment by juanschi...@gmail.com on 25 Apr 2011 at 8:11

GoogleCodeExporter commented 9 years ago
Sorry, I didn't understand the question.

Lets say you have such page:
<html>
    <script>
        var a=1;
    </script>
    ...
    <script>
        alert(a);
    </script>
</html>

HTML compressor makes 2 calls to Closure compiler:
1. Compress "var a=1;" and put compressed result back
2. Compress "alert(a);" and put compressed result back

It doesn't use any files, just compresses strings.

If your server doesn't handle big number of connections, you can combine all 
your javascript into one file (jquery.js + jquery plugins + your own files). 
Combine all your images into one sprite, all your css into one file. So as a 
result you will just have 3 external files to include (which will be cached by 
the browser too). It is considered a good practice to combine many files into 
one.

Original comment by serg472@gmail.com on 25 Apr 2011 at 8:27

GoogleCodeExporter commented 9 years ago
Thanks for the explanation. I will try to show the code that when compressed is 
renamethe tags value, enabled, checked, etc.

the source code :

// JavaScript Document
window['default_values'] = default_values;

function get_obj(name)
{
    return document.getElementById(name);
}

function default_values()
{
    get_obj("i31").enabled = false;
    get_obj("i32").enabled = false;
    get_obj("i30").checked = false;  
}

compressed with the following line:

java -jar compiler.jar --compilation_level ADVANCED_OPTIMIZATIONS --js 
general_inline.htm --js_output_file general_comp.htm

give this result:

window.default_values=a;function 
a(){document.getElementById("i31").enabled=!1;document.getElementById("i32").ena
bled=!1;document.getElementById("i30").checked=!1};

the
window.default_values=a;function 
a(){document.getElementById("i31").enabled=!1;document.getElementById("i32").ena
bled=!1;document.getElementById("i30").checked=!1};

when compile the same code without the <script> tags with the following line:

java -jar compiler.jar --compilation_level ADVANCED_OPTIMIZATIONS --js 
general_inline.htm --js_output_file general_comp.htm

give the following result:

window.default_values=a;function 
a(){document.getElementById("i31").enabled=!1;document.getElementById("i32").ena
bled=!1;document.getElementById("i30").checked=!1};

The htmlcompressor witch options pass to the closure?

Original comment by juanschi...@gmail.com on 25 Apr 2011 at 9:13

GoogleCodeExporter commented 9 years ago
Sorry, I don't see any differences between those 3 examples.

HTML compressor calls Closure with default settings, I am not sure what they 
are.

Original comment by serg472@gmail.com on 25 Apr 2011 at 9:36

GoogleCodeExporter commented 9 years ago
sorry, I past the wrong output:

<script>window.default_values=a;function 
a(){document.a("i31").b=!1;document.a("i32").b=!1;document.a("i30").c=!1};</scri
pt>

for htmlcompress. with the following line:

java -jar htmlcompressor-1.3.jar --type html --remove-link-attr 
--remove-intertag-spaces --simple-doctype --remove-quotes --remove-style-attr 
--remove-input-attr --remove-form-attr --remove-js-protocol --simple-bool-attr 
--remove-script-attr --charset ISO-8859-1 --compress-js --js-compressor closure 
--closure-opt-level advanced general_inline.js -o general_comp.htm

I thinks the problem is that from the htmlcompressor you pass the 
--use_only_custom_externs, that disable the defaults externs of closure 
compiler.

Original comment by juanschi...@gmail.com on 25 Apr 2011 at 9:51

GoogleCodeExporter commented 9 years ago
Ok, I will look into it. 

Original comment by serg472@gmail.com on 25 Apr 2011 at 10:28

GoogleCodeExporter commented 9 years ago
I see in the ClosureJavaScriptCompressor.java the line:

private JSSourceFile externs = JSSourceFile.fromCode("externs.js", "");

I don't know where to put the externs.js, I put in the same folder than 
htmlcompressor-1.3.jar, but don't load.

Original comment by juanschi...@gmail.com on 25 Apr 2011 at 10:53

GoogleCodeExporter commented 9 years ago
That line just means externs are empty (default settings). Are you compressing 
html from command line or directly from java api? If you are using java api 
then you could do something like:

ClosureJavaScriptCompressor jsCompressor = new ClosureJavaScriptCompressor();
jsCompressor.setExterns(JSSourceFile.fromFile("/path/to/file"));

Command line compressor currently doesn't support externs.

Original comment by serg472@gmail.com on 25 Apr 2011 at 11:22

GoogleCodeExporter commented 9 years ago
I compress html from the command line.
Can you explain me how to compile the source of htmcompressor?

Original comment by juanschi...@gmail.com on 26 Apr 2011 at 12:22

GoogleCodeExporter commented 9 years ago
hi, I found how to compile, type ant and I get the compressor compiled.

Original comment by juanschi...@gmail.com on 26 Apr 2011 at 12:45

GoogleCodeExporter commented 9 years ago
1) If I change JSSourceFile externs = JSSourceFile.fromCode("externs.js", "");
   to JSSourceFile externs = JSSourceFile.fromFile("externs.js");, I can 
   pass extern symbols in the externs.js file.

2) Also I found that in the this blog: 
http://blog.bolinfest.com/2009/11/calling-closure-compiler-from-java.html, they 
comment this:     
    // To get the complete set of externs, the logic in
    // CompilerRunner.getDefaultExterns() should be used here. 

3) Now I will try to implement, but my Java expertise is poor. 

Original comment by juanschi...@gmail.com on 26 Apr 2011 at 2:41

GoogleCodeExporter commented 9 years ago
Ok, thank you. I should be able to make those changes by the end of the week, 
so if you can wait a few days the problem should be fixed.

Original comment by serg472@gmail.com on 26 Apr 2011 at 6:03

GoogleCodeExporter commented 9 years ago
thanks, and I can wait.

In the mean time I made this workaround, proposed by the:

http://www.google.com/codesearch/p?hl=en#IwN_TdTjnqM/trunk/src/com/bolinfest/clo
sure/StripTypeOnlyExample.java&q=getDefaultExterns&sa=N&cd=2&ct=rc

The workaround need unzip the exports.zip in the same folder of htmlcompressor,
and add the following list:

    final String path = "externs/";
    List<JSSourceFile> externs = ImmutableList.of(
        JSSourceFile.fromFile(path + "deprecated.js"),
        JSSourceFile.fromFile(path + "es3.js"),
        JSSourceFile.fromFile(path + "es5.js"),
        JSSourceFile.fromFile(path + "fileapi.js"),
        JSSourceFile.fromFile(path + "flash.js"),
        JSSourceFile.fromFile(path + "gears_symbols.js"),
        JSSourceFile.fromFile(path + "gears_types.js"),
        JSSourceFile.fromFile(path + "gecko_css.js"),
        JSSourceFile.fromFile(path + "gecko_dom.js"),
        JSSourceFile.fromFile(path + "gecko_event.js"),
        JSSourceFile.fromFile(path + "gecko_xml.js"),
        JSSourceFile.fromFile(path + "google.js"),
        JSSourceFile.fromFile(path + "html5.js"),
        JSSourceFile.fromFile(path + "ie_css.js"),
        JSSourceFile.fromFile(path + "ie_dom.js"),
        JSSourceFile.fromFile(path + "ie_event.js"),
        JSSourceFile.fromFile(path + "ie_vml.js"),
        JSSourceFile.fromFile(path + "iphone.js"),
        JSSourceFile.fromFile(path + "w3c_css.js"),
        JSSourceFile.fromFile(path + "w3c_css3d.js"),
        JSSourceFile.fromFile(path + "w3c_dom1.js"),
        JSSourceFile.fromFile(path + "w3c_dom2.js"),
        JSSourceFile.fromFile(path + "w3c_dom3.js"),
        JSSourceFile.fromFile(path + "w3c_elementtraversal.js"),
        JSSourceFile.fromFile(path + "w3c_event3.js"),
        JSSourceFile.fromFile(path + "w3c_event.js"),
        JSSourceFile.fromFile(path + "w3c_geolocation.js"),
        JSSourceFile.fromFile(path + "w3c_indexeddb.js"),
        JSSourceFile.fromFile(path + "w3c_range.js"),
        JSSourceFile.fromFile(path + "w3c_selectors.js"),
        JSSourceFile.fromFile(path + "w3c_xml.js"),
        JSSourceFile.fromFile(path + "webgl.js"),
        JSSourceFile.fromFile(path + "webkit_css.js"),
        JSSourceFile.fromFile(path + "webkit_dom.js"),
        JSSourceFile.fromFile(path + "webkit_event.js"),
        JSSourceFile.fromFile(path + "webkit_notifications.js"),
        JSSourceFile.fromFile(path + "webstorage.js"),
        JSSourceFile.fromFile(path + "window.js")
        );

attached I send you the modification. 

Original comment by juanschi...@gmail.com on 26 Apr 2011 at 6:38

Attachments:

GoogleCodeExporter commented 9 years ago
Thanks, where can I get this "exports.zip"?

Original comment by serg472@gmail.com on 26 Apr 2011 at 8:29

GoogleCodeExporter commented 9 years ago
the exports.zip is inside compiler.jar

Original comment by juanschi...@gmail.com on 26 Apr 2011 at 9:56

Attachments:

GoogleCodeExporter commented 9 years ago
Fixed in 1.3.1 release.

When using advanced level of compilation, default externs from compiler.jar are 
applied automatically. You can also provide your own list of externs that will 
be added to default ones using --closure-externs <file> params, and set 
--closure-custom-externs-only parameter to skip default externs.

Thanks.

Original comment by serg472@gmail.com on 30 Apr 2011 at 6:35

GoogleCodeExporter commented 9 years ago
serg472,

I test the new version and work fine, thanks a lot.

Regards
Juan 

Original comment by juanschi...@gmail.com on 30 Apr 2011 at 8:59