简体   繁体   English

闭包编译器和奇怪的预代码

[英]Closure-compiler and strange pre code

I often use online closure compiler ( https://closure-compiler.appspot.com/home ) to minify my JS code. 我经常使用在线闭包编译器( https://closure-compiler.appspot.com/home )来缩小我的JS代码。 Now I'm trying to integrate this process into PhpStorm via "External tools" and I'm having very strange result. 现在,我正在尝试通过“外部工具”将此过程集成到PhpStorm中,结果非常奇怪。 Everything works fine, except one thing - some strange code is printed before mine if I use Set variables. 一切正常,除了一件事-如果我使用Set变量,在我之前会打印一些奇怪的代码。 For example: 例如:
Original code : 原始代码

function a(text) { alert(text); }
$(document).ready(function(){
    let mySet = new Set();
    $("#myButton").click(function(){
       a("CLICKED");
       mySet.clear();
       for(let i=0;i<10;i++) mySet.add(i);
    });
}); 

Minified via WEB-tool : 通过WEB工具缩小

function a(b){alert(b)}$(document).ready(function(){var b=new Set;$("#myButton").click(function(){a("CLICKED");b.clear();for(var c=0;10>c;c++)b.add(c)})});  

Minified via command line ( java -jar compiler.jar --charset UTF-8 --js closureTest.js --js_output_file closureTest.min.js ): https://pastebin.com/QqGXc6H7 通过命令行缩小java -jar Compiler.jar --charset UTF-8 --jsclosureTest.js --js_output_fileclosureTest.min.js ): https : //pastebin.com/QqGXc6H7


Without Set variable: 没有设置变量:

Original : 原件

function a(text) { alert(text); }
$(document).ready(function(){
    let mySet = [];
    $("#myButton").click(function(){
       a("CLICKED");
       mySet.length = 0;
       for(let i=0;i<10;i++) mySet.push(i);
    });
});

Minified via WEB-tool : 通过WEB工具缩小

function a(b){alert(b)}$(document).ready(function(){var b=[];$("#myButton").click(function(){a("CLICKED");for(var c=b.length=0;10>c;c++)b.push(c)})});

Minified via command line : 通过命令行缩小

function a(b){alert(b)}$(document).ready(function(){var b=[];$("#myButton").click(function(){a("CLICKED");for(var c=b.length=0;10>c;c++)b.push(c)})});

As you can see, WEB-tool compilation works fine, but what's wrong with command line compiling? 如您所见,WEB工具编译工作正常,但是命令行编译又出了什么问题?

Well, I found solution (thanks shiftweave ). 好吧,我找到了解决方案(感谢shiftweave )。
It needed argument --rewrite_polyfills false to work fine. 它需要参数--rewrite_polyfills false才能正常工作。

The compiler by default "transpiles" code from the latest supported language version to EcmaScript 5. To support this it also include the necessary polyfills. 默认情况下,编译器将代码从最新支持的语言版本“转换”到EcmaScript5。为了支持此功能,它还包含必要的polyfill。 If you don't need the polyfills, --rewrite_polyfills false as stated in another answer is one solution but a better one may be to set the --language_out=ECMASCRIPT_2015 or --language_out=ECMASCRIPT_2017 if you are only targetting browsers that support these later language features. 如果您不需要--rewrite_polyfills false ,则在另一个答案中指出--rewrite_polyfills false是一个解决方案,但是更好的方法是将--language_out=ECMASCRIPT_2015--language_out=ECMASCRIPT_2017为仅针对支持这些功能的浏览器后来的语言功能。

You should set your intended --language_out regardless as the default may change in the future. 无论将来的默认设置如何更改,都应设置所需的--language_out

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM