Version: 2.4.11-SNAPSHOT Group ID: org.codelibs Java: 11+
The YUI Compressor is a JavaScript compressor which, in addition to removing comments and white-spaces, obfuscates local variables using the smallest possible variable name. This obfuscation is safe, even when using constructs such as 'eval' or 'with' (although the compression is not optimal in those cases) Compared to jsmin, the average savings is around 20%.
The YUI Compressor is also able to safely compress CSS files. The decision on which compressor is being used is made on the file extension (js or css)
- CHANGELOG.md - Version history and changes
- docs/BUILDING.md - Detailed build instructions
- docs/TESTING.md - Testing guide
- docs/TOOLS.md - Development tools
- docs/README.md - Documentation index
yuicompressor/
├── src/
│ ├── main/java/ # Java source code
│ └── test/
│ ├── java/ # Java test code
│ └── resources/ # Test files (CSS/JS with .min versions)
├── nodejs/ # Node.js wrapper
├── tests/
│ └── node/ # Node.js integration tests
├── docs/ # Documentation (markdown)
├── tools/ # Development tools
├── pom.xml # Maven configuration
└── package.json # Node.js configuration
This project uses Maven for build management (requires Maven 3.8+ and Java 11+):
mvn clean packageThe compiled JAR will be available at target/yuicompressor-2.4.11-SNAPSHOT.jar
To run the JAR:
java -jar target/yuicompressor-2.4.11-SNAPSHOT.jar [options] [input file]To install to local Maven repository:
mvn clean installFor detailed build instructions, see docs/BUILDING.md
Run the comprehensive JUnit test suite:
mvn testThe test suite includes:
- JavaScript compression tests (variable obfuscation, minification, etc.)
- CSS compression tests (color optimization, whitespace removal, etc.)
- Command-line interface tests
Run the Node.js integration tests:
npm install
npm testFor more information on testing, see docs/TESTING.md
You can require compressor in a Node.js package and compress files and strings in async. It still uses Java under the hood
npm i yuicompressor
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
});Options:
charset// defaults to 'utf8'type// defaults to 'js'line-breaknomungepreserve-semi// accepted and ignored, see the CLI option belowdisable-optimizations// accepted and ignored, see the CLI option below
This project has been migrated from Ant to Maven. The following changes have been made:
- Build system: Ant (
build.xml) → Maven (pom.xml) - Dependencies:
lib/*.jar→ Maven dependencies - Test resources:
tests/→src/test/resources/ - Node.js tests:
nodejs_tests/→tests/node/ - Documentation: Consolidated in
docs/directory - Changelog:
doc/CHANGELOG→CHANGELOG.md
For contributors familiar with the old structure, please refer to the documentation in docs/ for the new organization.
-h, --help
Prints help on how to use the YUI Compressor
--line-break
Some source control tools don't like files containing lines longer than,
say 8000 characters. The linebreak option is used in that case to split
long lines after a specific column. It can also be used to make the code
more readable, easier to debug (especially with the MS Script Debugger)
A break is only ever inserted at a syntactically safe offset - between
statements in JavaScript, after a rule in CSS, never inside a token - so
a single statement or rule longer than the requested column is left on
one line rather than cut.
Specify 0 to get a line break after each rule in CSS. In JavaScript 0 is
a no-op: the JavaScript path requires a positive column, so it does NOT
give "a line break after each semi-colon" as earlier versions of this
document claimed. Use --line-break 1 for that.
--type js|css
The type of compressor (JavaScript or CSS) is chosen based on the
extension of the input file name (.js or .css) This option is required
if no input file has been specified. Otherwise, this option is only
required if the input file extension is neither 'js' nor 'css'.
--charset character-set
If a supported character set is specified, the YUI Compressor will use it
to read the input file. Otherwise, it will assume that the platform's
default character set is being used. The output file is encoded using
the same character set.
-o outfile
Place output in file outfile. If not specified, the YUI Compressor will
default to the standard output, which you can redirect to a file.
Supports a filter syntax for expressing the output pattern when there are
multiple input files. ex:
java -jar yuicompressor.jar -o '.css$:-min.css' *.css
... will minify all .css files and save them as -min.css
-v, --verbose
MOSTLY UNIMPLEMENTED. The flag reaches the CLI, where it enables one
informational line ("Using charset ...") when the requested charset is
unsupported and UTF-8 is substituted. The compressors themselves never
read it, so no other informational message or warning is produced.
--nomunge
Minify only. Do not obfuscate local symbols.
--preserve-semi
NOT CURRENTLY IMPLEMENTED - accepted and ignored.
Preserve unnecessary semicolons (such as right before a '}') This option
is useful when compressed code has to be run through JSLint (which is the
case of YUI for example)
--disable-optimizations
NOT CURRENTLY IMPLEMENTED - accepted and ignored.
Disable all the built-in micro optimizations.
-
If no input file is specified, it defaults to stdin.
-
Supports wildcards for specifying multiple input files.
-
The YUI Compressor requires Java version >= 11.
-
"Hints" are NOT CURRENTLY IMPLEMENTED. The syntax below is accepted by the parser like any other string statement, but the named symbols are obfuscated anyway AND the hint string is emitted into the compressed output as a live statement rather than disappearing. Restoring hint support is Release 2 work; until then, use
--nomungeif a symbol's name must survive.It was possible in earlier versions to prevent a local variable, nested function or function argument from being obfuscated by using "hints". A hint is a string that is located at the very beginning of a function body like so:
function fn (arg1, arg2, arg3) {
"arg2:nomunge, localVar:nomunge, nestedFn:nomunge";
...
var localVar;
...
function nestedFn () {
....
}
...
}
The hint itself used to disappear from the compressed file. It no longer does; see the note above.
- C-style comments starting with
/*!are preserved. This is useful with comments containing copyright/license information. As of 2.4.8, the '!' is no longer dropped by YUICompressor. For example:
/*!
* TERMS OF USE - EASING EQUATIONS
* Open source under the BSD License.
* Copyright 2001 Robert Penner All rights reserved.
*/
remains in the output, untouched by YUICompressor.
YUI Compressor uses a modified version of the Rhino library (http://www.mozilla.org/rhino/) The changes were made to support JScript conditional comments, preserved comments, unescaped slash characters in regular expressions, and to allow for the optimization of escaped quotes in string literals.
Copyright (c) 2013 Yahoo! Inc. All rights reserved. The copyrights embodied in the content of this file are licensed by Yahoo! Inc. under the BSD (revised) open source license.