YUI Compressor

repository·master·Indexed 25 days ago

https://github.com/yui/yuicompressor

A tool for compressing and obfuscating JavaScript and CSS files to reduce file size by removing whitespace and comments. Available as a CLI and a Node.js package (version 2.4.9), it requires a Java runtime to function. The Node.js API provides `compress` and `compressString` methods with configurable options for charset, type, line-breaks, and obfuscation control.

Tokens
1.6K
Snippets
4
Records
12
Agent score
76%

What's inside yuicompressor

  1. Prevent obfuscation using hints

    master

    You can prevent specific local variables, nested functions, or function arguments from being obfuscated by placing a "hint" string at the very beginning of the function body. The hint is removed during compression.

    Hint format: "argName:nomunge, localVar:nomunge, nestedFn:nomunge";

    function fn (arg1, arg2, arg3) {
        "arg2:nomunge, localVar:nomunge, nestedFn:nomunge";
    
        ... 
        var localVar;
        ...
    
        function nestedFn () {
            ....
        }
    }
  2. Build the YUI Compressor documentation

    master

    To build the documentation locally, you need to install selleck globally, clone the repository, and then use selleck to output the documentation to a local pages directory. This process involves pulling the gh-pages branch and running the build command from the docs/ directory.

    # Install the documentation builder
    $ npm -g install selleck
    
    # Clone the repository and a separate pages directory
    $ git clone git://github.com/yui/yuicompressor.git
    $ git clone git://github.com/yui/yuicompressor.git yuicompressor-pages
    
    # Prepare the pages directory
    cd yuicompressor-pages
    git fetch
    git checkout -t gh-pages
    
    # Build the docs
    cd ../yuicompressor/docs/
    selleck -o ../../yuicompressor-pages/
  3. Install the yuicompressor Node.js package

    master

    You can use YUI Compressor within a Node.js application to compress files or strings asynchronously. Note that this package still requires Java to be installed on the system as it uses Java under the hood.

    npm i yuicompressor
  4. Configure yuicompressor Node.js options

    master

    When using the Node.js API, you can pass the following options in the configuration object:

    • charset: Character set to use (defaults to 'utf8').
    • type: The type of compressor ('js' or 'css'; defaults to 'js').
    • line-break: Specifies a column for line breaks.
    • nomunge: If true, performs minification only without obfuscating local symbols.
    • preserve-semi: Preserves unnecessary semicolons (e.g., before a }).
    • disable-optimizations: Disables all built-in micro optimizations.
  5. Use the yuicompressor Node.js API

    master

    Import compressor from yuicompressor and call the compress method. The method accepts a file path or a string of JavaScript/CSS, an options object, and a callback function.

    Callback arguments:

    • err: Error object (contains stderr if an error occurs).
    • data: The compressed string.
    • extra: The stderr (contains warnings).
    var compressor = require('yuicompressor');
    
    compressor.compress('/path/to/file or String of JS', {
        //Compressor Options:
        charset: 'utf8',
        type: 'js',
        nomunge: true,
        'line-break': 80
    }, function(err, data, extra) {
        //err   If compressor encounters an error, it's stderr will be here
        //data  The compressed string, you write it out where you want it
        //extra The stderr (warnings are printed here in case you want to echo them
    });
  6. Configure compression options

    master

    When calling compress or compressString, you can pass an options object to control the compression behavior. The following keys are supported:

    • type: The type of file to compress (e.g., 'js' or 'css'). Defaults to 'js'.
    • charset: The character encoding. Defaults to 'utf8'.
    • line-break: Enables/disables line breaks.
    • nomunge: Enables/disables variable renaming (munging).
    • preserve-semi: Enables/disables semicolon preservation.
    • disable-optimizations: Enables/disables optimizations.
  7. Use YUI Compressor CLI Global Options

    master

    The YUI Compressor CLI supports the following global options:

    • -h, --help: Prints help documentation.
    • --line-break <column>: Splits long lines after a specific column. Use 0 to break after each semicolon (JS) or rule (CSS).
    • --type js|css: Specifies the compressor type. Required if no input file is provided or if the input file extension is not .js or .css.
    • --charset <character-set>: Specifies the character set for reading and writing files.
    • -o <outfile>: Specifies the output file. Supports filter syntax for multiple input files (e.g., -o '.css$:-min.css' *.css).
    • -v, --verbose: Displays informational messages and warnings.
  8. Use YUI Compressor JavaScript-only options

    master

    When compressing JavaScript, you can use these specific flags:

    • --nomunge: Minify only; do not obfuscate local symbols.
    • --preserve-semi: Preserve unnecessary semicolons (useful for tools like JSLint).
    • --disable-optimizations: Disable all built-in micro optimizations.
  9. Compress content using compress()

    master
    The compress function allows you to compress either a raw string or a file path. If a file path is provided, the function reads the file content before compression. It requires a Java runtime to be installed on the system as it spawns a Java process to run the YUI Compressor JAR.
  10. Run YUI Compressor via Node.js CLI

    master
    The nodejs/cli.js file provides a Node.js wrapper around the YUI Compressor .jar file. It allows you to execute the compressor from the command line using node instead of calling java -jar directly. This wrapper uses kexec to execute the Java command with the necessary arguments.
  11. Access the YUI Compressor JAR path

    master
    The jar property on the exported module provides the absolute path to the YUI Compressor .jar file found in the ../build directory of the package. This is useful for verifying that the required Java component is present.