问题
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