Use YUI + Ant to implement js css compression, yuiant
Today I have studied the YUI yahoo open-source framework.
As a result, I have implemented an ant Implementation of YUI. Many implementations of bat on the Internet allow me to develop a unique article about ant implementation. If this article is reproduced, it indicates the author and website
Copyright: Mr. chen
The specific operations are as follows:
Official Website:
Yuicompressor-2.4.6.jar http://yuilibrary.com/downloads/#yuicompressor
YUIAnt. jar http://www.ubik-ingenierie.com/miscellanous/YUIAnt/
The specific code is as follows:
Xml Code
- # 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
Build. xml Code
- <? 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>
Xml Code
- @ 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
I can download related files. If you feel good, leave a message.
- YUICompressor.zip (1.5 MB)
Error in js compression by ant yui
If the js variable definition is incorrect, an ERROR occurs when the variable name is replaced during compression. Click [ERROR] 27: 82: invalid property id.
Come up
How to compress js
Search
Online JS/CSS/HTML compression (implemented using YUI Compressor) one of the open-source online tools in China