11

I've beeing using YUI Compressor to minify JS files. I have this code:

(function(global) {
    "use strict";

     var X=1;

    /*** my code here ***/

}(window));

And I compress it with this command:

$> java -jar yuicompressor-2.4.7.jar test.js -o test.min.js --verbose

The "use strict" hint is not present in the compressed output file. It seems that YUI removes all hints. And it output this warning:

[WARNING] Invalid hint syntax: use strict
(function(global){ ---> "use strict" <--- ;var X=1;... 

Although, my code works fine after compression, I'd like to know if there is an argument to YUICompressor to preserve the hints and/or a way to avoid that warning.

Any advice? Thanks.

Guumaster
  • 389
  • 3
  • 10
  • You mean aside from just writing strict code without having the engine correct you? – Niet the Dark Absol Apr 23 '12 at 11:14
  • wow, it really looks like YUI strips away those text hints and therefore, auto-removes any strict mode. – jAndy Apr 23 '12 at 11:24
  • Go to yuilibrary.com and files a ticket for the compressor. – Mörre Apr 23 '12 at 11:31
  • @Kolink, I know that once i can write full strict code I won't need the "use strict" hint. In the meantime, is useful to me to know what I'm doing that is not strict and to tell validators (jslint/jshint) that my code is supposed to be strict. – Guumaster Apr 23 '12 at 14:42
  • @Mörre this doesn't seem to be an error. Just want to understand what's happening there. – Guumaster Apr 23 '12 at 14:44
  • 1
    @Guumaster: You can file tickets for ENHANCEMENTS too, not just for "errors". So my suggestion remains :-) – Mörre Apr 24 '12 at 10:47
  • 2
    @Kolink - As I understand it, the advantage of "use strict" is not just that it forces one to code better, but that it allows certain browsers to optimize code processing. So https://developer.mozilla.org/en/JavaScript/Strict_mode claims "strict mode code can sometimes be made to run faster than identical code that's not strict mode". – Nick Jul 01 '12 at 07:40

1 Answers1

3

A partial explanation can be found here. Although that question is about closure compiler the answer gives you a hint (as Google also had this issue). In Closure it is possible to use --language_in=ECMASCRIPT5_STRICT

Unfortunately up to now there has been no such thing for the YUI Compressor. At least I could not find anything like that.

Community
  • 1
  • 1
Tobias Krogh
  • 3,768
  • 20
  • 14
  • Note that JSLint prefers the function form of 'use strict' but Closure removes the function form and adds it at the begining of the file. So still not a correct solution. – Nick.T Feb 27 '14 at 16:39