Skip to content

Latest commit

 

History

History
86 lines (66 loc) · 3.03 KB

README.md

File metadata and controls

86 lines (66 loc) · 3.03 KB

Build License

Minifier

What is this?

This project provides Java classes to do one thing: safely "minify" web resources. The key word is "safely": the aim is not to produce maximal size reduction, nor introduce any morphological changes, but to provide reasonable file size reduction in most cases.

Getting started

There are two minifier classes:

  • CSSMinifier for CSS; and
  • JSMinifier for JavaScript.

They both implement Minifier, which takes the text of a resource and returns it minified:

String minify(String source) throws MinificationException;

A minifier keeps no state between calls, so a single instance can minify any number of resources, from any number of threads. For example, to minify a stylesheet in css:

Minifier minifier = new CSSMinifier();
try {
    String minified = minifier.minify(css);
} catch (MinificationException e) {
    // Handle exception
}

If you have a Reader and a Writer, Minifier also has a default method that reads the whole resource from one, and writes it, minified, to the other:

void minify(Reader reader, Writer writer) throws IOException, MinificationException;

It writes nothing unless minification succeeds, and it doesn't close either stream: that's up to you.

A MinificationException says what went wrong, and where: its message is something like Unterminated comment at line 12, column 5, and line() and column() return the position, both numbered from 1.

Comments are removed, apart from those that start with /*!, which mark a licence or copyright notice by convention: those are kept, where they are.

Using Minifier

You can use Minifier in your projects by including it as a Maven dependency:

<dependency>
  <groupId>net.logicsquad</groupId>
  <artifactId>minifier</artifactId>
  <version>2.0</version>
</dependency>

On the module path, its module is net.logicsquad.minifier.

If you're building a Maven project, and just want to minify project resources during the build, check out Minifier Maven Plugin, which uses Minifier to do the work.

Contributing

By all means, open issue tickets and pull requests if you have something to contribute.

References

JavaScript minification is based on Douglas Crockford's original C-based JSMin, via a translation from C to Java by John Reilly. Code from these projects is used here under license.

CSS minification is based on the CSSMin project by Barry van Oudtshoorn. Code from this project is used here under license.