Use YUI + Ant to implement js css compression, yuiant

Source: Internet
Author: User

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
  1. # Css work dir
  2. CommonCss. dir = css
  3. # Js work dir
  4. CommonJs. dir = js
  5. # Build temp dir
  6. Output. temp. dir = build
  7. # Output files in the directory
  8. Output. dir =$ {output. temp. dir} _ output
  9. # Environment needs lib
  10. Liblib = lib
Build. xml Code
  1. <? Xml version = "1.0" encoding = "UTF-8"?>
  2. <Project name = "Compress CSS-JS" default = "compress" basedir = ".">
  3. <Property file = "bulid. properties"/>
  4. <Path id = "yuiClasspath">
  5. <Fileset dir = "$ {lib}">
  6. <Include name = "*. *"/>
  7. </Fileset>
  8. </Path>
  9. <! -- ###################### Init the environment of the tool ########## ################ -->
  10. <Target name = "init">
  11. <Echo message = "begin to init the init"/>
  12. <Echo message = "delete all reference files."/>
  13. <Delete dir = "$ {output. dir}"/>
  14. <Echo message = "delete end"/>
  15. <Echo message = "make the reference files."/>
  16. <Mkdir dir = "$ {output. dir}"/>
  17. <Mkdir dir = "$ {output. temp. dir}"/>
  18. <Echo message = "make end."/>
  19. </Target>
  20. <! -- ####################### Combine the css files ############ ############# -->
  21. <Target name = "combinecss" depends = "init" description = "Combine common css files">
  22. <Echo message = "begin to combine the css files to one file."/>
  23. <Concat destfile = "$ {output. temp. dir}/combined_css.css" encoding = "UTF-8" append = "false">
  24. <Fileset dir = "$ {commonCss. dir}">
  25. <Include name = "*. css"/>
  26. </Fileset>
  27. </Concat>
  28. <Echo message = "combine end."/>
  29. </Target>
  30. <! -- ####################### Combine the js files ############ ############# -->
  31. <Target name = "combinejs">
  32. <Echo message = "begin to combine the js files to one file."/>
  33. <Concat destfile = "$ {output. temp. dir}/all_source.js" encoding = "UTF-8" append = "false">
  34. <Fileset dir = "$ {commonJs. dir}">
  35. <Include name = "*. js"/>
  36. </Fileset>
  37. </Concat>
  38. <Echo message = "combine end."/>
  39. </Target>
  40. <! -- ###################### Compress the js and css files ########## ################ -->
  41. <Target name = "compress" depends = "combinecss, combinejs" description = "Compress">
  42. <Echo message = "begin to compress the css file."/>
  43. <Taskdef name = "yuicompress" classname = "com. yahoo. platform. yui. compressor. YUICompressTask">
  44. <Classpath>
  45. <Path refid = "yuiClasspath"/>
  46. </Classpath>
  47. </Taskdef>
  48. <! -- First method compress the css files -->
  49. <Yuicompress linebreak = "10000000" warn = "false" munge = "yes" preserveallsemicolons = "true" outputfolder = "$ {output. dir}">
  50. <Fileset dir = "$ {output. temp. dir}">
  51. <Include name = "*. css"/>
  52. </Fileset>
  53. </Yuicompress>
  54. <Echo message = "compress the css end."/>
  55. <! -- Second method compress the js files -->
  56. <Echo message = "begin to compress the js file."/>
  57. <Apply executable = "java" parallel = "false" failonerror = "true">
  58. <Fileset dir = "$ {output. temp. dir}" includes = "all_source.js"/>
  59. <Arg line = "-jar"/>
  60. <Arg path = "$ {lib}/yuicompressor-2.4.6.jar"/>
  61. <Arg line = "-- charset UTF-8"/>
  62. <Arg line = "-o $ {output. dir}/combined_js.js"/>
  63. <Srcfile/>
  64. </Apply>
  65. <Echo message = "compress the js end."/>
  66. <Delete dir = "$ {output. temp. dir}"/>
  67. </Target>
  68. </Project>
Xml Code
  1. @ Echo off
  2. Echo ####################################### #########
  3. Echo ######### Tool Compress the js and css ##########
  4. Echo ####################################### #########
  5. Echo Please make sure your css and js in the css 'Directory and js' directory.
  6. Echo If sure, please enter any button to continue the tool.
  7. Pause
  8. Call ant-buildfile compress. xml compress> build. log
  9. Echo compress end
  10. 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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.