服务器之家

服务器之家 > 正文

使用YUI+Ant 实现JS CSS压缩

时间:2020-07-26 16:29     来源/作者:PHP教程网

今天研究了一下YUI yahoo开源框架,感觉很猛啊。

于是乎我做了一个YUI的ant实现,网上好多关于bat的实现,我就另辟蹊径,出个关于这个的ant实现,嘿嘿独一无二的文章,如果转载的话,其注明作者和网站

copyright:Mr.chen

好了具体操作如下:

官网:

yuicompressor-2.4.6.jar 下载地址 http://yuilibrary.com/downloads/#yuicompressor

YUIAnt.jar 下载地址 http://www.ubik-ingenierie.com/miscellanous/YUIAnt/

具体的相关代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#css work dir
commonCss.dir = css
 
#js work dir
commonJs.dir = js
 
#build temp dir 
output.temp.dir = build
 
#output files in the directory
output.dir = ${output.temp.dir}_output
 
#environment needs lib
liblib = lib
 
<?xml version="1.0" encoding="UTF-8"?>
 
<project name="Compress CSS-JS" default="compress" basedir=".">
 
  <property file="bulid.properties" />
 
  <path id="yuiClasspath">
    <fileset dir="${lib}">
      <include name="*.*" />
    </fileset>
  </path>
 
  <!-- #######################Init the environment of the tool ##########################-->
  <target name="init">
    <echo message="begin to init the init" />
    <echo message="delete all reference files." />
    <delete dir="${output.dir}" />
    <echo message="delete end" />
    <echo message="make the reference files." />
    <mkdir dir="${output.dir}" />
    <mkdir dir="${output.temp.dir}" />
    <echo message="make end." />
  </target>
 
  <!-- #######################Combine the css files       ##########################-->
  <target name="combinecss" depends="init" description="Combine common css files">
    <echo message="begin to combine the css files to one file." />
    <concat destfile="${output.temp.dir}/combined_css.css" encoding="UTF-8" append="false">
      <fileset dir="${commonCss.dir}">
        <include name="*.css" />
      </fileset>
    </concat>
    <echo message="combine end." />
  </target>
 
  <!-- #######################Combine the js files       ##########################-->
  <target name="combinejs">
    <echo message="begin to combine the js files to one file." />
    <concat destfile="${output.temp.dir}/all_source.js" encoding="utf-8" append="false">
      <fileset dir="${commonJs.dir}">
        <include name="*.js" />
      </fileset>
    </concat>
    <echo message="combine end." />
  </target>
 
  <!-- #######################Compress the js and css files ##########################-->
  <target name="compress" depends="combinecss,combinejs" description="Compress">
    <echo message="begin to compress the css file." />
    <taskdef name="yuicompress" classname="com.yahoo.platform.yui.compressor.YUICompressTask">
      <classpath>
        <path refid="yuiClasspath" />
      </classpath>
    </taskdef>
    <!-- first method compress the css files -->
    <yuicompress linebreak="10000000" warn="false" munge="yes" preserveallsemicolons="true" outputfolder="${output.dir}">
      <fileset dir="${output.temp.dir}">
        <include name="*.css" />
      </fileset>
    </yuicompress>
    <echo message ="compress the css end." />
    <!-- second method compress the js files-->
    <echo message ="begin to compress the js file." />
    <apply executable="java" parallel="false" failonerror="true">
      <fileset dir="${output.temp.dir}" includes="all_source.js" />
      <arg line="-jar" />
      <arg path="${lib}/yuicompressor-2.4.6.jar" />
      <arg line="--charset utf-8" />
      <arg line="-o ${output.dir}/combined_js.js" />
      <srcfile />
    </apply>
    <echo message ="compress the js end." />
    <delete dir="${output.temp.dir}" />
  </target>
 
</project>
 
@echo off
echo ################################################
echo ##########Tool Compress the js and css##########
echo ################################################
echo Please make sure your css and js in the css'directory and js'directory.
echo If sure,please enter any button to continue the tool.
pause
call ant -buildfile compress.xml compress>build.log
echo compress end
pause

 
 相关的文件我提供下载,感觉好的,就留言吧

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意 2019-07-07
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
返回顶部