Javascript minification with semicolon insertion

这一生的挚爱 提交于 2021-01-27 07:25:01

问题


Is there any Javascript minifier/compressor which will insert semicolons as necessary (or, one which works with source code which doesn't use semicolons)? I've started using a library which is really great, but the developer considers the semicolons as "unnecessary clutter"...


回答1:


When I was in a similar situation a couple of years ago, I seem to recall that the solution was essentially "the more the merrier"!

Firstly I ran the script through Dojo Shrinksafe on very modest compression settings. Because it uses Rhino, it would actually interpret the JS and then write out an equivalent version with syntactically correct semicolons.

From there, I fed the output into Dean Edward's /packer/, which (at the time, at least) was the highest-compression library that produced consistently functional output. This library requires the input to have semicolons, but that wasn't a problem thanks to the output from ShrinkSafe.

(Note that this is from memory and I haven't tested it recently, but there's no reason why the principle shouldn't hold any more. If ShrinkSafe doesn't insert semicolons for you, perhaps there's some other JS-preprocessing tool that will automatically do this? Modulo, of course, the fact that semicolon insertion is not 100% deterministic).



来源:https://stackoverflow.com/questions/4276176/javascript-minification-with-semicolon-insertion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!