### Install Selleck globally Source: https://github.com/yui/yuicompressor/blob/master/docs/README.md Install the Selleck documentation generator via npm. ```terminal $ npm -g install selleck ``` -------------------------------- ### Install YUI Compressor Node.js Package Source: https://context7.com/yui/yuicompressor/llms.txt Installation commands for the Node.js wrapper. ```bash # Install via npm npm install yuicompressor # Or add to package.json npm install yuicompressor --save ``` -------------------------------- ### Install Node.js package Source: https://github.com/yui/yuicompressor/blob/master/README.md Install the YUI Compressor package via npm. ```bash npm i yuicompressor ``` -------------------------------- ### YUI Compressor - Command Line Interface Source: https://context7.com/yui/yuicompressor/llms.txt Examples of using the YUI Compressor JAR file from the command line for basic compression, stdin usage, charset specification, line breaks, and verbose mode. ```APIDOC ## YUI Compressor - Command Line Interface ### Description Examples of using the YUI Compressor JAR file from the command line for basic compression, stdin usage, charset specification, line breaks, and verbose mode. ### Usage Examples ```bash # Basic JavaScript compression java -jar yuicompressor.jar input.js -o output.js # Basic CSS compression java -jar yuicompressor.jar input.css -o output.css # Compress JavaScript from stdin cat input.js | java -jar yuicompressor.jar --type js -o output.js # Compress with specific charset java -jar yuicompressor.jar --charset utf-8 input.js -o output.js # Add line breaks after 80 characters (for source control tools) java -jar yuicompressor.jar --line-break 80 input.js -o output.js # Verbose mode with warnings java -jar yuicompressor.jar -v input.js -o output.js ``` ``` -------------------------------- ### YUI Compressor - Node.js Package Installation Source: https://context7.com/yui/yuicompressor/llms.txt How to install the YUI Compressor npm package for programmatic use in Node.js applications. ```APIDOC ## YUI Compressor - Node.js Package Installation ### Description Install the YUI Compressor npm package to use it programmatically in Node.js applications. ### Installation ```bash # Install via npm npm install yuicompressor # Or add to package.json npm install yuicompressor --save ``` ``` -------------------------------- ### CSS Compression Example Source: https://context7.com/yui/yuicompressor/llms.txt Demonstrates CSS compression, which includes whitespace removal, shortening color codes (e.g., `rgb(255, 0, 0)` to `red`, `#AABBCC` to `#abc`), simplifying values (e.g., `0px` to `0`), and optimizing opacity. ```css /* Input CSS */ .color { background-color: rgb(255, 0, 0); color: #AABBCC; border: none; margin: 0px 0em 0%; padding: 0.5em; opacity: 0.80; } /* Output CSS */ /* .color{background-color:red;color:#abc;border:0;margin:0;padding:.5em;opacity:.8} */ /* Color optimizations: * - rgb(255, 0, 0) -> red * - #AABBCC -> #abc (when pairs match) * - border: none -> border:0 * - 0px, 0em, 0% -> 0 * - 0.80 -> .8 * - 0.5em -> .5em */ ``` -------------------------------- ### Gulp Integration for JavaScript Minification Source: https://context7.com/yui/yuicompressor/llms.txt Example of using YUI Compressor within a Gulp task to minify JavaScript files. Requires 'gulp', 'yuicompressor', and 'through2' packages. The task reads from 'src/**/*.js' and outputs to 'dist'. ```javascript // Gulp integration example var gulp = require('gulp'); var compressor = require('yuicompressor'); var through = require('through2'); gulp.task('minify-js', function() { return gulp.src('src/**/*.js') .pipe(through.obj(function(file, enc, cb) { var self = this; compressor.compress(file.contents.toString(), { type: 'js' }, function(err, data) { if (err) return cb(err); file.contents = Buffer.from(data); self.push(file); cb(); }); })) .pipe(gulp.dest('dist')); }); ``` -------------------------------- ### Preserve comments Source: https://github.com/yui/yuicompressor/blob/master/README.md Comments starting with /*! are preserved in the output, which is useful for copyright or license notices. ```javascript /*! * TERMS OF USE - EASING EQUATIONS * Open source under the BSD License. * Copyright 2001 Robert Penner All rights reserved. */ ``` -------------------------------- ### Build documentation with Selleck Source: https://github.com/yui/yuicompressor/blob/master/docs/README.md Run the selleck command from the docs directory to generate the site. ```terminal cd ../yuicompressor/docs/ selleck -o ../../yuicompressor-pages/ ``` -------------------------------- ### Build the project Source: https://github.com/yui/yuicompressor/blob/master/README.md Use Apache Ant to build the YUI Compressor project. ```bash ant ``` -------------------------------- ### Clone and prepare documentation repository Source: https://github.com/yui/yuicompressor/blob/master/docs/README.md Clone the source and pages repositories, then switch to the gh-pages branch. ```terminal $ git clone git://github.com/yui/yuicompressor.git $ git clone git://github.com/yui/yuicompressor.git yuicompressor-pages cd yuicompressor-pages git fetch git checkout -t gh-pages ``` -------------------------------- ### Build YUI Compressor JAR from Source Source: https://context7.com/yui/yuicompressor/llms.txt Clone the repository, navigate to the directory, and use Apache Ant to build the JAR file. The JAR will be located in the build/ directory. Includes commands for running tests. ```bash # Clone the repository git clone https://github.com/yui/yuicompressor.git cd yuicompressor # Build using Ant ant # The JAR file will be created in the build/ directory # Run tests ./tests/suite.sh # Run Node.js tests npm test ``` -------------------------------- ### Run the test suite Source: https://github.com/yui/yuicompressor/blob/master/README.md Execute the shell script to run the project tests. ```bash ./tests/suite.sh ``` -------------------------------- ### Execute YUI Compressor via Command Line Source: https://context7.com/yui/yuicompressor/llms.txt Standard command-line usage for compressing JavaScript and CSS files, including options for charset, line breaks, and verbose output. ```bash # Basic JavaScript compression java -jar yuicompressor.jar input.js -o output.js # Basic CSS compression java -jar yuicompressor.jar input.css -o output.css # Compress JavaScript from stdin cat input.js | java -jar yuicompressor.jar --type js -o output.js # Compress with specific charset java -jar yuicompressor.jar --charset utf-8 input.js -o output.js # Add line breaks after 80 characters (for source control tools) java -jar yuicompressor.jar --line-break 80 input.js -o output.js # Verbose mode with warnings java -jar yuicompressor.jar -v input.js -o output.js ``` -------------------------------- ### YUI Compressor - Batch Processing Source: https://context7.com/yui/yuicompressor/llms.txt Instructions for processing multiple files at once using pattern substitution for output filenames. ```APIDOC ## YUI Compressor - Batch Processing ### Description Process multiple files in a single command using pattern substitution for output filenames. ### Usage Examples ```bash # Compress all CSS files, saving as -min.css java -jar yuicompressor.jar -o '.css$:-min.css' *.css # Compress all JavaScript files, saving as -min.js java -jar yuicompressor.jar -o '.js$:-min.js' *.js # Compress all JS files in a directory with verbose output java -jar yuicompressor.jar -v -o '.js$:-min.js' src/*.js ``` ``` -------------------------------- ### Batch Process Files via Command Line Source: https://context7.com/yui/yuicompressor/llms.txt Use pattern substitution to process multiple files in a single command. ```bash # Compress all CSS files, saving as -min.css java -jar yuicompressor.jar -o '.css$:-min.css' *.css # Compress all JavaScript files, saving as -min.js java -jar yuicompressor.jar -o '.js$:-min.js' *.js # Compress all JS files in a directory with verbose output java -jar yuicompressor.jar -v -o '.js$:-min.js' src/*.js ``` -------------------------------- ### Apply JavaScript-Specific Compression Options Source: https://context7.com/yui/yuicompressor/llms.txt Advanced flags for controlling obfuscation, semicolon preservation, and micro-optimizations during JavaScript minification. ```bash # Minify only, do not obfuscate local variables (nomunge) java -jar yuicompressor.jar --nomunge input.js -o output.js # Preserve all semicolons (useful for JSLint compatibility) java -jar yuicompressor.jar --preserve-semi input.js -o output.js # Disable all micro optimizations java -jar yuicompressor.jar --disable-optimizations input.js -o output.js # Combine multiple options java -jar yuicompressor.jar --nomunge --preserve-semi input.js -o output.js # Generate munge mapping file (shows original -> obfuscated variable names) java -jar yuicompressor.jar -m munge-map.txt input.js -o output.js ``` -------------------------------- ### Batch minify CSS files Source: https://github.com/yui/yuicompressor/blob/master/README.md Use the command line interface to minify multiple CSS files and save them with a -min.css suffix. ```bash java -jar yuicompressor.jar -o '.css$:-min.css' *.css ``` -------------------------------- ### Prevent obfuscation with hints Source: https://github.com/yui/yuicompressor/blob/master/README.md Use a string hint at the beginning of a function body to prevent specific variables or functions from being obfuscated. ```javascript function fn (arg1, arg2, arg3) { "arg2:nomunge, localVar:nomunge, nestedFn:nomunge"; ... var localVar; ... function nestedFn () { .... } ... } ``` -------------------------------- ### Java CssCompressor Source: https://context7.com/yui/yuicompressor/llms.txt Programmatic CSS compression using the CssCompressor class. ```APIDOC ## CssCompressor.compress() ### Description Compresses CSS code using a Reader and Writer. ### Parameters - **out** (Writer) - Required - Output destination. - **linebreak** (int) - Required - Line break position (-1 for no line breaks). ``` -------------------------------- ### YUI Compressor - Node.js API compress() Source: https://context7.com/yui/yuicompressor/llms.txt Details on using the `compress()` function from the Node.js package to compress JavaScript or CSS content. ```APIDOC ## YUI Compressor - Node.js API compress() ### Description The main function for compressing JavaScript or CSS content asynchronously. Accepts either a file path or a string of code to compress. ### Method Signature `compress(input, options, callback)` - `input` (string): A file path or a string of code to compress. - `options` (object): Configuration options for compression. - `charset` (string): Specifies the character set (e.g., 'utf8'). - `type` (string): The type of content to compress ('js' or 'css'). - `nomunge` (boolean): If true, disables local variable obfuscation (JavaScript only). - `line-break` (number): Specifies the column number to insert line breaks. - `preserve-semi` (boolean): If true, preserves all semicolons (JavaScript only). - `disable-optimizations` (boolean): If true, disables micro optimizations (JavaScript only). - `callback` (function): A function to be called upon completion. Receives `err`, `data`, and `extra` (warnings). ### Usage Examples ```javascript var compressor = require('yuicompressor'); // Compress a JavaScript file compressor.compress('/path/to/file.js', { charset: 'utf8', type: 'js', nomunge: false, 'line-break': 80, 'preserve-semi': false, 'disable-optimizations': false }, function(err, data, extra) { if (err) { console.error('Compression error:', err); return; } console.log('Compressed output:', data); // extra contains stderr (warnings) if (extra) { console.log('Warnings:', extra); } }); // Compress a CSS file compressor.compress('/path/to/styles.css', { charset: 'utf8', type: 'css', 'line-break': 80 }, function(err, data, extra) { if (err) { console.error('Compression error:', err); return; } console.log('Compressed CSS:', data); }); // Compress a JavaScript string directly var jsCode = 'var x = (function() { var foo = 1, bar = 2; return (foo + bar) }())'; compressor.compress(jsCode, { type: 'js' }, function(err, data) { if (err) { console.error('Error:', err); return; } // Output: var x=(function(){var b=1,a=2;return(b+a)}()); console.log('Minified:', data); }); ``` ``` -------------------------------- ### Compress Content Programmatically in Node.js Source: https://context7.com/yui/yuicompressor/llms.txt Use the compress() function to minify files or raw strings asynchronously. ```javascript var compressor = require('yuicompressor'); // Compress a JavaScript file compressor.compress('/path/to/file.js', { charset: 'utf8', type: 'js', nomunge: false, 'line-break': 80, 'preserve-semi': false, 'disable-optimizations': false }, function(err, data, extra) { if (err) { console.error('Compression error:', err); return; } console.log('Compressed output:', data); // extra contains stderr (warnings) if (extra) { console.log('Warnings:', extra); } }); // Compress a CSS file compressor.compress('/path/to/styles.css', { charset: 'utf8', type: 'css', 'line-break': 80 }, function(err, data, extra) { if (err) { console.error('Compression error:', err); return; } console.log('Compressed CSS:', data); }); // Compress a JavaScript string directly var jsCode = 'var x = (function() { var foo = 1, bar = 2; return (foo + bar) }())'; compressor.compress(jsCode, { type: 'js' }, function(err, data) { if (err) { console.error('Error:', err); return; } // Output: var x=(function(){var b=1,a=2;return(b+a)}()); console.log('Minified:', data); }); ``` -------------------------------- ### Prevent Variable Obfuscation with JavaScript Hints Source: https://context7.com/yui/yuicompressor/llms.txt Employ special hint strings at the beginning of JavaScript function bodies to prevent specific variables from being obfuscated during compression. These hints are removed in the output. ```javascript // Use hints to prevent specific variables from being munged function myFunction(arg1, arg2, arg3) { "arg2:nomunge, localVar:nomunge, nestedFn:nomunge"; var localVar = arg1 + arg2; function nestedFn() { return localVar + arg3; } return nestedFn(); } // The hint string is removed during compression // arg2, localVar, and nestedFn will keep their original names // Other variables will be obfuscated to shorter names ``` -------------------------------- ### Compress files in Node.js Source: https://github.com/yui/yuicompressor/blob/master/README.md Use the require('yuicompressor') module to compress JavaScript or CSS strings and files asynchronously. ```javascript 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 }); ``` -------------------------------- ### YUI Compressor - JavaScript-Specific Options Source: https://context7.com/yui/yuicompressor/llms.txt Options that apply only to JavaScript compression, controlling obfuscation and optimization behavior. ```APIDOC ## YUI Compressor - JavaScript-Specific Options ### Description Options that apply only to JavaScript compression, controlling obfuscation and optimization behavior. ### Options - `--nomunge`: Minify only, do not obfuscate local variables. - `--preserve-semi`: Preserve all semicolons (useful for JSLint compatibility). - `--disable-optimizations`: Disable all micro optimizations. - `-m `: Generate a munge mapping file (shows original -> obfuscated variable names). ### Usage Examples ```bash # Minify only, do not obfuscate local variables (nomunge) java -jar yuicompressor.jar --nomunge input.js -o output.js # Preserve all semicolons (useful for JSLint compatibility) java -jar yuicompressor.jar --preserve-semi input.js -o output.js # Disable all micro optimizations java -jar yuicompressor.jar --disable-optimizations input.js -o output.js # Combine multiple options java -jar yuicompressor.jar --nomunge --preserve-semi input.js -o output.js # Generate munge mapping file (shows original -> obfuscated variable names) java -jar yuicompressor.jar -m munge-map.txt input.js -o output.js ``` ``` -------------------------------- ### Compress CSS with Java API Source: https://context7.com/yui/yuicompressor/llms.txt Use the `CssCompressor` class in Java for compressing CSS files programmatically. The `compress` method takes a writer and an optional linebreak position. ```java import com.yahoo.platform.yui.compressor.CssCompressor; import java.io.*; public class CompressCSS { public static void main(String[] args) throws Exception { Reader in = new InputStreamReader(new FileInputStream("styles.css"), "UTF-8"); Writer out = new OutputStreamWriter(new FileOutputStream("styles.min.css"), "UTF-8"); CssCompressor compressor = new CssCompressor(in); in.close(); // Parameter:linebreak position (-1 for no line breaks) compressor.compress(out, -1); out.close(); } } ``` -------------------------------- ### Node.js compressString() Source: https://context7.com/yui/yuicompressor/llms.txt Low-level function to compress a string directly without file detection. ```APIDOC ## compressString(string, options, callback) ### Description Compresses a string directly in memory. ### Parameters - **string** (string) - Required - The code to compress. - **options** (object) - Required - Configuration object (charset, type). - **callback** (function) - Required - Callback function (err, data, extra). ### Request Example compressor.compressString(jsCode, { charset: 'utf8', type: 'js' }, function(err, data, extra) { ... }); ``` -------------------------------- ### Java JavaScriptCompressor Source: https://context7.com/yui/yuicompressor/llms.txt Programmatic JavaScript compression using the JavaScriptCompressor class. ```APIDOC ## JavaScriptCompressor.compress() ### Description Compresses JavaScript code using a Reader and Writer. ### Parameters - **out** (Writer) - Required - Output destination. - **mungemap** (Map) - Optional - Munge map. - **linebreak** (int) - Required - Line break position. - **munge** (boolean) - Required - Whether to obfuscate variables. - **verbose** (boolean) - Required - Verbose output. - **preserveSemi** (boolean) - Required - Preserve semicolons. - **disableOptimizations** (boolean) - Required - Disable optimizations. ``` -------------------------------- ### Compress JavaScript with Java API Source: https://context7.com/yui/yuicompressor/llms.txt Utilize the `JavaScriptCompressor` class within Java applications for programmatic JavaScript compression. Requires setting up an `ErrorReporter` for handling warnings and errors. ```java import com.yahoo.platform.yui.compressor.JavaScriptCompressor; import org.mozilla.javascript.ErrorReporter; import org.mozilla.javascript.EvaluatorException; import java.io.*; public class CompressJS { public static void main(String[] args) throws Exception { Reader in = new InputStreamReader(new FileInputStream("input.js"), "UTF-8"); Writer out = new OutputStreamWriter(new FileOutputStream("output.js"), "UTF-8"); ErrorReporter reporter = new ErrorReporter() { public void warning(String message, String sourceName, int line, String lineSource, int lineOffset) { System.err.println("[WARNING] " + line + ':' + lineOffset + ':' + message); } public void error(String message, String sourceName, int line, String lineSource, int lineOffset) { System.err.println("[ERROR] " + line + ':' + lineOffset + ':' + message); } public EvaluatorException runtimeError(String message, String sourceName, int line, String lineSource, int lineOffset) { error(message, sourceName, line, lineSource, lineOffset); return new EvaluatorException(message); } }; JavaScriptCompressor compressor = new JavaScriptCompressor(in, reporter); in.close(); // Parameters: out, mungemap, linebreak, munge, verbose, preserveSemi, disableOptimizations compressor.compress(out, null, -1, true, false, false, false); out.close(); } } ``` -------------------------------- ### CSS File Compression and Debugging Logic Source: https://github.com/yui/yuicompressor/blob/master/tools/cssmin-debugger.html This JavaScript code handles file selection, reading, and compression using YAHOO.compressor.cssmin. It requires the browser to support File and FileReader APIs. The 'changeHandler' function is attached to the file input's change event. ```javascript (function() { var dumpContents = function(node, str) { node.innerHTML = ""; node.appendChild(document.createTextNode(str)); }, testFile, changeHandler; if (window.File && window.FileReader) { testFile = document.getElementById('testFile'); changeHandler = function(e) { var file = this.files[0], fr = new FileReader(), input = document.getElementById("in"), output = document.getElementById("out"), contents; fr.onload = function(e) { dumpContents(input, e.target.result); var min = YAHOO.compressor.cssmin(e.target.result); dumpContents(output, min); }; fr.readAsText(file, "utf-8"); } if (testFile.addEventListener) { testFile.addEventListener('change', changeHandler, false); } else { testFile.attachEvent('onChange', changeHandler); } } else { document.getElementById("notsupportedmsg").removeClass("hidden"); } })(); ``` -------------------------------- ### Compress JavaScript String with Node.js API Source: https://context7.com/yui/yuicompressor/llms.txt Use the `compressString` function for direct string compression in Node.js when the content is already in memory. Specify the charset and type for accurate compression. ```javascript var compressor = require('yuicompressor'); var jsCode = '(function() { var w = window; w.hello = function(name) { w.alert("Hello, " + name); }; })();'; compressor.compressString(jsCode, { charset: 'utf8', type: 'js' }, function(err, data, extra) { if (err) { console.error('Error:', err); return; } // Output: (function(){var a=window;a.hello=function(b){a.alert("Hello, "+b)}})(); console.log('Minified:', data); }); ``` -------------------------------- ### Node.js Script for JavaScript Minification Source: https://context7.com/yui/yuicompressor/llms.txt A simple Node.js script to compress multiple JavaScript files using YUI Compressor. It iterates through a list of files, reads them from 'src/', compresses them, and writes the minified versions to 'dist/'. ```javascript // Simple Node.js build script var fs = require('fs'); var path = require('path'); var compressor = require('yuicompressor'); var files = ['app.js', 'utils.js', 'main.js']; files.forEach(function(file) { var inputPath = path.join('src', file); var outputPath = path.join('dist', file.replace('.js', '.min.js')); compressor.compress(inputPath, { type: 'js' }, function(err, data) { if (err) { console.error('Error compressing ' + file + ':', err); return; } fs.writeFileSync(outputPath, data); console.log('Compressed: ' + file); }); }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.