Newsletter
TechAnV Blog
Get updates on security engineering, Rust, eBPF, and DevSecOps. No spam, unsubscribe anytime.
Check your inbox and click the confirmation link to complete your subscription.
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):
1npx uglify-js codemirror-5.57.0.js -o codemirror-5.57.0.min.jsOne 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:
1npx uglify-js codemirror-5.57.0.js -o codemirror-5.57.0.min.js --comments '/LICENSE/'For CSS I used clean-css-cli:
1npx clean-css-cli codemirror-5.57.0.css -o codemirror-5.57.0.min.cssUsing tercer instead#
It turns out uglify-js doesn’t support ES6 at all. You can use tercer instead:
1npx terser codemirror-5.57.0.js -o codemirror-5.57.0.min.js --comments '/LICENSE/'Discovered in datasette-edit-tables #16.
Trying out Uglify interactively#
UglifyJS 3: Online JavaScript minifier is a useful way to try out Ugllify since it shows you the results as you type, which makes it easy to spot tiny changes you can make that result in a shorter minified output.