论坛首页 Web前端技术论坛

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

浏览 4178 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-05-21   最后修改:2012-05-21
如果你不知道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.


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

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





最后看看实际效果图吧:



最后附上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>

  • UglifyJS.7z (44.2 KB)
  • 描述: windows右键工具
  • 下载次数: 125
  • 大小: 66.7 KB
  • 大小: 44.8 KB
  • 大小: 1.2 MB
   发表时间:2012-05-29  
一开始没注意phase,所以导致不能把压缩后的js和jsp打包到war包里面,解决办法:


<!-- 思路:打包直接把js和jsp压缩后复制到目的地,在war插件的复制里面,排除刚刚复制的js和jsp,这样就不会覆盖了。打包的时候也能打进去了。 -->
<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>prepare-package</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.out.directory" value="${project.build.directory}/${project.build.finalName}/WEB-INF" />
								<property name="jsp.compress.directory" value="src/main/webapp/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.out.directory}">
									<fileset dir="${jsp.compress.directory}" includes="**/*-min.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.out.directory" value="${project.build.directory}/${project.build.finalName}/resources/js" />
								<property name="js.compress.directory" value="src/main/webapp/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.out.directory}">
									<fileset dir="${js.compress.directory}" includes="**/*-min.js" />
									<mapper type="regexp" from="^(.*)-min\.(js)$" to="\1.\2" />
								</move>
								<echo message="成功用uglifyjs压缩 JS 文件" />
								
							</target>
						</configuration>
					</execution>
				</executions>
			</plugin>
<plugin>
				<!-- 打war包插件 -->
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.2</version>
				<configuration>
				<!-- 声明:packagingExcludes中的*.properties文件均位于src/main/resources目录中 warSourceExcludes中的?test/*,venue/**位于src/main/webapp目录中 -->
					<warSourceExcludes>resources/js/**/*.js,WEB-INF/views/**/*.jsp</warSourceExcludes>
					<archive>
						<!-- 不把maven的依赖加入 -->
						<addMavenDescriptor>false</addMavenDescriptor>
					</archive>
				</configuration>
			</plugin>


0 请登录后投票
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics