durehed / htmlcompressor

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

Replace YUI compressor #27

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Hello. What about to add possibility to replace css and javascript compressor?

Add some interfaces and config options like CSSCompressorClass and 
JSCompressorClass...?

I use google closure compiler and don't like yahoo compressor (some versions 
has bugs, with 2.4.4 I get StackOverflowException).

P.S. Sorry for my English.

Original issue reported on code.google.com by lexius.j...@gmail.com on 11 Mar 2011 at 10:17

GoogleCodeExporter commented 8 years ago
Most I can do is add ability to provide your own js compressor implementation 
to java htmlcompressor API, command line compressor won't support it. Options 
also would be out of the question probably, as you would need to set all 
options in your implementation.

So it could be something like:

HtmlCompressor compressor = new HtmlCompressor();
compressor.setJsCompressor(new MyJsCompressor(myoption1, myoption2));   
compressor.compress(html);

Where MyJsCompressor is:

public class MyJsCompressor implements Compressor {

    public MyJsCompressor(String myoption1, String myoption2) {
        //set your own options 
    }

    public String compress(String uncompressedSource) {
        //you do all the JS compression here yourself
        return compressedResult;
    }
}

With interface being as simple as:

public interface Compressor {
    public abstract String compress(String content) throws Exception;
}

Is it something similar to what you proposed?

Original comment by serg472@gmail.com on 11 Mar 2011 at 11:20

GoogleCodeExporter commented 8 years ago
Yes. It will be great!

I have an idea about css compressor: you can borrow code from yahoo compressor. 
It is 6.9KB class without dependencies (this is a port of Isaac Schlueter's 
cssmin utility).

Original comment by lexius.j...@gmail.com on 12 Mar 2011 at 11:53

GoogleCodeExporter commented 8 years ago
Посмотрел в исходниках копирайт... я так 
понимаю мы и на русском/украинском друг 
друга поймем?

Original comment by lexius.j...@gmail.com on 12 Mar 2011 at 7:25

GoogleCodeExporter commented 8 years ago
Поймем, но лучше по англ. чтобы и остальным 
было понятно о чем речь :)

Original comment by serg472@gmail.com on 12 Mar 2011 at 10:57

GoogleCodeExporter commented 8 years ago
I checked google closure sources and seems like making it compress inline 
javascript would require some work. Are you sure you will be able to use 
closure to compile inline javascript on a page? Have you tried it already and 
can you show some code? (maybe I will include closure support to html 
compressor)

Original comment by serg472@gmail.com on 13 Mar 2011 at 4:47

GoogleCodeExporter commented 8 years ago
It looks like this: 

public String compressJS(String code) {

        StringWriter writer = new StringWriter();
        try {
            Compiler.setLoggingLevel(Level.SEVERE);
            final Compiler compiler = new Compiler();

            compiler.disableThreads();
            final CompilerOptions options = new CompilerOptions();
            //set some options here
            compilationLevel.setOptionsForCompilationLevel(options);
            final JSSourceFile extern = JSSourceFile.fromCode("externs.js", "");
            final JSSourceFile input = JSSourceFile.fromCode("", code);

            final Result result = compiler.compile(extern, input, options);
            if (result.success) {
                writer.write(compiler.toSource());
                return compressed = writer.toString();

            } else {
                //logging here
                return code;
            }
        }

    }

Original comment by lexius.j...@gmail.com on 13 Mar 2011 at 10:01

GoogleCodeExporter commented 8 years ago
import com.google.javascript.jscomp.*;
import com.google.javascript.jscomp.Compiler;

Original comment by lexius.j...@gmail.com on 13 Mar 2011 at 10:02

GoogleCodeExporter commented 8 years ago
Maven dependencies:

<dependency>
            <groupId>com.google.javascript</groupId>
            <artifactId>closure-compiler</artifactId>
            <version>r706</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.ant</groupId>
                    <artifactId>ant</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>args4j</groupId>
                    <artifactId>args4j</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.google.code.findbugs</groupId>
                    <artifactId>jsr305</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.json</groupId>
                    <artifactId>json</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.google.protobuf</groupId>
                    <artifactId>protobuf-java</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

Original comment by lexius.j...@gmail.com on 13 Mar 2011 at 10:03

GoogleCodeExporter commented 8 years ago
Great, this would save me a lot of time, thanks :)

Original comment by serg472@gmail.com on 13 Mar 2011 at 4:14

GoogleCodeExporter commented 8 years ago
Support for custom JS and CSS compressor implementations was added in 1.0 
release. 

I also created default Google Closure compressor implementation called 
com.googlecode.htmlcompressor.compressor.ClosureJavaScriptCompressor which I 
hope will be enough for most cases (it is also will be used in command line 
compressor and other places that don't support custom implementations)

Css compressor implementation was moved from YUI to HtmlCompressor, so YUI 
dependency is eliminated.

Thank you for good ideas and helping with the coding. It should be good 
improvement for the project :)

Original comment by serg472@gmail.com on 19 Mar 2011 at 11:42