UglifyJS uses strict expressions

I am using Grunt related to the grunt-contrib-uglify to minimize JavaScript in my application.

After minimizing it, it removes every 'use strict' statement except the very first, so I get a huge JavaScript file with the 'use strict' directive on top.

The problem is that the global 'use strict' directive causes the browser to execute the code of each lib that I use in the project in "strict mode" and causes errors, since not every third-party code is written for strict mode.

Any ideas on how to solve this?

+8
javascript gruntjs bundling-and-minification uglifyjs
source share
1 answer

If you complete all your scripts with IIFE , then grunt-contrib-uglify will not position this statement at the halt, and rather, it will leave it inside every IIFE section you write.

 (function() { 'use strict'; // do stuff })(); 

Yes, this is more code, but if your gzipping file, it should be no problem. It will also contain any variables that you define outside the global scope, which will lead to more efficient code.

+6
source share

All Articles