Артем Носов
Артем Носов

Reputation: 226

Closure-compiler and strange pre code

I often use online closure compiler (https://closure-compiler.appspot.com/home) to minify my JS code. Now I'm trying to integrate this process into PhpStorm via "External tools" and I'm having very strange result. Everything works fine, except one thing - some strange code is printed before mine if I use Set variables. 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:

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


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:

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?

Upvotes: 1

Views: 275

Answers (2)

John
John

Reputation: 5468

The compiler by default "transpiles" code from the latest supported language version to EcmaScript 5. To support this it also include the necessary polyfills. 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.

You should set your intended --language_out regardless as the default may change in the future.

Upvotes: 0

Артем Носов
Артем Носов

Reputation: 226

Well, I found solution (thanks shiftweave).
It needed argument --rewrite_polyfills false to work fine.

Upvotes: 2

Related Questions