I'm using Grunt hooked up with grunt-contrib-uglify
task to minify JavaScript in my app.
Upon minification, it's removing every 'use strict'
statement except the very first one, so I'm getting a huge JavaScript file with 'use strict' directive at the top.
The issue is that the global 'use strict'
directive makes the browser execute the code of every lib I'm using in the project in the "strict mode" and it's causing errors, since not every 3rd party code is written for strict mode.
Any ideas on how to solve this?
I'm using Grunt hooked up with grunt-contrib-uglify
task to minify JavaScript in my app.
Upon minification, it's removing every 'use strict'
statement except the very first one, so I'm getting a huge JavaScript file with 'use strict' directive at the top.
The issue is that the global 'use strict'
directive makes the browser execute the code of every lib I'm using in the project in the "strict mode" and it's causing errors, since not every 3rd party code is written for strict mode.
Any ideas on how to solve this?
Share Improve this question edited Dec 16, 2016 at 21:16 danwellman 9,2738 gold badges63 silver badges91 bronze badges asked Dec 19, 2013 at 14:50 Roman KolpakRoman Kolpak 2,0221 gold badge22 silver badges26 bronze badges 2- It's not easy to solve, as documented in this ment on the UglifyJS2 project page. – Matthew Bakaitis Commented Dec 19, 2013 at 15:48
- @rawry Can you try using next version github./mishoo/UglifyJS2 . Looks like the problem is solved in this new version. – Vishwanath Commented Jan 30, 2015 at 9:37
1 Answer
Reset to default 8If you wrap all your scripts with an IIFE then the grunt-contrib-uglify
won't position that statement to the stop and rather it'll leave it inside every IIFE you write.
(function() {
'use strict';
// do stuff
})();
Yes, it's more code, but if your gzipping the file it should be a non-issue. Also this will keep any variables you define outside of the global scope, leading to more performant code.