标签: google-closure-compiler

如何将Google Closure应用到我的整个网页?

谷歌有一个非常好的JavaScript压缩器叫做"Closure"

http://closure-compiler.appspot.com/home

但是在HTML文件中使用内联JavaScript会很痛苦.

问题:是否存在一个在线工具,我只是输入"uncompress.html",并且它还会压缩所有内联JavaScript压缩的HTML版本?

javascript compression google-closure-compiler

5
推荐指数
1
解决办法
394
查看次数

dojo和谷歌闭包编译器

我注意到dojo 1.4.0在简单模式下支持google编译器,它最终是否会支持高级模式?

dojo google-closure-compiler

5
推荐指数
2
解决办法
1833
查看次数

当使用具有高级优化的Closure编译器时,如何在javascript中保留全局变量?

我有自己的Javascript库,我想通过使用带有高级优化的Google闭包编译器来缩小它.通过查看文档,我看到如何声明在库外部使用的函数.

但是我找不到如何保存我的库中声明的全局变量的方法.Closure编译器只是删除它们,因为它认为它们从未被使用过.有人可以帮忙吗?

编辑:示例代码:

var variable_1 = true;
Run Code Online (Sandbox Code Playgroud)

这是在我的库的开头全局定义的,但它从未在库本身中使用过.当它包含在某个页面中时,它在库外使用.但Closure编译器不知道,这就是它删除这些声明的原因.

javascript obfuscation minify google-closure-compiler

5
推荐指数
1
解决办法
4761
查看次数

有关从JavaScript中的闭包返回对象文字的详细信息

背景:我想重写一个库(我没有写过),以避免Closure Compiler使用Advanced选项生成警告。对于这个问题,JavaScript“ this”关键字和Closure Compiler警告,答案是使用闭包重写代码。目的是避免使用关键字this(生成编译器警告)。

由于该库具有许多函数,所以我最好使新闭包返回一个对象文字。我想了解这是如何工作的以及可能产生的后果。因此,我编写了以下(无意义的)示例作为学习技巧(也在此处:jsFiddle):

  var CurrencyObject = function(Amount) {
        var money = Amount;
        return {
              "toCents": function() {
                    money *= 100;
                    return money;
              },
              "toDollars": function() {
                    money /= 100;
                    return money;
              },
              "currentValue": money  // currentValue is always value of Amount
        };
  }; // end currencyObject

  var c1 = CurrencyObject(1.99); // what's the difference if the syntax includes `new`?

  alert('cents = ' + c1.toCents() + '\ncurrent money = ' + c1.currentValue …
Run Code Online (Sandbox Code Playgroud)

javascript closures google-closure-compiler

5
推荐指数
1
解决办法
1957
查看次数

Hide Google Library's warnings

I'm using Google Closure and Google Library with my projects and I'm meeting troubles with warnings.

My project is made of ~50 files with somme small warnings (JSDoc mistakes). The problem is Google Library, when I'm compiling my project, a huge list of warnings come from the google's library, like this :

../Libs/Closure/closure/goog/uri/utils.js:255: WARNING - inconsistent return type
found   : (null|string|undefined)
required: (null|string)
  return uri && decodeURIComponent(uri);
         ^

../Libs/Closure/closure/goog/uri/utils.js:634: WARNING - inconsistent return type
found   : (Array.<(string|undefined)>|undefined)
required: Array.<(string|undefined)>
  return …
Run Code Online (Sandbox Code Playgroud)

google-closure-library google-closure-compiler

5
推荐指数
1
解决办法
1081
查看次数

如何在缩小之前管理AngularJS WebApp的E2E测试?

我们使用AngularJS Seed作为项目模板创建WebApp.有许多控制器,指令,服务,每个都在它自己的js文件中.

  • 应用程序文件夹的内容将使用Closure缩小.
  • 所有lib依赖项将通过CDN替换为缩小版本.
  • 使用Testacular/Jasmine进行单元/ e2e测试.

我不确定典型的dev-compile-test过程是什么,因为我是AngularJS和Javascript WebApps的新手一般:-)我的问题是如何在开发期间管理app js文件,以便E2E测试是可调试的.这是我目前的想法;

  1. Testacular监视源文件以进行连续单元测试.
  2. index.html页面始终引用连接但非minfied的js文件.
  3. 必须在应用程序运行/ e2e测试之前进行编译.
  4. 构建服务器将缩小作为其活动的一部分.

这是一种合理的方法吗?它有助于澄清我正在使用WebStorm,nodejs等.

web-applications google-closure-compiler webstorm angularjs karma-runner

5
推荐指数
1
解决办法
459
查看次数

组织和合并JS文件,Google Closure?

我正在尝试合并我的所有插件,所以我可以改变这个:

<html>
    <head>
    </head>
    <body>    
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script src="js/plugin1.js"></script>
        <script src="js/plugin2.js"></script>
        <script src="js/plugin3.js"></script>
        <script src="js/plugin4.js"></script>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

对此:

<html>
    <head>
    </head>
    <body>    
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script src="js/plugins.min.js"></script>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

主要的想法是有一个工具"自动导入"我的plugins.js文件所有我的网站所需的插件,并在编译时缩小它们.我一直在测试Google Closure(可下载的Java版本的编译器),我想到了如何缩小文件,但我无法导入外部文件.所以我想问你是否可以告诉我Google Closure是否是我正在寻找的工具,或者我是否应该使用其他工具或方法.

