标签: google-closure-compiler

在调用函数时,如何让Closure Compiler停止抱怨union类型?

我在使用jQuery-1.4.3 externs文件时特别看到了这一点.这个javadocs读取

/**
 * @param {(string|number|function(number,number))=} arg1
 * @return {(number|jQueryObject)}
 * @nosideeffects
 */
jQueryObject.prototype.width = function(arg1) {};
Run Code Online (Sandbox Code Playgroud)

我有一个看起来像这样的电话:

   var w =  $(window).width();
    $('#whatever').width(w)
Run Code Online (Sandbox Code Playgroud)

Closure抱怨:警告 - jQueryObject.prototype.height的实际参数1与找到的形式参数不匹配:( jQueryObject | null | number)required :( function(number,number):?| number | string | undefined)$('# .Xobni的')的高度($(窗口).height());

从玩游戏(删除可能的返回类型),我可以看到问题是第一次调用width可能返回一个jQueryObject,因为那不是一个有效的输入,Closure给了我一个错误.我尝试添加这个:

/**
 * @type {number}
 */
var w =  $(window).width();
$('#Xobni').width(w);
Run Code Online (Sandbox Code Playgroud)

但是然后Closure抱怨:警告 - 初始化变量found:(jQueryObject | null | number)required:number var w = $(window).width();

问题是当width接受一个参数时,它返回一个jQueryObject.当它不参数时,它返回一个数字.所以我知道我的电话没问题,但javadocs并没有完全反映出来,所以Closure警告我.有没有办法适当地修复javadoc或告诉Closure我知道这个结果将是一个数字.我知道我可以抑制错误,但我想知道如何更好地注释这些事情.

谢谢您的帮助!

javascript javadoc google-closure google-closure-compiler

10
推荐指数
1
解决办法
2792
查看次数

在构建服务器上集成Google Closure Compiler和MS Build

我正在研究如何缩小javascript文件作为CI过程的一部分,以便我们可以在开发中使用未缩小的文件,并在部署到登台和实时服务器时自动压缩它们.

这是一个ASP.NET站点; 我们使用Hudson作为构建服务器.

我对Google Closure编译器很感兴趣,并且我遇到了这个.Net MSBuild Google Closure编译器任务,但它似乎并没有被广泛使用.是否有更好的选择与MSBuild一起使用,使用Closure或其他缩小工具?

msbuild continuous-integration minify google-closure google-closure-compiler

10
推荐指数
1
解决办法
2002
查看次数

如何使用JSDoc中的可选属性注释匿名对象

我有一个JavaScript类,它接受一个Object类型的参数和定义的属性集,当我注释它时,Closure Compiler很高兴:

@constructor
@param {{ subview:BaseView, el:(jQuery|Element), title:String }} options
var MyView = function(options){ }
Run Code Online (Sandbox Code Playgroud)

我想使titlekey可选并将title值传递给我的类的某些实例,并在此键不存在时实现回退行为,因此我使用以下内容对我的类进行了注释:

@constructor
@param {{ subview:BaseView, el:(jQuery|Element), title:String= }} options
var MyView = function(options){ }
Run Code Online (Sandbox Code Playgroud)

现在Closure Compiler正在抱怨:

WARNING - Bad type annotation. expected closing }
Run Code Online (Sandbox Code Playgroud)

我已经为Closure Compiler检查了Annotating JavaScript,但是我没有看到描述这种用例的单行.

javascript google-closure-compiler jsdoc

10
推荐指数
1
解决办法
3653
查看次数

如何在不同网页的文件组中自动化Javascript组合和缩小?

假设我的页面结构是:

1. one.html :
   includes ->   a.js , b.js , c.js ,d.js

2. two.html :
   includes ->   a.js , b.js,  x.js, y.js, z.js

3. three.html :
   includes ->   a.js , b.js, s.js,  x.js, y.js
Run Code Online (Sandbox Code Playgroud)

等等.有些页面访问量比其他页面更多,比如3页提供了网站所有页面浏览量的99%.

我正在寻找一个解决方案:

i)组合并最小化可以包含在页面中的组中的文件.

ii)具有将组的文件名映射到最终组合文件名的逻辑.

iii)包括像Google Closure编译器/ YUI压缩器这样的缩小器.

我看过的一个解决方案是: PHP缩小

它完成了大部分工作.但是它对我有以下缺点:

i)我将在CDN服务器上托管我的静态组合文件,而不是托管PHP minify的同一个Web服务器,因此PHP按组名缩小服务器文件的逻辑对我来说不起作用.

ii)PHP Minify使用PHP CGI来处理和提供脚本,而我希望我的缩小内容直接从CDN服务器提供.

PHP Minify是否有一些功能可以将组名映射到组合文件名,我可以在我的网页中使用它来直接设置组合JS文件的CDN路径.例如

