`
wbj0110
  • 浏览: 1586782 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

YUI Compressor

阅读更多

According to Yahoo!'s Exceptional Performance Team, 40% to 60% of Yahoo!'s users have an empty cache experience and about 20% of all page views are done with an empty cache (see this article by Tenni Theurer on the YUIBlog for more information on browser cache usage). This fact outlines the importance of keeping web pages as lightweight as possible. Improving the engineering design of a page or a web application usually yields the biggest savings and that should always be a primary strategy. With the right design in place, there are many secondary strategies for improving performance such as minification of the code, HTTP compression, using CSS sprites, etc.

In terms of code minification, the most widely used tools to minify JavaScript code are Douglas Crockford's JSMINthe Dojo compressor and Dean Edwards' Packer. Each of these tools, however, has drawbacks. JSMIN, for example, does not yield optimal savings (due to its simple algorithm, it must leave many line feed characters in the code in order not to introduce any new bugs).

The goal of JavaScript and CSS minification is always to preserve the operational qualities of the code while reducing its overall byte footprint (both in raw terms and after gzipping, as most JavaScript and CSS served from production web servers is gzipped as part of the HTTP protocol). The YUI Compressor is JavaScript minifier designed to be 100% safe and yield a higher compression ratio than most other tools. Tests on the YUI Library have shown savings of over 20% compared to JSMin (becoming 10% after HTTP compression). The YUI Compressor is also able to compress CSS files by using a port of Isaac Schlueter's regular-expression-based CSS minifier.

Quick Links

  • Documentation: Detailed description of the YUI Compressor and how to use it.
  • Release Notes: Detailed change log for the YUI Compressor.
  • CSS minification: Description of the CSS minification performed by the compressor.
  • License: All code specific to YUI Compressor is issued under a BSD license. YUI Compressor extends and implements code from Mozilla's Rhino project. Rhino is issued under the Mozilla Public License (MPL), and MPL applies to the Rhino source and binaries that are distributed with YUI Compressor, including Rhino modifications made by YUI Compressor. YUI Compressor also makes use of and distributes a binary ofJArgs; the JArgs BSD license applies to this binary.
  • Download: Download the YUI Compressor.

How does the YUI Compressor work?

The YUI Compressor is written in Java (requires Java >= 1.4) and relies on Rhino to tokenize the source JavaScript file. It starts by analyzing the source JavaScript file to understand how it is structured. It then prints out the token stream, omitting as many white space characters as possible, and replacing all local symbols by a 1 (or 2, or 3) letter symbol wherever such a substitution is appropriate (in the face of evil features such as evalor with, the YUI Compressor takes a defensive approach by not obfuscating any of the scopes containing the evil statement) The CSS compression algorithm uses a set of finely tuned regular expressions to compress the source CSS file. The YUI Compressor is open-source, so don't hesitate to look at the code to understand exactly how it works.

Using the YUI Compressor from the command line

$ java -jar yuicompressor-x.y.z.jar
Usage: java -jar yuicompressor-x.y.z.jar [options] [input file]

  Global Options
    -h, --help                Displays this information
    --type <js|css>           Specifies the type of the input file
    --charset <charset>       Read the input file using <charset>
    --line-break <column>     Insert a line break after the specified column number
    -v, --verbose             Display informational messages and warnings
    -o &lt;file&gt;                 Place the output into <file> or a file pattern.
                              Defaults to stdout.

  JavaScript Options
    --nomunge                 Minify only, do not obfuscate
    --preserve-semi           Preserve all semicolons
    --disable-optimizations   Disable all micro optimizations

GLOBAL OPTIONS

  -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 ONLY OPTIONS

  --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.

Note: If no input file is specified, it defaults to stdin.

The following command line (x.y.z represents the version number):

$ java -jar yuicompressor-x.y.z.jar myfile.js -o myfile-min.js

will minify the file myfile.js and output the file myfile-min.js. For more information on how to use the YUI Compressor, please refer to the documentation included in the archive.

The charset parameter isn't always required, but the compressor may throw an error if the file's encoding is incompatible with the system's default encoding. In particular, if your file is encoded in utf-8, you should supply the parameter.

$ java -jar yuicompressor-x.y.z.jar myfile.js -o myfile-min.js --charset utf-8

Additional notes

  • Don't hesitate to use the -v option. Although not a replacement for JSLint, it will output some helpful hints when it senses that something might be wrong with your code.
  • If you wish to minify your files on the backend (also known as on-the-fly minification) instead of at build time, you will want to cache the minified files in memory for optimal performance (instead of minifying the same files over and over & minification is a time consuming process) Note that the YUI Compressor can easily be instantiated and used from a Java-based environment (Servlet).

Support & Community

The YUI Library and related topics are discussed on the on the YUILibrary.com forums.

Also be sure to check out YUIBlog for updates and articles about the YUI Library written by the library's developers.

Filing Bugs & Feature Requests

YUICompressor uses Github Issues to track issues.

More Reading about JavaScript/CSS minification and the YUI Compressor

分享到:
评论

相关推荐

    yuicompressor-2.4.8.jar

    《yuicompressor-2.4.8.jar:前端优化与代码压缩的艺术》 在Web开发领域,优化前端性能是一项至关重要的任务,而yuicompressor-2.4.8.jar正是这样一款强大的工具,专为JavaScript代码压缩而设计,助力开发者实现...

    yuicompressor-yui compressor

    yuicompressor-2.4.2.jar yuicompressor-2.4.7.jar jsZip.exe yuicompressor yui compressor js压缩工具 javascript压缩工具 css压缩工具 ------------------------------------ //压缩JS java -jar yui...

    yuicompressor安装文件

    **JavaScript压缩工具——YUICompressor** YUICompressor是一款由Yahoo开发的高效JavaScript和CSS压缩工具。它通过删除空格、换行符以及不必要的字符,有效地减小了JavaScript和CSS文件的大小,从而提高了网页的...

    YUI compressor源码jar包

    1. **可执行文件**:`yuicompressor-2.4.2.jar`是YUI Compressor的一个版本,包含了所有必要的类和资源,可以直接通过命令行进行JavaScript和CSS文件的压缩。只需将这个jar文件添加到系统路径,就可以在终端中运行`...

    yuicompressor

    **yuicompressor** 是一个非常著名的前端资源优化工具,主要用于CSS和JavaScript文件的压缩。它由雅虎(Yahoo!)开发并开源,旨在提高网页加载速度,减少HTTP请求的大小,从而提升整体网站性能。这款工具能够删除...

    yuicompressor.zip

    《yuicompressor:高效能的JS与CSS压缩工具》 在Web开发中,为了提高页面加载速度和优化用户体验,对JavaScript(JS)和Cascading Style Sheets(CSS)进行压缩是一项必不可少的工作。yuicompressor,由雅虎(Yahoo...

    使用yuicompressor压缩及合并js,css静态资源

    java -jar yuicompressor.jar --type js -o script.min.js script.js ``` 这两个命令分别将原始的CSS和JavaScript文件压缩成更小的`style.min.css`和`script.min.js`。`-o`参数指定输出文件名,`--type js`指定了...

    YUI Compressor js css压缩工具

    在提供的文件中,`compressor.bat`是一个批处理脚本,用于调用`yuicompressor.jar`执行压缩任务。使用步骤如下: 1. **环境准备**:确保安装了Java运行环境(JRE)。 2. **运行脚本**:双击`compressor.bat`,或者...

    webstorm_phpstorm_yuicompressor-2.4.8.jar

    1. **下载与安装**:首先,你需要下载"phpstorm-yuicompressor-2.4.8"压缩包,解压后找到"yuicompressor-2.4.8.jar"文件。这是集成的关键组件。 2. **配置File Watcher**:在IDE中,选择`File` &gt; `Settings`...

    js、css 注释清理工具 (yuicompressor)

    为了解决这个问题,开发者通常会使用像`yuicompressor`这样的工具进行代码优化。 `yuicompressor`是由Yahoo开发的一款开源的JavaScript和CSS压缩工具。它通过删除不必要的空格、换行符以及注释,将源代码压缩到最小...

    YUI Compressor压缩JS和Css工具(包含详细配置)

    1. **命令行工具**:通过安装Java Development Kit (JDK),然后下载YUI Compressor的jar文件,可以在命令行中使用`java -jar yuicompressor.jar`命令进行压缩操作。 2. **集成开发环境插件**:许多IDE如Eclipse、...

    YUI compressor

    java -jar yuicompressor.jar input.css -o output.css --type css ``` 在Java项目中,可以将YUI Compressor集成到构建过程,如Maven或Gradle,以自动压缩源代码。 **四、YUI Compressor的局限性** 虽然YUI ...

    YUI compressor右键压缩脚本

    YUI Compressor是一款高效且功能强大的JavaScript和CSS代码压缩工具,由Yahoo!公司开发。这个“YUI compressor右键压缩脚本”提供了一种便捷的方式,使得用户无需通过命令行或图形用户界面(GUI)就能快速对JS...

    yuicompressor-maven-plugin

    **yuicompressor-maven-plugin详解** `yuicompressor-maven-plugin`是一款强大的Maven插件,主要用于优化前端资源,特别是JavaScript和CSS文件。这个插件是基于YUI Compressor,一个由Yahoo开发的开源工具,它能...

    YUI Compressor(JS压缩工具)

    **YUI Compressor** 是一个由Yahoo开发的开源JavaScript和CSS压缩工具,旨在减少Web页面加载时间,提高网站性能。它的主要功能是去除代码中的空白、注释,并进行变量名混淆,从而减小文件大小,加快网页加载速度。在...

    yui compressor 2.4.2 javascript/css压缩程序

    java -jar yuicompressor-2.4.2.jar -o compressed.js input.js ``` 对于CSS文件,命令类似,只是扩展名不同。此外,`YUI Compressor` 还支持通过参数配置压缩级别、是否保留注释等选项。 5. **与其他工具的比较...

    yuicompressor-2.4.8.zip js、css注释清理及压缩工具

    `yuicompressor-2.4.8.zip` 是一个前端开发中常用的工具,主要功能是对JavaScript和CSS文件进行注释清理和代码压缩,从而优化网页加载速度,提高用户体验。这个压缩工具出自YUI(Yahoo! User Interface Library)...

Global site tag (gtag.js) - Google Analytics