Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any risk in minifying JS and CSS?

I have a complex web app on an embedded system and soon it will be going through testing. Currently all files are in their source form for debugging. I will absolutely need to minify all JS and CSS and I am wondering if there is any chance that something could go wrong. Is it better to test the code minified or in a source state?

Thanks.

like image 306
BarryBones41 Avatar asked Mar 16 '23 05:03

BarryBones41


1 Answers

If a cosmic ray were to penetrate your build server's case and hit the memory while the minification was happening, it could ruin everything. The chances of that are very small, though.

Another step in your build will always introduce complexity and have a potential for errors, especially when it modifies a file. Your testing will discover that, since you should always test what you plan to deploy to production. If you deploy the minified code, you test the minified code.

Minification is so widely used that most of the tools are pretty mature and robust, with excellent accuracy at detecting what is in-use and what is not. They are very good at not removing code that is used in an unusual way.

However, it is possible for the minifier to decide something isn't being used and be incorrect. In that case, your unit and/or integration testing should fail and you'll be able to change the code or tell the minifier to keep the section that was missing.

Those fixes are pretty quick and usually obvious, so the advantages of having a few small files (instead of many larger files) will almost always outweigh them. Since minifiers rename code significantly or remove it entirely, even simple smoke testing of your app is often enough to expose any errors.

As an example, Google's closure compiler has a tutorial on handling global and external variables, exports, and unused code you need to keep.

like image 58
ssube Avatar answered Mar 29 '23 08:03

ssube