Class ClosureJavaScriptCompressor

java.lang.Object
com.googlecode.htmlcompressor.compressor.ClosureJavaScriptCompressor
All Implemented Interfaces:
Compressor

public class ClosureJavaScriptCompressor extends Object implements Compressor
Basic JavaScript compressor implementation using Google Closure Compiler that could be used by HtmlCompressor for inline JavaScript compression.
Author:
Sergiy Kovalchuk
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The Constant COMPILATION_LEVEL_ADVANCED.
    static final String
    The Constant COMPILATION_LEVEL_SIMPLE.
    static final String
    The Constant COMPILATION_LEVEL_WHITESPACE.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Instantiates a new closure java script compressor.
    ClosureJavaScriptCompressor(com.google.javascript.jscomp.CompilationLevel compilationLevel)
    Instantiates a new closure java script compressor.
  • Method Summary

    Modifier and Type
    Method
    Description
    compress(String source)
    The main method that compresses the given source and returns a compressed result.
    com.google.javascript.jscomp.CompilationLevel
    Returns level of optimization that is applied when compiling JavaScript code.
    com.google.javascript.jscomp.CompilerOptions
    Returns options that are used by the Closure compiler.
    List<com.google.javascript.jscomp.SourceFile>
    Returns SourceFile used as a reference during the compression at CompilationLevel.ADVANCED_OPTIMIZATIONS level.
    Returns logging level used by the Closure compiler (note: closure compiler still uses java util logging, don't import Level).
    com.google.javascript.jscomp.WarningLevel
    Returns WarningLevel used by the Closure compiler.
    boolean
    Returns true if default externs defined inside Closure's jar are ignored and only user defined ones are used.
    void
    setCompilationLevel(com.google.javascript.jscomp.CompilationLevel compilationLevel)
    Sets level of optimization that should be applied when compiling JavaScript code.
    void
    setCompilerOptions(com.google.javascript.jscomp.CompilerOptions compilerOptions)
    Sets options that will be used by the Closure compiler.
    void
    setCustomExternsOnly(boolean customExternsOnly)
    If set to true, default externs defined inside Closure's jar will be ignored and only user defined ones will be used.
    void
    setExterns(List<com.google.javascript.jscomp.SourceFile> externs)
    Sets external JavaScript files that are used as a reference for function declarations if CompilationLevel.ADVANCED_OPTIMIZATIONS compression level is used.
    void
    setLoggingLevel(Level loggingLevel)
    Sets logging level for the Closure compiler (note: closure compiler still uses java util logging, don't import Level).
    void
    setWarningLevel(com.google.javascript.jscomp.WarningLevel warningLevel)
    Indicates the amount of information you want from the compiler about possible problems in your code.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • COMPILATION_LEVEL_SIMPLE

      public static final String COMPILATION_LEVEL_SIMPLE
      The Constant COMPILATION_LEVEL_SIMPLE.
      See Also:
    • COMPILATION_LEVEL_ADVANCED

      public static final String COMPILATION_LEVEL_ADVANCED
      The Constant COMPILATION_LEVEL_ADVANCED.
      See Also:
    • COMPILATION_LEVEL_WHITESPACE

      public static final String COMPILATION_LEVEL_WHITESPACE
      The Constant COMPILATION_LEVEL_WHITESPACE.
      See Also:
  • Constructor Details

    • ClosureJavaScriptCompressor

      public ClosureJavaScriptCompressor()
      Instantiates a new closure java script compressor.
    • ClosureJavaScriptCompressor

      public ClosureJavaScriptCompressor(com.google.javascript.jscomp.CompilationLevel compilationLevel)
      Instantiates a new closure java script compressor.
      Parameters:
      compilationLevel - the compilation level
  • Method Details

    • compress

      public String compress(String source)
      Description copied from interface: Compressor
      The main method that compresses the given source and returns a compressed result.
      Specified by:
      compress in interface Compressor
      Parameters:
      source - The source to compress.
      Returns:
      Compressed result.
    • getCompilationLevel

      public com.google.javascript.jscomp.CompilationLevel getCompilationLevel()
      Returns level of optimization that is applied when compiling JavaScript code.
      Returns:
      CompilationLevel that is applied when compiling JavaScript code.
      See Also:
    • setCompilationLevel

      public void setCompilationLevel(com.google.javascript.jscomp.CompilationLevel compilationLevel)
      Sets level of optimization that should be applied when compiling JavaScript code. If none is provided, CompilationLevel.SIMPLE_OPTIMIZATIONS will be used by default.

      Warning: Using CompilationLevel.ADVANCED_OPTIMIZATIONS could break inline JavaScript if externs are not set properly.

      Parameters:
      compilationLevel - Optimization level to use, could be set to CompilationLevel.ADVANCED_OPTIMIZATIONS, CompilationLevel.SIMPLE_OPTIMIZATIONS, CompilationLevel.WHITESPACE_ONLY
      See Also:
    • getCompilerOptions

      public com.google.javascript.jscomp.CompilerOptions getCompilerOptions()
      Returns options that are used by the Closure compiler.
      Returns:
      CompilerOptions that are used by the compiler
      See Also:
    • setCompilerOptions

      public void setCompilerOptions(com.google.javascript.jscomp.CompilerOptions compilerOptions)
      Sets options that will be used by the Closure compiler. If none is provided, default options constructor will be used: new CompilerOptions().
      Parameters:
      compilerOptions - CompilerOptions that will be used by the compiler
      See Also:
    • getLoggingLevel

      public Level getLoggingLevel()
      Returns logging level used by the Closure compiler (note: closure compiler still uses java util logging, don't import Level).
      Returns:
      Level of logging used by the Closure compiler
    • setLoggingLevel

      public void setLoggingLevel(Level loggingLevel)
      Sets logging level for the Closure compiler (note: closure compiler still uses java util logging, don't import Level).
      Parameters:
      loggingLevel - logging level for the Closure compiler.
      See Also:
    • getExterns

      public List<com.google.javascript.jscomp.SourceFile> getExterns()
      Returns SourceFile used as a reference during the compression at CompilationLevel.ADVANCED_OPTIMIZATIONS level.
      Returns:
      SourceFile used as a reference during compression
    • setExterns

      public void setExterns(List<com.google.javascript.jscomp.SourceFile> externs)
      Sets external JavaScript files that are used as a reference for function declarations if CompilationLevel.ADVANCED_OPTIMIZATIONS compression level is used.

      A number of default externs defined inside Closure's jar will be used besides user defined ones, to use only user defined externs set setCustomExternsOnly(true)

      Warning: Using CompilationLevel.ADVANCED_OPTIMIZATIONS could break inline JavaScript if externs are not set properly.

      Parameters:
      externs - SourceFile to use as a reference during compression
      See Also:
    • getWarningLevel

      public com.google.javascript.jscomp.WarningLevel getWarningLevel()
      Returns WarningLevel used by the Closure compiler.
      Returns:
      WarningLevel used by the Closure compiler
    • setWarningLevel

      public void setWarningLevel(com.google.javascript.jscomp.WarningLevel warningLevel)
      Indicates the amount of information you want from the compiler about possible problems in your code.
      Parameters:
      warningLevel - WarningLevel to use
      See Also:
    • isCustomExternsOnly

      public boolean isCustomExternsOnly()
      Returns true if default externs defined inside Closure's jar are ignored and only user defined ones are used.
      Returns:
      true if default externs defined inside Closure's jar are ignored and only user defined ones are used
    • setCustomExternsOnly

      public void setCustomExternsOnly(boolean customExternsOnly)
      If set to true, default externs defined inside Closure's jar will be ignored and only user defined ones will be used.
      Parameters:
      customExternsOnly - true to skip default externs and use only user defined ones
      See Also: