通过压缩组件,可以显著减少HTTP请求和响应事件。这也是减小页面大小最简单的技术,但也是效果最明显的。

压缩JS,CSS代码有几种常用插件,YUI Compressor是个不错的选择。通过maven的YUI Compressor plugin可以方便的压缩项目中的前端代码。最简单便是将所有的文件一一压缩,看下pom.xml的配置:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 

    <modelVersion>4.0.0</modelVersion> 

    <groupId>KUI_demo</groupId> 

    <artifactId>KUI_demo</artifactId> 

    <version>1.0</version> 

    <packaging>jar</packaging> 

    <build> 

        <plugins> 

            <plugin> 

                <groupId>net.alchim31.maven</groupId> 

                <artifactId>yuicompressor-maven-plugin</artifactId> 

                <version>1.3.0</version> 

               <executions>

                <execution>

                    <!-- YUI Compressor has two goals: compress and jslint -->

                    <goals>

                        <goal>compress</goal>

                    </goals>

                </execution>

            </executions>

            <configuration>

                <failOnWarning>false</failOnWarning>

                <!-- Break contiguous output at this column number. -->

                <!-- If this is zero, you will get line breaks after every line in the aggregated file. -->

                <linebreakpos>-1</linebreakpos>

                <!-- Set your own suffix. [default: "-min"]-->

                <nosuffix>true</nosuffix> 

            </configuration>

            </plugin> 

        </plugins> 

        <resources> 

            <resource> 

                <directory>${basedir}/webapp</directory> 

                <includes> 

                    <include>**/*.js</include> 

                    <include>**/*.css</include> 

                </includes> 

            </resource> 

        </resources> 

    </build> 

</project> 

更进一步,将所有文件合并到同一个文件,可以减少HTTP请求。由于项目开发阶段并不会压缩所有代码,因此,可以采取通过一个JS引进所有其他文件。后期压缩的时候,将所有代码压缩进该文件。

看看复杂一点的需求:

  1. 先压缩文件,并排除部分已压缩的文件。
  2. 合并已压缩文件,并排除部分不需要合并的文件。
  3. 将压缩好的文件放到指定文件夹。

先看看合并的基本配置:

<project>

...

  <build>

    <plugins>

...

      <plugin>

        <groupId>net.alchim31.maven</groupId>

        <artifactId>yuicompressor-maven-plugin</artifactId>

        <executions>

          <execution>

            <goals>

              <goal>compress</goal>

            </goals>

          </execution>

        </executions>       

        <configuration>

          <nosuffix>true</nosuffix>

          <aggregations>

            <aggregation>

              <!-- remove files after aggregation (default: false)

              <removeIncluded>true</removeIncluded>

              -->

              <!-- insert new line after each concatenation (default: false) -->

              <insertNewLine>true</insertNewLine>

              <output>${project.build.directory}/${project.build.finalName}/static/all.js</output>

              <!-- files to include, path relative to output's directory or absolute path-->

              <!--inputDir>base directory for non absolute includes, default to parent dir of output</inputDir-->

              <includes>

                <include>${basedir}/src/licenses/license.js</include>

                <include>**/*.js</include>

              </includes>

              <!-- files to exclude, path relative to output's directory

              <excludes>

                <exclude>**/*.pack.js</exclude>

                <exclude>**/compressed.css</exclude>

              </excludes>

              -->

            </aggregation>

          </aggregations>

        </configuration>

      </plugin>

...

    </plugins>

  </build>

...

</project>

注意aggregation的inputDir非常重要,它默认为outputDir的上一层。并且aggregation的动作是在yuicompression之后的。因此,上面的配置实际上是先将代码压缩到${project.build.directory}/${project.build.finalName}目录下再进行aggregation。

          <configuration>

               <!-- exclude file witch not need to compress -->

                <excludes>

                    <exclude>**/*.min.js</exclude>

                    <exclude>**/KUI.js</exclude>

                </excludes>

                <includes>

                    <include>**/*.js</include>

                    <include>**/*.css</include>

                </includes>

                <linebreakpos>-1</linebreakpos>

                <nosuffix>false</nosuffix> 

                <suffix>.min</suffix> 

                <preProcessAggregates>false</preProcessAggregates>

                <preserveAllSemicolons>true</preserveAllSemicolons>

                <failOnWarning>false</failOnWarning>

                <aggregations>

                    <!--  JS concatenation -->

                    <aggregation>

                        <inputDir>${project.build.directory}</inputDir>

                        <!-- Insert a new line after each concatenation. -->

                        <insertNewLine>true</insertNewLine>

                        <removeIncluded>false</removeIncluded>

                        <!-- Pathname of final output. -->

                        <output>${basedir}/webapp/KUI.min.js</output>

                        <!-- Files to include, path relative to output directory OR absolute path -->

                        <includes>

                            <inlcude>**/*.min.js</inlcude>

                        </includes>

                        <excludes>

                            <exclude>**/KUI.js</exclude>

                            <exclude>**/KUI.min.js</exclude>

                        </excludes>

                    </aggregation>

                    <!--  CSS concatenation -->

                    <aggregation>

                        <inputDir>${project.build.directory}</inputDir>

                        <!-- Pathname of final output. -->

                        <output>${basedir}/webapp/content.min.css</output>

                        <!-- Files to include, path relative to output directory OR absolute path -->

                        <includes>

                            <include>**/*.min.css</include>

                        </includes>

                    </aggregation>

                    <excludes>

                        <exclude>**/content.min.css</exclude>

                    </excludes>

                </aggregations>

            </configuration>

