标签: google-closure-compiler

是否与Closure Compiler兼容jQuery 1.4.2?

根据官方发布声明 1.4版本已重新写来,当我使用的在线版本与使用Closure Compiler压缩尚未关闭的编译器,我得到130个警告.

这是我使用的代码.

// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @output_file_name default.js
// @code_url http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js
// ==/ClosureCompiler==
Run Code Online (Sandbox Code Playgroud)

据我所知,如果您将库包含在您的代码中,那么您将获得Closure Compiler的真正好处,因此它会删除未使用的函数.然而,我的测试表明,除了压缩图书馆本身之外我无法进一步发展.

我究竟做错了什么?任何形式的见解将不胜感激.

compression jquery compatibility google-closure-compiler

3
推荐指数
1
解决办法
1281
查看次数

Google Closure 反编译器?

我正在寻找一种反编译由 Google Closure 编译的 JavaScript 的方法。我确实找到了一个 Decompiler 类(https://code.google.com/p/closure-compiler/source/browse/lib/rhino/src/org/mozilla/javascript/Decompiler.java?name=v20140407),但是我运气不太好。

有人尝试过这个或者知道其他方法吗?

decompiling decompiler google-closure google-closure-compiler

3
推荐指数
1
解决办法
5719
查看次数

Javascript Closure编译器 - 导出全局变量

我的webapp基于一个通用脚本,我在其中定义了常用函数和全局变量以及处理这些函数的动态加载脚本.到目前为止,我发现导出全局变量的唯一方法是替换任何出现window["myGlobalVar"]但我发现它非常难看.有更好的方法吗?

这是一个例子

// commonscript.js before compilation
function incrementVariable() {window["myGlobalVar"]++;}
window["incrementVariable"] = incrementVariable;
window["myGlobalVar"] = 0;
Run Code Online (Sandbox Code Playgroud)

在另一个脚本中

alert(myGlobalVar); // <= alerts 0
incrementVariable();
alert(myGlobalVar); // <= alerts 1
Run Code Online (Sandbox Code Playgroud)

我正在寻找一种直接myGlobalVar在两个文件中使用的方法,因为它会更优雅.但是,我需要设置window["myGlobalVar"]指针而不是对象的副本,我不知道如何在简单类型上执行此操作.

可能吗?是否myGlobalVarObject另一种方式封装?

非常感谢您的灯光.

javascript google-closure-compiler

3
推荐指数
1
解决办法
1867
查看次数

如何防止Google Closure Compiler删除一行

我有一个函数,我在其中创建一个测试元素.在返回函数之前,我将对test元素的引用无效,以帮助防止内存泄漏.但是闭包编译器正在删除它认为不需要的那条线b/c(在两种模式下).是否有某种评论我可以添加以防止删除该行?

function isExample (testElem) {
 var bool; 
 testElem = testElem || document.createElement('div');

 // Do stuff in here to determine `bool`
 // ...

 // Then nullify the reference
 testElem = null; // The compiler removes this line. How do I make it keep it?

 return bool;
}
Run Code Online (Sandbox Code Playgroud)

javascript google-closure google-closure-compiler

3
推荐指数
1
解决办法
999
查看次数

Closure Compiler - 在命令行中解析Javascript而不从磁盘读取它?

我在脚本中调用Closure Compiler(closurecompiler.jar).该脚本还生成一些Closure Compiler需要编译的javascript.有没有办法解析这个javascript到Closure Compiler而不将其写入磁盘并阅读它--js.

google-closure-compiler

3
推荐指数
1
解决办法
501
查看次数

闭包编译器选项

我想使用 Closure Compiler 来缩小/压缩 JS 代码。

问题是它并没有像我期望的那样缩小。考虑下面的代码。当我传递字符串时

var func = function ( someArgument ) {
  alert ( someArgument ); 
  return someArgument; 
}
Run Code Online (Sandbox Code Playgroud)

我希望缩小后的代码将“someArgument”重命名为更短的名称,例如“a”。

是这样还是我做错了什么?TIA

public static void Compress( String src ) {

    ByteArrayOutputStream err = new ByteArrayOutputStream();

    CompilerOptions opt = new CompilerOptions();

    CompilationLevel.ADVANCED_OPTIMIZATIONS.setDebugOptionsForCompilationLevel( opt );

    Compiler.setLoggingLevel( Level.OFF );
    Compiler compiler = new Compiler( new PrintStream( err ) );
    compiler.disableThreads();

    List<SourceFile> externs = Collections.emptyList();
    List<SourceFile> inputs = Arrays.asList( SourceFile.fromCode( "javascript-code.js", src) );

    Result result = compiler.compile( externs, inputs, opt );

    System.out.println( …
Run Code Online (Sandbox Code Playgroud)

java compression minify google-closure google-closure-compiler

3
推荐指数
1
解决办法
2899
查看次数

使用"类"容器对象时,Closure Compiler @extends不起作用

我正在为闭包编译器注释我的所有javascript,但是 - 我目前的代码在很大程度上取决于在对象中定义类,即:

Class.SomeClass = function() {};
Class.SomeOtherClass = function() {};
Run Code Online (Sandbox Code Playgroud)

而不是:

function SomeClass() {};
SomeClass.prototype = {};
Run Code Online (Sandbox Code Playgroud)

但是,它在尝试注释扩展时给了我一个警告......编译器声明我无法确定Class.SomeClass是什么类型:

JSC_TYPE_PARSE_ERROR: Bad type annotation. Unknown type Class.SomeObject 
*  @extends Class.SomeObject
Run Code Online (Sandbox Code Playgroud)

使用ADVANCED_OPTIMIZATIONS将以下代码粘贴到闭包编译器中:

// ==ClosureCompiler==
// @output_file_name default.js
// @compilation_level ADVANCED_OPTIMIZATIONS
// ==/ClosureCompiler==

(function($) {

    "use strict";

    var Class = {};

    /**
     *  @constructor
     */
    Class.ObjectA = function() {};
    Class.ObjectA.prototype = {};

    /**
     *  @constructor
     *  @extends Class.ObjectA
     */
    Class.ObjectB = function() {};
    Class.ObjectB.prototype = $.extend(new Class.ObjectA(), {
        initialize: function() …
Run Code Online (Sandbox Code Playgroud)

javascript google-closure-compiler

3
推荐指数
1
解决办法
802
查看次数

为Google Closure Compiler注释"任何类型的零个或多个参数"

我有一个与之相同的函数jQuery.noop,angular.noop并且goog.nullFunction:它什么都不做,返回undefined,但它的形式很方便callback(successFn || noop);.

可以使用任意类型的任意数量的参数(0或更多)调用它.

这就是我目前的方式:

/**
 * @param varArgs {...*}
 */
var noop = function(varArgs) {};
Run Code Online (Sandbox Code Playgroud)

问题:当没有参数调用时,Google Closure Compiler给了我:

Function noop: called with 0 argument(s). 
Function requires at least 1 argument(s) and no more than 1 argument(s).
Run Code Online (Sandbox Code Playgroud)

有趣的是goog.nullFunction是为Closure Compiler注释的,但是它的注释也存在缺陷,当它被一个或多个参数调用时会抛出错误:

Function noop: called with 1 argument(s).
Function requires at least 0 argument(s) and no more than 0 argument(s).
Run Code Online (Sandbox Code Playgroud)

问题:如何noop正确注释我的功能?

javascript google-closure-compiler

3
推荐指数
1
解决办法
846
查看次数

如何在闭包编译器中从重命名中排除对象

我使用closure-util 并希望有一个不重命名的对象来更改 html 中的内容。
在我的第一步中,我想将我的模块与 openlayers 3|4 一起编译。
我不熟悉 externs、export 或 api,所以我需要帮助声明闭包编译器。

片段 .html(未编译)

  olc.lon=7.11875;olc.lat=51.15345;olc.zoom=12;
Run Code Online (Sandbox Code Playgroud)

main.js 中的代码段声明将被编译

var olc = {      // namespace controls and constants
  lon         :  2.0,lat: 25.0,rota: 0,zoom: 2, // as default
  debug       :  'force'
};
window['olc'] = olc;
Run Code Online (Sandbox Code Playgroud)

现在编译后
-olc.lon重命名为olc.B,
-olc.lat重命名为olc.uj,
-olc.rota重命名为olc.mf,
-olc.zoom没有重命名,不知道为什么不是和
- olc.debug 没有重命名。

有像zoom这样的受保护词吗?
例如,我如何保护 olc.lon 免于重命名?

javascript google-closure-compiler openlayers-3

3
推荐指数
1
解决办法
1029
查看次数

由于从未发生过的空条件,闭包编译器认为参数不匹配

我正在使用google 闭包编译器来缩小我的代码,我最近添加了JSDoc 注释以允许编译器进行类型检查和更好的缩小(在高级模式下)。不幸的是,它正在显示警告。有没有一种方法可以更改代码以防止警告,而无需添加闭包编译器注释来抑制它?

JSC_TYPE_MISMATCH: actual parameter 1 of add does not match formal parameter
found   : (Date|null)
required: Date at line 30 character 8
    add(date, 1, "second");
        ^
Run Code Online (Sandbox Code Playgroud)

这是我试图缩小的 JS 代码

JSC_TYPE_MISMATCH: actual parameter 1 of add does not match formal parameter
found   : (Date|null)
required: Date at line 30 character 8
    add(date, 1, "second");
        ^
Run Code Online (Sandbox Code Playgroud)

date instanceof Date检查应处理日期福祉null,使该方法返回FALSE,甚至没有尝试将所有的add功能。不知道为什么 cc 认为我可以传递nulladd函数。在实际代码中,isValidDateObject用在很多地方,所以我不想内联它。

您可以通过使用闭包编译器的在线版本并将优化级别设置为“高级”来缩小我的代码。 …

javascript null types minify google-closure-compiler

3
推荐指数
1
解决办法
511
查看次数