UglifyJS压缩js文件和jsp里面的js代码

如果你不知道UglifyJS就out了,jquery的官方压缩工具啦~
本来UglifyJS没有windows的客户端压缩工具,后来自己参照YUI的BAT工具(好像是淘宝一个哥们写的)改写了一个,具体见下面的说明。

REM =====================================
REM YUI Compressor CMD Script
REM
REM - by yubo@taobao.com
REM - 2009-02-12
REM =====================================


自己修改后的:



UglifyJS


安装指南
=========

安装步骤:

0. 安装nodejs运行环境:http://nodejs.org/,设置nodejs的安装目录到PATH中,一句话CMD中输入 node --version:
--->C:\Users\xin>node --version
--->v0.6.15
1. 安装请点击 install.cmd
2. 卸载请点击 uninstall.cmd
3. 如果安装过之前的版本,请先卸载老版本


压缩测试:

选中 test.js, 执行右键菜单“Process with UglifyJS”,会生成 test-min.js.


[color=red][size=large]注意需要nodejs环境,怎么安装?自己谷歌吧,还有jsp里面的script标签要标准哦,只能是:"<script>"和'<script type="text/javascript">',其他的自己去写吧,text.indexOf('<script type="text/javascript">')[/size][/color]

[size=medium]当然我觉得这个工具最好能支持[color=red]压缩jsp里的js代码[/color]啦,所以自己看了下bin下面的uglifyjs文件,无非就是一些js的函数,找到了修改代码的切入点:output(squeeze_it(text)),接下来就好办了,下图为修改前后的对比[/size]:

[img]http://dl.iteye.com/upload/attachment/0068/4413/761b9cce-bc3e-390b-bc8e-ddb47d12c108.jpg[/img]

[img]http://dl.iteye.com/upload/attachment/0068/4417/21737dfc-3cf1-35cf-9900-8f46d8768c5a.jpg[/img]

最后看看实际效果图吧:

[img]http://dl.iteye.com/upload/attachment/0068/4415/40da9bfe-5f41-3773-a42d-0749d017589f.jpg[/img]

最后附上maven下面的压缩脚本:


<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-config</id>
<goals>
<goal>copy-resources</goal>
</goals>
<phase>compile</phase>
<configuration>
<outputDirectory>${project.build.directory}</outputDirectory>
<resources>
<resource>
<directory>src/test/resources</directory>
<includes>
<include>uglify/*</include>
<include>uglify/lib/*</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<echo message="开始用uglifyjs压缩 JSP 文件. 请耐心等待!" />
<property name="uglifyjs" value="${project.build.directory}/uglify/uglifyjs" />
<property name="jsp.compress.directory" value="${project.build.directory}/${project.build.finalName}/WEB-INF" />
<apply executable="node" parallel="false" failonerror="true" dest="${jsp.compress.directory}">
<fileset dir="${jsp.compress.directory}" includes="**/*.jsp" />
<arg line=" ${uglifyjs}" />
<arg line=" --ascii" />
<arg line=" -o" />
<targetfile />
<srcfile />
<mapper type="regexp" from="^(.*)\.(jsp)$" to="\1-min.\2" />
</apply>
<move todir="${jsp.compress.directory}">
<fileset dir="${jsp.compress.directory}" includes="**/*.jsp" />
<mapper type="regexp" from="^(.*)-min\.(jsp)$" to="\1.\2" />
</move>
<echo message="成功用uglifyjs压缩 JSP 文件" />

<echo message="开始用uglifyjs压缩 JS 文件. 请耐心等待!" />
<property name="uglifyjs" value="${project.build.directory}/uglify/uglifyjs" />
<property name="js.compress.directory" value="${project.build.directory}/${project.build.finalName}/resources/js" />
<apply executable="node" parallel="false" failonerror="true" dest="${js.compress.directory}">
<fileset dir="${js.compress.directory}" includes="**/*.js" />
<arg line=" ${uglifyjs}" />
<arg line=" --ascii" />
<arg line=" -o" />
<targetfile />
<srcfile />
<mapper type="regexp" from="^(.*)\.(js)$" to="\1-min.\2" />
</apply>
<move todir="${js.compress.directory}">
<fileset dir="${js.compress.directory}" includes="**/*.js" />
<mapper type="regexp" from="^(.*)-min\.(js)$" to="\1.\2" />
</move>
<echo message="成功用uglifyjs压缩 JS 文件" />

</target>
</configuration>
</execution>
</executions>
</plugin>
<!-- CSS压缩 -->
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<version>1.3.0</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>compress</goal>
</goals>
<configuration>
<includes>
<include>**/*.css</include>
</includes>
<nosuffix>true</nosuffix><!-- 不需要后缀 -->
<linebreakpos>-1</linebreakpos><!-- 是否在一行 -->
</configuration>
</execution>
</executions>
</plugin>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值