Minify tool that can be executed through terminal

I am looking for a tool or a way to minify (similar to Compress my code and this question on Stack) all of the code in my .xml, .css, .html and .js files through Ubuntu's terminal. Eventually I will bash script the process, but at the moment I would like to just find something to test. Is there a tool out there that I can use to compress all these file formats through the terminal?

3

9 Answers

This is not the best option but it's probably the easiest. The YUI compressor was long thought to be the best compressor for Javascript and CSS, offering 20-40% improvements over other minifiers.

It has since been superseded by newer projects like Uglify.JS (which Grunt will probably suggest) but it's still a fairly easy thing to get up and running in Ubuntu.

sudo apt-get install yui-compressor

That's it. Now you can run yui-compressor myfile.js and it'll do its magic, just not as well, or as conveniently as a properly install Node/Grunt/Uglify+YUI stack.

13

You can minify js easily with node and uglify-js from command line:

  • install uglify-js with npm install uglify-js -g

  • run it uglifyjs app-test.js > app-test.min.js


For css I would suggest clean-css (probably the most stable css minifier on npm)
example usage:

cleancss -o public-min.css public.css

As far as html is concerned usually minification is not usually worth the time you invest in setting it up, but I have tried html-minifier and it's an awesome tool.

Whatever you do just be sure that you gzip what you're serving.


Edit 05-03-2020

Nowadays, instead of using uglify-js, js developers usually use terser.

6

Use minify - unlike the other suggestions, this tool minifies a lot more file types:

CSS text/css
HTM text/html
HTML text/html
JS text/javascript
JSON application/json
SVG image/svg+xml
XML text/xml
2

JavaScript Minifier is a Online JavaScript Minifier project by Andrew Chilton. Free, decent and no installation needed

curl -X POST -s --data-urlencode '' > my.min.js

There is no reason to minify php files (except you have very limited disk space and want to use every bit of it).

If you could add a goal (What do you want to accomplish and why?), somebody might show you a better way.

JS and CSS files are minified on runtime and cached in most webprojects. There is minify (), a php "library" which is able to do exactly this. (can also be executed with php from the terminal)

But keep in mind that one big javascript file does not necessary load faster than 5 small files. If you need a reason and a solution for this statement, take a look at

May the source be with you...

I have had good results with Closure Compiler.

The Closure Compiler is a tool for making JavaScript download and run faster. Instead of compiling from a source language to machine code, it compiles from JavaScript to better JavaScript. It parses your JavaScript, analyzes it, removes dead code and rewrites and minimizes what's left. It also checks syntax, variable references, and types, and warns about common JavaScript pitfalls.

It’s developed by Google and written in Java. It’s packaged for Debian-based systems as closure-compiler and is easily installed on Ubuntu systems. As it doesn’t use a GUI, it requires the more lightweight default-jre-headless package.

It’s slower that YUI compressor but the resulting filesize is (slightly) smaller. It also prints useful warning messages, similar to compilers for other programming languages.

Documentation: Getting started

Usage:

closure-compiler --js input.js --js_output_file output.js
2

I would recommend using Grunt.js. It's an automation tool that has minifiers available as plugins and can be run in your terminal via Node.js. It should not be necessary to minify PHP as the code executes on the server side and only its HTML output is sent to the client.

You can find available plugins here

1

Another option is to use npx command from Node.js. npx runs a command of a Node.js package without installing it explicitly.

# Minify JS
npx -p uglify-js uglifyjs -o app.min.js app.js common.js
# Minify CSS
npx clean-css-cli -o style.min.css css/bootstrap.css style.css
# Minify HTML
npx html-minifier index-2.html -o index.html --remove-comments --collapse-whitespace
# XML
npx pretty-data-cli --type xml --minify input.xml > input.min.xml

I found this java library for HTML (you need to have java installed):

Command line usage is simple:

java -jar htmlcompressor.jar inputfile.html > outputfile.html

And for CSS I use this (better compression rates than other tools that I've tried):

Command line usage:

accss inputfile.css > outputfile.css

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like