谷歌有一个非常好的JavaScript压缩器叫做"Closure"
http://closure-compiler.appspot.com/home
但是在HTML文件中使用内联JavaScript会很痛苦.
问题:是否存在一个在线工具,我只是输入"uncompress.html",并且它还会压缩所有内联JavaScript压缩的HTML版本?
我注意到dojo 1.4.0在简单模式下支持google编译器,它最终是否会支持高级模式?
我有自己的Javascript库,我想通过使用带有高级优化的Google闭包编译器来缩小它.通过查看文档,我看到如何声明在库外部使用的函数.
但是我找不到如何保存我的库中声明的全局变量的方法.Closure编译器只是删除它们,因为它认为它们从未被使用过.有人可以帮忙吗?
编辑:示例代码:
var variable_1 = true;
Run Code Online (Sandbox Code Playgroud)
这是在我的库的开头全局定义的,但它从未在库本身中使用过.当它包含在某个页面中时,它在库外使用.但Closure编译器不知道,这就是它删除这些声明的原因.
背景:我想重写一个库(我没有写过),以避免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) 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) 我们使用AngularJS Seed作为项目模板创建WebApp.有许多控制器,指令,服务,每个都在它自己的js文件中.
我不确定典型的dev-compile-test过程是什么,因为我是AngularJS和Javascript WebApps的新手一般:-)我的问题是如何在开发期间管理app js文件,以便E2E测试是可调试的.这是我目前的想法;

这是一种合理的方法吗?它有助于澄清我正在使用WebStorm,nodejs等.
web-applications google-closure-compiler webstorm angularjs karma-runner
我正在尝试合并我的所有插件,所以我可以改变这个:
<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)
我希望自己清楚明白,如果我搞砸了英语,我会道歉.
谢谢!!
我正在编译文件并获得编译代码,但注释似乎完全被忽略了; 没有警告没有错误.使用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更改为字符串,如下所示: …
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) 我正在使用TypeScript,我想使用Closure-Compiler来缩小和混淆我在构建TS代码后得到的JS输出.
我读到GCC能够根据类型定义进行混淆.据我所知(如果我错了请纠正我),这意味着如果我的代码上有类型注释,那么GCC将使用它们来进行更好的混淆.
例如,对于obj.somePropGCC,当前someProp在我的代码中查找属性名称的所有实例,而不考虑它所在的对象,并将所有这些实例替换为相同的混淆名称(例如o.a).
但是如果我在我的代码上有类型注释,GCC将能够知道哪个对象属于哪种类型并相应地对其进行模糊处理 - 因此两个不同类型的相同属性名称将被混淆为两个不同的名称.
问题:
编辑
javascript ×7
obfuscation ×2
angularjs ×1
closures ×1
compression ×1
dojo ×1
karma-runner ×1
minify ×1
typescript ×1
webstorm ×1