Ant和compiler合并壓縮js

來源:互聯網
上載者:User

首先下載ant和compiler
地址:http://ant.apache.org/
          http://code.google.com/p/closure-compiler/
[html] view plaincopy  www.2cto.com
<?xml version="1.0" encoding="UTF-8"?> 
<project name="Javascritp_build" default="clean" basedir="../"> 
      
    <description>Javascritp build for Ant</description> 
      
    <property name="src" location="js"/> 
    <property name="build" location="build"/> 
    <property name="target" location="result"/> 
    <property name="lib" location="lib"/> 
    <property name="charset" value="utf-8"/> 
      
    <!-- - - - - - - - - - - - - - - - - -  
    這個 ant 設定檔要經過4個流程: 
    1、target init 進行初始化處理,建立一個目錄build,用於暫存檔案; 
    2、target concat 合并兩個 js 檔案,放到 build 目錄下; 
    3、target compress 調用 Yui Compressor 對合并後的 js 進行壓縮 
    4、target clean 進行清理動作,刪除產生的 build 目錄 
  
    ANT標籤和屬性解釋: 
    project 的 default 對應某個 target 的 name 值,表示預設執行哪個步驟; 
    target 的 depends 對應其他某些 target 的 name 屬性,表示依賴性; 
    ${name} 可以引用 property 中定義的值。 
    mkdir 標籤建立一個目錄 
    replaceregexp, Regex替換,將DEBUG標識替換為空白,在正式環境不處理DEBUG資訊 
    注意設定檔案的 encoding 屬性,否則可能有亂碼情況 
  
    關於ANT的詳細文檔,請看官方手冊:http://ant.apache.org/manual/         
         - - - - - - - - - - - - - - - - - --> 
    <target name="init"> 
        <mkdir dir="${build}" /> 
    </target> 
      
    <target name="concat" depends="init"> 
        <concat destfile="${build}/all.js" encoding="${charset}" outputencoding="${charset}"> 
            <path path="${src}/core.js" /> 
            <path path="${src}/g.js" /> 
            <path path="${src}/nav.js" /> 
        </concat> 
          
        <!-- - - - - - - - - - - - - - - - - -  
            replaceregexp的說明    http://ant.apache.org/manual/Tasks/replaceregexp.html 
         - - - - - - - - - - - - - - - - - --> 
          
        <replaceregexp match="DEBUG" replace="" flags="g" byline="true" file="${build}/all.js" encoding="${charset}" /> 
    </target> 
      
    <!-- - - - - - - - - - - - - - - - - -  
        YUICompressor參數 http://developer.yahoo.com/yui/compressor/#work 
          
        通用參數: 
            -h, \-\-help                 顯示協助資訊 
           \-\-type <js|css>            指定輸入檔案的檔案類型 
           \-\-charset <charset>        指定讀取輸入檔案使用的編碼 
           \-\-line-break <column>      在指定的列後插入一個 line-bread 符號 
           \-v, \-\-verbose              顯示info和warn層級的資訊 
           -o <file>                  指定輸出檔案。預設輸出是控制台。 
          
        JavaScript專用參數: 
             \-\-nomunge                  只壓縮, 不對局部變數進行混淆。 
           \-\-preserve-semi            保留所有的分號。 
           \-\-disable-optimizations    禁止最佳化。 
    - - - - - - - - - - - - - - - - - --> 
    <target name="compress" depends="concat"> 
        <echo message="start compress" /> 
        <java jar="${lib}/yuicompressor-2.4.2.jar" fork="true" failonerror="false"> 
            <arg line="--type js --charset ${charset} --nomunge ${build}/all.js -o ${target}/core.js" /> 
        </java> 
        <echo message="end compress" /> 
    </target> 
          
    <target name="clean" depends="compress"> 
        <delete dir="${build}"/> 
    </target> 
      
</project> 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.