最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - UglifyJS 'use strict' statements - Stack Overflow

programmeradmin4浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 8

If 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.

发布评论

评论列表(0)

  1. 暂无评论