Minifying JavaScript with npx uglify-js

While upgrading CodeMirror in Datasette I figured out how to minify JavaScript using uglify-js on the command line without first installing any teels, using npx (which downloads and executes a CLI tool while skipping the install step):

npx uglify-js codemirror-5.57.0.js -o codemirror-5.57.0.min.js

One problem: this stripped out the LICENSE comment at the top of the file.

Turns out you can tell uglify-js not to strip comments that match a specific regular expression.

So I edited the CodeMirror file to use a single /* ... */ comment at the top of the file (instead of multiple // lines) and ran Uglify like this:

npx uglify-js codemirror-5.57.0.js -o codemirror-5.57.0.min.js --comments '/LICENSE/'

For CSS I used clean-css-cli:

npx clean-css-cli codemirror-5.57.0.css -o codemirror-5.57.0.min.css

Using tercer instead

It turns out uglify-js doesn't support ES6 at all. You can use tercer instead:

npx terser codemirror-5.57.0.js -o codemirror-5.57.0.min.js --comments '/LICENSE/'

Discovered in datasette-edit-tables #16.

Created 2020-08-30T11:20:43-07:00, updated 2020-09-30T17:57:52-07:00 · History · Edit