zoukankan      html  css  js  c++  java
  • Google的js压缩与优化工具: Getting Started with the Closure Compiler Application

    https://developers.google.com/closure/compiler/docs/gettingstarted_app

    Getting Started with the Closure Compiler Application

    The Hello World of the Closure Compiler Application

    The Closure Compiler application is a Java command-line utility that compresses, optimizes, and looks for mistakes in your JavaScript. To try out the Closure Compiler application with a simple JavaScript program, follow the steps below.

    To work through this exercise you need the Java Runtime Environment version 6.

    1. Download the Closure Compiler package

      Create a working directory called closure-compiler.

      Download the Closure Compiler compiler.jar file and save it in closure-compiler.

    2. Create a JavaScript file

      Create a file named hello.js containing the following JavaScript:

      // A simple function.
      function hello(longName){
        alert
      ('Hello, '+ longName);
      }
      hello
      ('New User');

      Save this file in the closure-compiler directory.

    3. Compile the JavaScript file

      Run the following command from the closure-compiler directory:

      java -jar compiler.jar --js hello.js --js_output_file hello-compiled.js
      

      This command creates a new file called hello-compiled.js, which contains the following JavaScript:

      function hello(a){alert("Hello, "+a)}hello("New User");

      Note that the compiler has stripped comments, whitespace and an unnecessary semi-colon. The compiler has also replaced the parameter name longName with the shorter name a. The result is a much smaller JavaScript file.

      To confirm that the compiled JavaScript code still works correctly, include hello-compiled.js in an HTML file like this one:

      <html>
      <head><title>Hello World</title></head>
      <body>
      <scriptsrc="hello-compiled.js"></script>
      </body>
      </html>

      Load the HTML file in a browser, and you should see a friendly greeting!

    Next Steps

    This example illustrates only the most simple optimizations performed by the Closure Compiler. To learn more about the compiler's capabilities, read Advanced Compilation and Externs.

    To learn more about other flags and options for the Closure Compiler, execute the jar with the --help flag:

    java -jar compiler.jar --help
  • 相关阅读:
    TFS 安装遇到的问题
    批量将MP4 转换为 MP3
    sqlite like 通配符 ,匹配区分大小写(默认不区分大小写)
    AutoCAD 2007-2012 长度统计工具
    python27 ImportError: No module named site
    github push时,要求密码的问题
    sqlserver中自定义计算函数
    关于win10家庭版不能开启虚拟机的问题
    js的MD5实现
    高德各省行政区显示不同区别颜色(转)
  • 原文地址:https://www.cnblogs.com/wucg/p/2395280.html
Copyright © 2011-2022 走看看