scribe/yuicompressor-library

此包已废弃,不再维护。未建议替代包。

Yahoo的JS和CSS压缩器的最新分支,与上游保持同步,除了这个分支附带composer文件。

v2.4.10 2014-09-26 00:39 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:39:58 UTC


README

YUI Compressor是一个JavaScript压缩器,除了删除注释和空白外,还使用尽可能小的变量名来混淆局部变量。这种混淆是安全的,即使在使用“eval”或“with”等构造(尽管在这种情况下压缩效果不是最佳)的情况下也是如此。与jsmin相比,平均节省约20%。

YUI Compressor还能安全地压缩CSS文件。使用哪种压缩器的决定基于文件扩展名(js或css)。

构建

ant

测试

./tests/suite.sh

Node.js 包

你可以在Node.js包中引入压缩器,并异步压缩文件和字符串。它仍然在底层使用Java

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
});

选项

  • charset // 默认为 'utf8'
  • type // 默认为 'js'
  • line-break
  • nomunge
  • preserve-semi
  • disable-optimizations

待办事项

  • 更好的文档
  • 帮助页面

构建状态

Build Status

全局选项

-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)
    Specify 0 to get a line break after each semi-colon in JavaScript, and
    after each rule in CSS.

--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
    Display informational messages and warnings.

JavaScript 仅选项

--nomunge
    Minify only. Do not obfuscate local symbols.

--preserve-semi
    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
    Disable all the built-in micro optimizations.

备注

  • 如果没有指定输入文件,则默认为stdin。

  • 支持使用通配符指定多个输入文件。

  • YUI Compressor需要Java版本 >= 1.5。

  • 可以通过使用“提示”来防止局部变量、嵌套函数或函数参数被混淆。提示是一个字符串,位于函数体的开头,如下所示

function fn (arg1, arg2, arg3) {
    "arg2:nomunge, localVar:nomunge, nestedFn:nomunge";

    ...
    var localVar;
    ...

    function nestedFn () {
        ....
    }

    ...
}

提示本身会从压缩文件中消失。

  • /*!开始的C风格注释被保留。这对于包含版权/许可信息的注释很有用。从2.4.8版本开始,YUICompressor不再删除'!'。例如
/*!
 * TERMS OF USE - EASING EQUATIONS
 * Open source under the BSD License.
 * Copyright 2001 Robert Penner All rights reserved.
 */

在输出中保持不变,未受到YUICompressor的影响。

修改过的Rhino文件

YUI Compressor使用Rhino库的修改版本(http://www.mozilla.org/rhino/)。所做的更改是为了支持JScript条件注释、保留注释、正则表达式中的转义斜杠字符,并允许优化字符串文字中的转义引号。

版权和许可

版权(c)2013 Yahoo! Inc. 保留所有权利。本文件内容中的版权由Yahoo! Inc.根据BSD(修订版)开源许可证许可。