如何使用Google Closure编译器删除未使用的代码?
我正在使用JQuery Slider控件但是我没有在JQuery中使用任何其他东西.所以我读到高级模式下的Google Closure编译器可以删除未使用的代码,但我不知道如何.
我有frontpage.html链接到我的网站上托管的html页面的外部JQuery,JQuery UI和JQuery Slider控件.
在我的frontpage.html上,我还在HTML中嵌入了JavaScript,启动了JQuery Slider控件.
我如何使用我使用在线Closure Compiler来评估我的frontpage.html,JQuery,JQuery UI和JQuery Slider来删除我不使用的所有未使用的JQuery代码?
javascript jquery jquery-ui google-closure google-closure-compiler
在Google Closure Compiler中,我收到了警告
警告 - 危险使用全局此对象
这是一个例子.错误行和偏移量指的是单词的开头this
function aToggle() {
if(shown)
toggle.show()
else
toggle.hide()
$(this).text(shown ? 'Click to hide' : 'Click to show')
shown = !shown
}
link.onclick = aToggle
Run Code Online (Sandbox Code Playgroud)
我只是将它改为匿名方法,但我aToggle在文件的其他地方重新使用,所以需要命名.
我可以标记aToggle为/**@constructor*/- 但它不是构造函数.是否有另一个注释我可以用来消除这个警告,或者我是否在将其标记为构造函数或者出现一堆无用的警告之间?
我已经看到Google Closure编译器在if子句中做了很多重写.例如:
if (a === 3) {…}
Run Code Online (Sandbox Code Playgroud)
转向
if (3 === a) {…}
Run Code Online (Sandbox Code Playgroud)
如果原语是第一个参数,那么在JavaScript中比较速度更快,或者这是什么原因?
我正在尝试连接到ClojureScript浏览器REPL,我遇到了麻烦clojure.browser.repl/connect.我编译的JavaScript会TypeError尝试调用顶部的Google Closure代码块中appendChild的null对象.我按照ClojureScript中的说明进行操作:启动和运行(第9章,第78页,预览版中提供),并想知道自发布以来这个工具是否发生了变化.
我正在使用Leiningen 2.0.0,Java 1.6.0_37,OS X 10.7.5,以及我的依赖项project.clj:
(defproject brepl-hello "0.1.0-SNAPSHOT"
:dependencies [[org.clojure/clojure "1.4.0"]
[org.clojure/clojurescript "0.0-1552"]
[compojure "1.1.5"]
[ring/ring-jetty-adapter "1.1.8"]]
:plugins [[lein-cljsbuild "0.3.0"]]
:source-paths ["src/clj"]
:cljsbuild {:builds [{
:source-paths ["src/cljs"]
:compiler {
:output-to "resources/public/brepl-hello.js"
:optimizations :whitespace
:pretty-print true}}]})
Run Code Online (Sandbox Code Playgroud)
这是唯一的ClojureScript源文件,src/cljs/brepl_hello/brepl-hello.cljs:
(ns brepl-hello
(:require [clojure.browser.repl :as repl]))
(repl/connect "http://localhost:9000/repl")
Run Code Online (Sandbox Code Playgroud)
这将编译到resources/public/brepl-hello.js我index.html在同一目录中插入的文件:
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript" src="brepl-hello.js"></script>
</head>
<body>
</body> …Run Code Online (Sandbox Code Playgroud) clojure google-closure google-closure-compiler leiningen clojurescript
任何人都知道我在哪里可以找到封闭编辑器的好插件.到目前为止我只发现:
https://github.com/yinhm/google-closure-editor-image
和
http://github.com/shripadk/google-closure-image-plugin
图像,视频,HTML编辑,表格插件会很有用.另一个有趣的插件是让一个工具栏编辑多个字段的插件.(http://groups.google.com/group/closure-library-discuss/browse_thread/thread/6fd6d412406d40/113aca2e17b28d54)
理想的解决方案是编辑器的预编译版本,可以直接包含在页面中,只需要一些小配置(一厢情愿).
谢谢!
谷歌的Closure编译器有一个"@typedef"标签,但你可以在你的代码中使用它们吗?(我知道它会起作用,但它不受欢迎吗?)
所以这是我的类型
/**
* The plan object's typedef
* @typedef {Object}
*/
Types.Plan = {
"style": "bordersmall",
"width": "50%",
"height": "40%",
"x": "20%",
"y": "10%",
"clickable": true,
"moveable": true
};
然后我可以在我的JSDoc注释中使用该类型.
这允许我的IDE在传递的参数上给我自动完成功能
因此声明的对象不会在代码中的任何位置使用.
/**
* The Instructions class
* @param {Types.Plan} plan Plan for position and dimension
* @param {Manager} manager The manager
* @param {Instructions} parent This widget's parent's instructions
* @return {Instructions} Returns the new instructions object
*/
Instructions = function(plan, manager, parent){
plan.
}
这样可以吗?或者有更好的解决方案吗?
我一直在使用google javascript闭包库来处理各种组件,我也使用了它的依赖关系管理.我很好奇这与使用requirejs进行依赖关系管理相比如何.具体来说,有一个特别的理由选择一个而不是另一个吗?
如果我正在使用谷歌闭包库,是否有合理的理由使用requirejs来管理依赖项与使用闭包依赖项管理?是否存在需要js的标准,关闭不是吗?
最近我开始使用Google Closure Tools进行javascript开发.到目前为止,我已经习惯用CoffeeScript编写代码,但CoffeeScript生成的javascript 似乎与Google Closure Compiler的高级模式不兼容.
添加Google Closure支持的CoffeeScript编译器是否有任何扩展?
在jQuery中,您可以执行以下操作:
$('#j_unoffered').on('click', '.icon_del', function () {...
Run Code Online (Sandbox Code Playgroud)
j_unoffered如果icon_del单击任何带有类的后代元素,则会在触发的元素上放置一个处理程序.此外,它适用于任何后续创建的icon_del元素.
我可以在Closure中正常工作,其中点击是在元素本身上.
goog.events.listen(
goog.dom.getElement('j_unoffered'),
goog.events.EventType.CLICK,
function(e) {...
Run Code Online (Sandbox Code Playgroud)
我如何在Closure中指定一个父事件目标,它以与jQuery示例相同的方式为其子/后代工作?
我假设我需要以setParentEventTarget某种方式使用,但我不确定如何为DOM事件实现它.我发现的大多数文档都与自定义调度事件有关.
- 更新 -
我想知道这个相当简单的解决方案是否有任何问题:
goog.events.listen(
goog.dom.getElement('j_unoffered'),
goog.events.EventType.CLICK,
function(e) {
if (e.target.className.indexOf('icon_del') !== -1) {...
Run Code Online (Sandbox Code Playgroud)
它仍然this与父母绑定,但e.target允许解决方法.listen(opt_handler)中的第五个参数允许你绑定this到其他东西,所以我猜这也是一个途径.
javascript jquery event-handling event-delegation google-closure
我喜欢使用Closure库以及使用Closure Compiler和库背后的想法.我已经在几个项目中使用过它,例如Post This For Me.一个重新发布的项目.
我正在考虑在一个庞大的项目中使用Closure我已经开始在那里与许多开发人员合作,而这些开发人员并没有像我一样快.
我不喜欢使用Closure的一件事是在网络上缺乏良好的文档,帮助和示例.
我的问题是,开源Closure项目的未来是什么?谷歌放弃了吗?我知道它在Google内部使用了很多,但我很少看到它在网络上的其他地方使用过.我应该引导我的团队使用它还是我可能犯了一个大错?
在此先感谢您的帮助和建议.希望也许一些Google员工可能会看到这一点.
google-closure google-closure-library google-closure-compiler
google-closure ×10
javascript ×6
jquery ×2
clojure ×1
coffeescript ×1
jquery-ui ×1
jsdoc ×1
leiningen ×1
requirejs ×1