如何使用Google Closure编译器删除未使用的JavaScript代码? [英] How do I use Google Closure compiler to remove unused JavaScript code?

查看:107
本文介绍了如何使用Google Closure编译器删除未使用的JavaScript代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Google Closure编译器删除未使用的代码?

How do I use Google Closure compiler to remove unused code?

我使用的是JQuery Slider控件,但我没有在JQuery中使用任何其他内容。所以我读到高级模式下的Google Closure编译器可以删除未使用的代码,但我不知道如何

I am using the JQuery Slider control but am not using anything else within JQuery. So I read that Google Closure compiler in Advanced mode can remove unused code, but I don't know how.

我有 frontpage.html ,它链接到我网站上托管的html页面的外部JQuery,JQuery UI和JQuery Slider控件。

I have frontpage.html that links to an external JQuery, JQuery UI and JQuery Slider control from that html page hosted on my site.

在我的frontpage.html上,我还在HTML中嵌入了JavaScript来启动JQuery Slider控件。

On my frontpage.html, I also have JavaScript embedded within the HTML that initiates the JQuery Slider control.

我如何使用在线Closure Compiler 评估我的frontpage.html,JQuery,JQuery UI和JQuery Slider,以删除所有未使用的JQuery代码我不使用?

How do I use I use the online Closure Compiler to evaluate my frontpage.html, JQuery, JQuery UI, and JQuery Slider to remove all of the unused JQuery code that I don't use?

推荐答案

有两种方法可以从组合输出脚本中删除/排除未使用的代码:

There are two ways to remove/exclude unused code from your combined output script:


  • 第一种,涉及使用 ADVANCED_OPTIMIZATIONS 编译级别(参见高级编译和扩展以获取详细说明和用法)

  • 第二,是允许编译器管理依赖关系(即使用 - manage_closure_dependencies 编译标志

  • The first, involves using ADVANCED_OPTIMIZATIONS compilation level (refer to Advanced Compilation and Externs for detailed explanation and usage)
  • The second, is to allow the compiler to manage dependencies (i.e. using the --manage_closure_dependencies compilation flag

在这两个选项中,使用 - manage_closure_dependencies 删除未使用代码的标记不太精细(即在文件级别排除代码),但它确实允许您保持 SIMPLE_OPTIMIZATIONS 编译级别。在任何一种情况下,你都必须包含
jQuery和jQueryUI脚本的url以及你的页面中使用jQuery Slider的代码 - 这样的东西(第一个选项):

Of the two options, using the --manage_closure_dependencies flag to remove unused code is less granular (i.e. excludes code at a file level), but it does allow you to remain at SIMPLE_OPTIMIZATIONS compilation level. In either case, you will have to include the url to jQuery and jQueryUI scripts and your code that consumes the jQuery Slider from your page-something like this (1st option):

// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @output_file_name default.js
// @code_url http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js
// @code_url http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.0/jquery-ui.js
// ==/ClosureCompiler==

// ADD YOUR CODE HERE

希望这会有所帮助。

这篇关于如何使用Google Closure编译器删除未使用的JavaScript代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