Really late to the party here, but figured this might help someone still looking for a different answer:
After trying to use YUI Compressor, I was disappointed that it was incompatible with more recent versions of jQuery and Prism (the two main 3rd party JS libraries I needed for my project which I wanted compressed into a single file). So I decided to use Terser, which is a fork of Uglify-JS that supports ES6+. I wasn't able to get it to run directly using the <exec>
task, but using the Windows command line method works for Win 10, at least (not saying it can't work otherwise, but this was a very easy work-around). No need to add anything else to the Path system variable (as Node.JS is typically added during installation). I first use the ANT <concat>
task to make a big, uncompressed file. Use <fileset>
as it will preserve the order (if that's important, anyway).
<concat destfile="${js-big-file}" encoding="UTF-8" outputencoding="UTF-8" fixlastline="true">
<filelist refid="js-input-filelist"/>
</concat>
Then use the <exec>
task to run any NPM program, such as Terser. The Apache manual page on this task indicated this is the Windows workaround for running .bat files, but it really allows you run just about any command line application (even those that <exec>
mysteriously cannot find otherwise).
<exec executable="cmd">
<arg value="/c"/>
<arg value="terser"/>
<arg value="${js-big-file}" />
<arg value="-o" />
<arg value="${smaller-js-file}"/>
</exec>
Integrate? It is part of an ANT build script (a DITA Open Toolkit plugin to support custom JavaScript, among other things - not a Java Web application, per se, but using Java to build HTML5 output), so integration was not much more than adding those tasks to a new target (there's more code regarding setting defaults and checking input parameters!).
Easy to Enable/Disable? In my case, I have a parameter I pass to the ANT Build in order to include building and minifying the JS file. So yes, it only performs this target if I set the param to 'Yes'. That's a pretty easy thing to set up in an ANT build.
Transparent So far, it appears to have no effect on any of the several JS files I'm including. Some of those are my own (and I'm no JS expert, by any means) and some are, as I mentioned, common JS libraries.
Minifier Terser, but you could use just about any minified with command line input with this method.
Lack features? Terser only works with JavaScript. If I want to do the same for my CSS files (which I do), I use YUI Compressor.
Like That it is a currently active project and has good support. Plus, the current implementation (only calling it via the ANT <exec>
target) allows me to swap out minifiers should I need to use something else down the road.
Don't like That it requires Node.JS. Nothing against Node.JS, mind you, just that this particular project doesn't need it otherwise. I'd much prefer to use a Java .jar file like YUI Compressor for this (I can easily distribute that with a plugin should I need to).