如何使用Google的Closure编译JavaScript

发布于 2024-08-10 03:30:50 字数 288 浏览 12 评论 0原文

Google 刚刚发布了 Closure,这是一个用于缩小 JavaScript 的编译器。

在产品网站上,它说“Closure Compiler 也已与 Page Speed 集成”。

如何使用 Page Speed 来编译带有 Closure 的网页 JavaScript?

(或者,是否有一个网站,我可以简单地粘贴到我的 JavaScript 中以使其关闭缩小?

Google just released Closure, which is a compiler to minify JavaScript.

On the product site, it says "The Closure Compiler has also been integrated with Page Speed".

How do I use Page Speed to compile my web pages JavaScript with Closure?

(Or, is there a web site that I can simply paste in my JavaScript to have closure minify it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

蓝戈者 2024-08-17 03:30:50

对于单个文件,这很简单

java -jar $path_to_jar/compiler.jar --js input_file.js \
          --js_output_file output_file.js

对于多文件项目,您可以使用 calcdeps .py编译器结合使用。 jar

#!/bin/sh$
$CALCDEPS_PATH=/path/to_calcdeps  #directory containing calcdeps.py
$JAR_PATH=/path/to_jar            #directory containing compiler.jar
$CLOSURE_PATH=/path/to_closure    #contains directory "closure"
$CALCDEPS_PATH/calcdeps.py --path $CLOSURE_PATH \
                           --path . \
                           --compiler_jar $JAR_PATH/compiler.jar \
                           --input main_project_file.js \
                           --output_mode compiled \
                           > compiled_project_file.js

这样编译器就可以提供有关类型错误等有意义的信息。类型错误可以在编译时捕获,因为 compiler.jar 使用 某些用于类型信息的 JSDoc 注释

额外的编译器标志可以与 -f--compiler_flags 选项一起传递给 calcdeps.py

如果您想使用高级优化设置

--compiler_flags "--compilation_level=ADVANCED_OPTIMIZATIONS"

注意双引号和等号 - 必须在 bash 中使用该格式

For a single file it's simple

java -jar $path_to_jar/compiler.jar --js input_file.js \
          --js_output_file output_file.js

For a multi-file project you can use calcdeps.py in combination with the compiler.jar

#!/bin/sh$
$CALCDEPS_PATH=/path/to_calcdeps  #directory containing calcdeps.py
$JAR_PATH=/path/to_jar            #directory containing compiler.jar
$CLOSURE_PATH=/path/to_closure    #contains directory "closure"
$CALCDEPS_PATH/calcdeps.py --path $CLOSURE_PATH \
                           --path . \
                           --compiler_jar $JAR_PATH/compiler.jar \
                           --input main_project_file.js \
                           --output_mode compiled \
                           > compiled_project_file.js

That way compiler gives meaningful information about type errors, etc. Type errors can be caught at compile time because compiler.jar uses certain JSDoc comments for type information.

Extra compiler flags can be passed to calcdeps.py along with -f or --compiler_flags options

If you want to use advanced optimizations set

--compiler_flags "--compilation_level=ADVANCED_OPTIMIZATIONS"

notice the double quotes and the equal sign - had to use that format in bash

樱花坊 2024-08-17 03:30:50

Closure 编译器现在可以作为 JavaScript 应用程序使用。不再需要 Java 依赖项

有几种方法可以与其集成。我已经将其作为 Rollup

ex 的一部分完成:

import rollup from 'rollup';
import closure from 'rollup-plugin-closure-compiler-js';

export default {
  entry: 'index.js',
  dest: 'dist/build.js',
  format: 'iife',
  plugins: [
    closure({
      languageIn: 'ECMASCRIPT6',
      languageOut: 'ECMASCRIPT5',
      compilationLevel: 'ADVANCED',
      warningLevel: 'VERBOSE',
      externs: [{src:`
                      var jQuery;
                      jQuery.fadeIn = function() {};  

                      var ko;  
                      ko.applyBindings = function(vm) {};
                      ko.computed = function(a,b) {};
                      ko.observable = function(a) {};
               `}],
    })
  ]
}

更多信息在这里:

http://www.syntaxsuccess.com/viewarticle/using-the-closure-compiler---advanced_optimizations

The Closure compiler is now available as a JavaScript application. No need for the Java dependency anymore

There are a few ways to integrate with it. I have done it as part of Rollup

ex:

import rollup from 'rollup';
import closure from 'rollup-plugin-closure-compiler-js';

export default {
  entry: 'index.js',
  dest: 'dist/build.js',
  format: 'iife',
  plugins: [
    closure({
      languageIn: 'ECMASCRIPT6',
      languageOut: 'ECMASCRIPT5',
      compilationLevel: 'ADVANCED',
      warningLevel: 'VERBOSE',
      externs: [{src:`
                      var jQuery;
                      jQuery.fadeIn = function() {};  

                      var ko;  
                      ko.applyBindings = function(vm) {};
                      ko.computed = function(a,b) {};
                      ko.observable = function(a) {};
               `}],
    })
  ]
}

More info here:

http://www.syntaxsuccess.com/viewarticle/using-the-closure-compiler---advanced_optimizations

温柔嚣张 2024-08-17 03:30:50

“Page Speed 1.4 Beta 集成了 Closure Compiler,可以自动缩小 JavaScript 文件。但是,您需要单独下载并安装 Page Speed Beta 和 Closure Compiler。”

http://code.google.com/speed/page-speed/download。 html

我还没有安装这个版本,但我相当确定 Page Speed 会在其优化建议中为您提供编译后的代码。

"Page Speed 1.4 Beta integrates the Closure Compiler to minify JavaScript files automatically. However, you will need to download and install the Page Speed Beta and Closure Compiler separately."

http://code.google.com/speed/page-speed/download.html

I haven't installed this version yet, but I'm fairly certain that Page Speed will present you with compiled code in its optimization recommendations.

半窗疏影 2024-08-17 03:30:50

Closure Compiler 似乎已与 Page Speed 集成 仅适用于 Windows

It seems that Closure Compiler is integrated with Page Speed only for Windows.

痴者 2024-08-17 03:30:50

将闭包编译器与 PHP 一起使用(通过 CURL 托管或通过命令行工具本地托管)

http://bohuco.net/blog/2009/11/google-closure-compiler-with-php/

Use the closure compiler with PHP (hosted via CURL or local via command line tool)

http://bohuco.net/blog/2009/11/google-closure-compiler-with-php/

太阳公公是暖光 2024-08-17 03:30:50

如果需要编译多个js文件或者想简化编译过程,可以使用kjscompiler: https:// github.com/knyga/kjscompiler(基于googleclosure编译器)

If you need to compile multiple js files or if you would like to simplify compilation process, you may use kjscompiler: https://github.com/knyga/kjscompiler (based on google closure compiler)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文