通过压缩组件,可以显著减少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.

}

最新文章

  1. 深入理解Objective-C:Category
  2. des (C语言)
  3. Apache htpasswd命令用法详解
  4. MYSQL界面操作系统之phpMyAdmin
  5. iOS开发 在scrollView上增加滑动手势(Pan)
  6. Android利用V4包中的SwipeRefreshLayout实现上拉加载
  7. 链表:删除链表中重复的结点(java实现)
  8. 《深入浅出WPF》重点摘要(—)Binding自动通知机制
  9. jquery选择器:nth-child()与空格:eq() 的区别;
  10. Android利用网络编程HttpClient批量上传(两)AsyncTask+HttpClient监测进展情况,并上传
  11. ele.me在IOS浏览器端启动APP的技巧分析
  12. VFL语言
  13. Android设置item的行间距,以及去掉分割线方法
  14. &lt;第一站&gt;人生的第一个博客
  15. 【转载】Docker 安装后 报 Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? 解决办法
  16. 9个用来爬取网络站点的 Python 库
  17. asp.net webform/mvc导出Excel通用代码
  18. Adapter类 调用Activity中的函数
  19. OD之破解密钥文件授权(三)
  20. 使用Teleport Ultra批量克隆网站,使用Easy CHM合并生成chm文件

热门文章

  1. Python中在脚本中引用其他文件函数的方法
  2. SQL游标 更新
  3. IE10修改select样式
  4. Ext 4.2 RowEditing
  5. Sqlserver中存储过程,触发器,自定义函数(二)
  6. iframe 跨域自适应 纯css解决方法
  7. VS2013配置WTL90_4140_Final
  8. 1、程序启动原理和UIApplication【转】
  9. 解决Git报错:The current branch is not configured for pull No value for key branch.master.merge found in configuration
  10. [翻译]你不会想知道的kobject,kset,和ktypes