还有一点要注意,如果对象的属性名为Javascript的保留字,并且没有用引号引住属性名,那么在压缩的时候会抛出错误。

var test = {

long : “test”  // throw error when compress

}

var test = {

“long” : “test”  // correct.

}

前端代码优化: 使用YUI Compressor的更多相关文章

  1. YUI Compressor for Sublime text2

    YUI Compressor 是一个用来压缩 JS 和 CSS 文件的工具,采用Java开发. 最近压缩文件,常使用在线压缩的方式来压缩文件,一来多有不便,二来如果没有网络,只能搁置了.本文来描述如何 ...

  2. GitHub 上一份很受欢迎的前端代码优化指南

    http://segmentfault.com/a/1190000002587334?utm_source=weekly&utm_medium=email&utm_campaign=e ...

  3. C#Css/Js静态文件压缩--Yui.Compressor.Net

    一.Asp.Net 自带静态文件压缩工具包 Microsoft.AspNet.Web.Optimization http://www.nuget.org/packages/Microsoft.AspN ...

  4. YUI Compressor压缩失效的场景-eval和with

    一.官方文档的说明 in the face of evil features such as eval or with, the YUI Compressor takes a defensive ap ...

  5. YUI Compressor

    简介 根据雅虎卓越性能团队的说法,40%到60%的雅虎用户拥有空闲缓存体验,所有页面浏览量中约有20%是使用空缓存完成的(请参阅Tenni Theurer在YUIBlog上的这篇文章)有关浏览器缓存使 ...

  6. YUI Compressor JS和CSS压缩工具使用方式(使用前安装JDK)

    压缩测试: 选中 test.js, 执行右键菜单“Process with &YUICompressor”,会生成 test-min.js. 注意事项: 1. 需要安装 JDK >= 1 ...

  7. 【转载】Yui.Compressor高性能ASP.NET开发:自动压缩CSS、JS

    在开发中编写的js.css发布的时候,往往需要进行压缩,以减少文件大小,减轻服务器的负担.这就得每次发版本的时候,对js.js进行压缩,然后再发布.有没有什么办法,让代码到了服务器上边,它自己进行压缩 ...

  8. YUI Compressor 压缩 JavaScript 原理-《转载》

    YUI Compressor 压缩 JavaScript 的内容包括: 移除注释 移除额外的空格 细微优化 标识符替换(Identifier Replacement) YUI Compressor包括 ...

  9. YUI Compressor是如何压缩JS代码的?

    YUI Compressor 压缩 JavaScript 的内容包括: 移除注释 移除额外的空格 细微优化 标识符替换(Identifier Replacement) YUI Compressor 包 ...

随机推荐

  1. Python进阶02 文本文件的输入输出

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! Python具有基本的文本文件读写功能.Python的标准库提供有更丰富的读写功能 ...

  2. 创建一个hibernate helloword

    1.下载hibernate包 http://hibernate.org/orm/ 点download下载最终版 安装hibernate插件hibernatetools 2.创建一个java工程 导入必 ...

  3. 算法库:Matlab与C++混合编程

    算法库:Matlab与C++混合编程 最近做光流算法预演过程中,下载的源码中涉及到了Matlab和C++的混合编程.在同事Matlab2014的环境下,程序到是一下就运行通过了.但在我这Matlab2 ...

  4. codeforces 2B The least round way 【DP】

    VJ上可找到中文题意. 思路: 首先分解有多少2与多少5.接下来就是dp. 分两次,一次是根据2的数量贪心,另外一次是根据5的数量贪心,看哪一次乘积的末尾0最少. 需要注意的是两点: 1.输入有0的情 ...

  5. How to Release the Temp LOB Space and Avoid Hitting ORA-1652 (文档 ID 802897.1)

    APPLIES TO: Oracle Database - Enterprise Edition - Version 8.1.5.0 and laterInformation in this docu ...

  6. (转)直接拿来用!最火的iOS开源项目(一)

    1. AFNetworking 在众多iOS开源项目中,AFNetworking可以称得上是最受开发者欢迎的库项目.AFNetworking是一个轻量级的iOS.Mac OS X网络通信类库,现在是G ...

  7. 制作chm无搜索标签解决方法

    chm无搜索标签解决方法: 1.hpp文件中设置Full-text search=Yes 2.下面[windows]有个数字设置为0x420, 网上普通流传的是0x20 === chm制作一般要三个文 ...

  8. VC 获 取 当前程序运行路径的几种方法

    1.使用APi函数GetModuleFileName char path[MAX_PATH]; GetModuleFileName(NULL, path, MAX_PATH);        //获取 ...

  9. 怎样调整CODESOFT中条码线的宽度

       CODESOFT是一款功能强大.灵活便捷的标签条码设计打印软件.在使用CODESOFT设计并打印标签,有时会因为打印精度或扫面清晰度等原因,需要调整条形码中行的宽度,即调整条码线宽度.本文,小编 ...

  10. mysql创建远程用户

    grant all privileges on *.* to myuser@"%" identified by 'password'; 用root用户登陆,然后: grant al ...