"自动导入"我的意思是:

PLUGINS/HELLO.JS

function hello(name) {
    alert('Hello, ' + name);
}
Run Code Online (Sandbox Code Playgroud)

PLUGINS.JS

@import hello.js
hello('New user');
Run Code Online (Sandbox Code Playgroud)

我希望自己清楚明白,如果我搞砸了英语,我会道歉.

谢谢!!

javascript google-closure-compiler

5
推荐指数
2
解决办法
3723
查看次数

使用闭包编译器进行类型检查不是隐式的?

我正在编译文件并获得编译代码,但注释似乎完全被忽略了; 没有警告没有错误.使用calcdeps.py使用以下命令编译我的代码:

set calc="D:\software\closure compiler\library\closure\bin\calcdeps.py"
c:\Python27\python.exe %calc% ^
--path D:\flex_sdk_4.6\projects\EnglishConverter\bin\js\ ^
--input D:\flex_sdk_4.6\projects\EnglishConverter\bin\js\mmt\Mediator.js ^
--input D:\flex_sdk_4.6\projects\EnglishConverter\bin\js\mmt\DomDependent.js ^
--input D:\flex_sdk_4.6\projects\EnglishConverter\bin\js\mmt\WorkFlow.js ^
--input D:\flex_sdk_4.6\projects\EnglishConverter\bin\js\mmt\Messenger.js ^
--input D:\flex_sdk_4.6\projects\EnglishConverter\bin\js\mmt\data.js ^
--compiler_jar "D:\software\closure compiler\compiler.jar" ^
--output_mode compiled ^
--compiler_flags="--compilation_level=ADVANCED_OPTIMIZATIONS" ^
--compiler_flags="--formatting=PRETTY_PRINT" ^
--output_file D:\flex_sdk_4.6\projects\EnglishConverter\bin\js\main.js
pause
Run Code Online (Sandbox Code Playgroud)

例如在Messenger.js中我有一个函数:

/**
 * Replaces words in matches with a yes/no/all box
 * @param {Array} matches contains the items of myApp.data that matched words in text
 * @param {string} text contains the cleaned up user input (no html)
 */
myApp.Messenger.builtReplacewithOptions=function(matches,text){
Run Code Online (Sandbox Code Playgroud)

变量matches必须是一个Array,所有调用此函数的代码都使用Array调用它.要测试类型检查,我将Array更改为字符串,如下所示: …

javascript strong-typing google-closure-compiler

5
推荐指数
1
解决办法
1254
查看次数

预编译的闭包模板 - "声明前引用的变量"闭包编译器中的警告

java -jar SoyToJsSrcCompiler.jar --shouldGenerateJsdoc --outputPathFormat 
           simple.js --srcs simple.soy
Run Code Online (Sandbox Code Playgroud)

SoyToJsSrcCompiler生成一个如下所示的js文件:

if (typeof templates == 'undefined') { var templates = {}; }
if (typeof templates.simple == 'undefined') { templates.simple = {}; }

/**
 * @param {Object.<string, *>=} opt_data
 * @param {(null|undefined)=} opt_ignored
 * @return {string}
 * @notypecheck
 */

 templates.simple.tinyButton = function(opt_data, opt_ignored) {
     .....
 };
Run Code Online (Sandbox Code Playgroud)

我正在使用Closure Compiler with --warning_level=VERBOSE--compilation_level ADVANCED_OPTIMIZATIONS

我收到这个警告:

simple.js:1: WARNING - Variable referenced before declaration: templates
if (typeof templates == 'undefined') { var templates = {}; } …
Run Code Online (Sandbox Code Playgroud)

javascript google-closure-compiler google-closure-templates

5
推荐指数
1
解决办法
565
查看次数

可以为Closure Compiler输出TypeScript输出注释吗?

我正在使用TypeScript,我想使用Closure-Compiler来缩小和混淆我在构建TS代码后得到的JS输出.

我读到GCC能够根据类型定义进行混淆.据我所知(如果我错了请纠正我),这意味着如果我的代码上有类型注释,那么GCC将使用它们来进行更好的混淆.

例如,对于obj.somePropGCC,当前someProp在我的代码中查找属性名称的所有实例,而不考虑它所在的对象,并将所有这些实例替换为相同的混淆名称(例如o.a).
但是如果我在我的代码上有类型注释,GCC将能够知道哪个对象属于哪种类型并相应地对其进行模糊处理 - 因此两个不同类型的相同属性名称将被混淆为两个不同的名称.

问题:

  • 我理解正确吗?
  • "类型安全"混淆是一件好事吗?
    我的意思是,它有什么好处?它似乎不会对生成的文件大小产生影响,甚至可能对gzip压缩文件大小产生负面影响(因为不同类型的密钥可能有更多不同).
  • 我可以使用TypeScript以某种方式自动创建GCC注释吗?
    由于TS已经是类型安全的,我相信它是可能的,但我怀疑它是一个功能.它可能需要TS编译器内部的知识.有什么见解吗?

编辑

我其实刚发现这个这个.我会检查出来并尽快发布更新.

javascript obfuscation google-closure-compiler typescript

5
推荐指数
1
解决办法
3121
查看次数