2

I am currently in the process of writing a javascript class that loads libraries from my server dependant upon what the site needs. Much like the Google API e.g. google.load('jquery','1.7.1').

One of the things that I would like to do is combine all the JS/CSS files into one and minify them using PHP. I have had a good look around the net and whilst I was able to find a fair few questions on Stackoverflow that refer to this topic, they seemed somewhat outdated. So, resultantly, I have decided to re-open this debate in order to gain some up to date info on this subject.

I was able to find two PHP Javascript minifiers on the net, these being

  1. JShrink
  2. Javascript Packer

I did actually find a third (JSMin), but this was unmaintained

My questions are quite simple:

  1. Are these scripts reliable and maintained? Do they even need to be maintained?
  2. Do they compress CSS?
  3. Which script is better?
2
  • 1
    Sass (possibly LESS and Stylus as well) will minify CSS and more. Even if you aren't interested in Ruby, it's still worth installing on your development server just for Sass. Commented Oct 23, 2012 at 13:57
  • I'm the author of JShrink, and yes it's still maintained :-) Commented Jan 7, 2014 at 19:19

2 Answers 2

5

I haven't use JShrink or Javascript Packer. I think the best solution is using Google's Closure Compiler for JS minification. You can use their command line Java application on your server (if you have the rights to do so) or access their RESTful API through cURL or Zend_Request.

Don't forget to cache the minified files and to reuse them if the source files didn't changed.

Using cURL it would look like something like that:

$inFile = "sample.js";
$outFile = "sample.min.js";

$ch = curl_init('http://closure-compiler.appspot.com/compile');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array(
  'js_code' => file_get_contents($inFile),
  'compilation_level' => 'SIMPLE_OPTIMIZATIONS',
  'output_format' => 'text',
  'output_info' => 'compiled_code'
)));

file_put_contents($outFile, curl_exec($ch));
Sign up to request clarification or add additional context in comments.

3 Comments

Can you post an example of using the cURL method as cannot seem to find the documentation on this from your link?
Thank you for your edit, I am still not sure which one to go with. How can you be sure that the 'closure-compiler.appspot.com/compile' will not fail? If was a subdomain of Google then I would have some confidence, but I cannot afford the risk of a third party domain due to the fact that they could go offline anytime
The Domain is owned by Google. It is up to you, but i use the Closure Compiler for years in all my bigger projects. Of course your code needs some exception handling for the case that the API is unavailable or your javascript is invalid. In that case you can return the unminified code at least.
0

i'm using this one and it works well http://code.google.com/p/minify/

has some problems if you are developing on windows, might not recognize new version, but modify the file and it will see it

3 Comments

What do you mean it might not recognise new version? I can handle the caching with PHP and filemtime if that is what you mean?
it means that when i was developing on a Windows7 machine, i upgraded my jQuery library and it would still send the old one to the browser, i modified one letter in the .js file and then it send the new one, this bug only happened once
Ah okay, probably just a caching issue. This wouldnt matter because of the way I am going to handle the caching with PHP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.