This extension adds Douglas Crockford's jsmin functionality to PHP.
http://www.crockford.com/javascript/jsmin.html
This extension currently works with PHP 7.0.0+. Use versions < 3.0 for PHP 5.X support.
You can install this extension by using the pecl
command:
pecl install jsmin
To install via source, clone this repo and then run the following:
$ cd /path/to/source
$ phpize
$ ./configure
$ make install clean
Then, move the built module to your extensions directory.
For those on OSX, you can use Homebrew to manage PHP versions. Included is the jsmin extension.
Take a look at Homebrew PHP for installation notes.
Thanks to Jon Whitcraft for pushing the definition for jsmin to the project.
You will want to enable the extension in php.ini by adding:
extension="jsmin.so"
Using jsmin is simple.
Use this function to minify JavaScript. It accepts a single string argument.
<?php
$javascript = <<<JS
/**
* My JavaScript Library
*/
var notes = "jsmin is easy!";
JS;
echo jsmin($javascript);
Example output is the following:
var notes="jsmin is easy!";
You can also retrieve the latest error by passing by reference to the second argument:
jsmin($javascript, $error);
Returns error code of last call to jsmin().
Returns an error message (string) for the last call to jsmin().
Discovery Communications developed a similar extension in-house for minifying bundled JavaScript.
I decided to take the most recent source from Douglas Crockford's JSMin and port / manage the extension for PECL.