I've done some looking around, but I'm still confused a bit.
I tried Crockford's JSMin, but Win XP can't unzip the executable file for some reason.
What I really want though is a simple and easy-to-use JS minifier that uses PHP to minify JS code--and return the result.
The reason why is because: I have 2 files (for example) that I'm working between: scripts.js and scripts_template.js
scripts_template is normal code that I write out--then I have to minify it and paste the minified script into scripts.js--the one that I actually USE on my website.
I want to eradicate the middle man by simply doing something like this on my page:
<script type="text/javascript" src="scripts.php"></script>
And then for the contents of scripts.php:
<?php include("include.inc"); header("Content-type:text/javascript"); echo(minify_js(file_get_contents("scripts_template.js")));
This way, whenever I update my JS, I don't have to constantly go to a website to minify it and re-paste it into scripts.js--everything is automatically updated.
Yes, I have also tried Crockford's PHP Minifier and I've taken a look at PHP Speedy, but I don't understand PHP classes just yet...Is there anything out there that a monkey could understand, maybe something with RegExp?
How about we make this even simpler?
I just want to remove tab spaces--I still want my code to be readable.
It's not like the script makes my site lag epically, it's just anything is better than nothing.
Tab removal, anyone? And if possible, how about removing completely BLANK lines?
Depending on the restrictions of your server (eg, not running in safe mode), perhaps you can also look beyond PHP for a minifier and run it using
shell_exec()
. For instance, if you can run Java on your server, put a copy of YUI Compressor on the server and use it directly.Then scripts.php would be something like:
Other suggestion: build the minification step into your development workflow, before you deploy to the server. For example I set up my Eclipse PHP projects to compress JS and CSS files into a "build" folder. Works like a charm.
Take a look at Assetic, a great asset management library in PHP. It is well integrated with Symfony2 and widely used.
https://github.com/kriswallsmith/assetic
Using "PHPWee": https://github.com/searchturbine/phpwee-php-minifier (which also uses
JSmin
), I pushed @Robert K solution a little bit further.This solution allows minifying both CSS and JS files. If the non-minified file cannot be found, it will return an empty string. If the minified file is older than the non-minified, it will try to create it. It will create a sub-folder for the minified file if it doesn't exist. If the method can minify the file successfully, it returns it either in a
<script>
(javascript) or a<link>
(CSS) tag. Otherwise, the method will return the non-minified version in the proper tag.Note: tested with PHP 7.0.13
In a (Smarty) template, you might use those methods like this:
Unit tests:
Additional notes:
phpwee.php
I had to replace<?
by<?php
.class_exists()
was not able to find the classes even though they were in the same file). I solved this problem by removing the namespace in every file.I have used a PHP implementation of JSMin by Douglas Crockford for quite some time. It can be a little risky when concatenating files, as there may be a missing semicolon on the end of a closure.
It'd be a wise idea to cache the minified output and echo what is cached so long as it's newer than the source file.
You could also try JShrink. I haven't ever used it before, since I haven't had difficulty with JSMin before, but this code below should do the trick. I hadn't realized this, but JShrink requires PHP 5.3 and namespaces.