<?php
  $groupName = array("onePage" => array('a.js','b.js','c.js','d.js');
?>

<script type="text/javascript" src="http://www.MYSTATICSERVER.com/js/<?php getMergedFileName($groupName)"></script>
Run Code Online (Sandbox Code Playgroud)

而不是调用PHP Minify的PHP脚本来获取组的文件,这实际上是一个PHP页面调用,然后从以前生成的文件中提供javascript内容:

<script type="text/javascript" src="http://www.MYWEBSERVER.com/min/?g=onePage" ></script>
Run Code Online (Sandbox Code Playgroud)

(我同意大部分可以通过将不同的解决方案与自定义部署脚本和缩小工具相结合来实现,例如ANT,FABRIC + YUICompressor/ClosureCompiler,但我正在寻找一个我可能错过的完善的可配置解决方案)

javascript cdn minify yui-compressor google-closure-compiler

9
推荐指数
1
解决办法
2081
查看次数

用于闭包编译器的Regexp类型

我目前正在为个人javascript模块添加类型注释,但是当我尝试键入函数时,我正在考虑将regexp作为参数,但以下尝试都没有工作:

/** @type {function(RegExp)} */
/** @type {function(regex)} */
Run Code Online (Sandbox Code Playgroud)

我只是得到:

WARNING - Bad type annotation. Unknown type regexp
Run Code Online (Sandbox Code Playgroud)

我应该在声明中使用什么类型?谢谢.

javascript regex types google-closure-compiler jsdoc

9
推荐指数
1
解决办法
2903
查看次数

使用Google Closure Compiler可以从编译版本中排除一部分源代码吗?

我最近使用Dojo工具包构建了一个项目,并且喜欢如何将一段代码标记为仅基于任意条件检查包含在编译版本中.我用它来导出私有变量进行单元测试或抛出错误而不是记录它们.这是Dojo格式的一个例子,我很想知道Google Closure Compiler是否有这样的特殊指令.

window.module = (function(){

  //private variable
  var bar = {hidden:"secret"};

  //>>excludeStart("DEBUG", true);
    //export internal variables for unit testing 
    window.bar = bar;
  //>>excludeEnd("DEBUG");

  //return privileged methods
  return {
    foo: function(val){
      bar.hidden = val;
    }
  };
})();
Run Code Online (Sandbox Code Playgroud)

编辑

关闭权威指南提到您可以扩展CommandLineRunner以添加您自己的检查和优化,这可能是一种方法.Plover看起来很有前途,因为它支持自定义传递.

javascript unit-testing google-closure-compiler

8
推荐指数
2
解决办法
2031
查看次数

什么是"警告 - 可疑代码.'getprop'运算符的结果未被使用." 意思?

"警告 - 可疑代码.'getprop'运算符的结果未被使用."

当我使用闭包编译器时,我在JavaScript代码中看到这两行.它们是不报告问题的其他typedef中的typedef.我应该寻找什么?

编辑

受影响的代码:

/**
 * @typedef {{playerId: number, playerName: string, baseScores: Array.<number>, bonusScores: Array.<number>,
 *          teamScoreAdjustments: Array.<number>}}
 */
wias.GameTableTeamMember;

/**
 * @typedef {{id: number, teamMembers: Array<wias.GameTableTeamMember>, teamName: string}}
 */
wias.GameTableTeam;

/**
 * @typedef {{id: number, availableRound: boolean, bonusScoring: boolean, complete: boolean, gameLength: number,
 *          gameType: string, lastPlayed: string, numberOfRounds: number, teams: Array.<wias.GameTableTeam>, winners:
 *          Array.<string>}}
 */
wias.GameTable;
Run Code Online (Sandbox Code Playgroud)

警告:

wias.js:77: WARNING - Suspicious code. The result of the 'getprop' operator is not being used.
wias.GameTableTeam;
^
Run Code Online (Sandbox Code Playgroud)

为什么要警告而不是其他地方?

javascript google-closure-compiler

8
推荐指数
1
解决办法
4967
查看次数

使用Closure Compiler简单优化从函数内部删除调试代码

我正在寻找一种从函数中删除调试代码的方法,这样我就可以为闭包添加测试挂钩.我已经阅读了 Google Closure Compiler高级版:在编译时删除了代码块,并测试了删除调试代码的方法如下:

/** @define {boolean} */
var DEBUG = true;

if (DEBUG) {
    console.log('remove me');
}
Run Code Online (Sandbox Code Playgroud)

简单的优化--define='DEBUG=false'减少了这个var DEBUG=!1;.这同样适用于此:

/** @const */
var DEBUG = false;

if (DEBUG) {
    console.log('remove me');
}
Run Code Online (Sandbox Code Playgroud)

我遇到麻烦的地方是在函数中使用这个约定:

/** @const */
var DEBUG = false;

function logMe() {
    if (DEBUG) {
        console.log('remove me');
    }
}
Run Code Online (Sandbox Code Playgroud)

这减少到以下几点:

var DEBUG=!1;function logMe(){DEBUG&&console.log("remove me")};
Run Code Online (Sandbox Code Playgroud)

我希望它进一步减少到:

var DEBUG=!1;function logMe(){};
Run Code Online (Sandbox Code Playgroud)

有没有理由不按预期工作?我真的只是想找到一种清除方法来剥离调试代码,我还没准备好接受高级优化.

更新

Per @ John的回答,我实现了自己的编译器,并发现以下配置将从if (DEBUG) {}代码的内部和外部删除@define:

CompilerOptions options = new …
Run Code Online (Sandbox Code Playgroud)

javascript minify google-closure-compiler

8
推荐指数
1
解决办法
4484
查看次数

连接到ClojureScript bREPL:clojure.browser.repl/connect在编译的JavaScript中抛出TypeError

我正在尝试连接到ClojureScript浏览器REPL,我遇到了麻烦clojure.browser.repl/connect.我编译的JavaScript会TypeError尝试调用顶部的Google Closure代码块中appendChildnull对象.我按照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.jsindex.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

8
推荐指数
1
解决办法
1684
查看次数

在javascript中重命名文档变量是否安全

我注意到,谷歌关闭编译没有重命名document为类似d减少的空间.

我想不出这会破坏代码的情况(即document指向其他东西的地方).实际上也是如此window.

是否有理由保护document这种方式?

==编辑==

通过重命名它我正在考虑重新分配它.以下示例.

var d=document;
var obj1=d.getElementById("obj1");
var obj2=d.getElementById("obj2");
... // with enough uses of document so it makes to reassign it size-wise.
Run Code Online (Sandbox Code Playgroud)

javascript google-closure-compiler

8
推荐指数
1
解决办法
382
查看次数