Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to minify JS in PHP easily...Or something else

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?

like image 624
RickyAYoder Avatar asked Jun 12 '12 15:06

RickyAYoder


People also ask

How do I minify JavaScript code?

To minify JavaScript, try UglifyJS. The Closure Compiler is also very effective. You can create a build process that uses these tools to minify and rename the development files and save them to a production directory.

Should you minify JavaScript?

It is important to minify your CSS and minimise JavaScript files so they can load faster on your web pages. There are many reasons why you should minify your CSS and JavaScript: Reduce file size: The more code there is in a file, the larger it will be. Minified code is usually much smaller than the original version.

How do you minify source code?

Go to minifycode.com and click the CSS minifier tab. Then paste the CSS code into the input box and click the Minify CSS button. After the new minified code is generated, copy the code. Then go back to the css file of your website and replace the code with the new minified version.

How much faster is minified JS?

The minified version of this sample code is 48% smaller. In some cases, minification can reduce file size by as much as 60%. For instance, there's a 176 kb difference between the original and minified version of the JQuery JavaScript library. Minification has become standard practice for page optimization.


2 Answers

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.

require 'jsmin.php';

if(filemtime('scripts_template.js') < filemtime('scripts_template.min.js')) {
  read_file('scripts_template.min.js');
} else {
  $output = JSMin::minify(file_get_contents('scripts_template.js'));
  file_put_contents('scripts_template.min.js', $output);
  echo $output;
}

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.

require 'JShrink/Minifier.php';

if(filemtime('scripts_template.js') < filemtime('scripts_template.min.js')) {
  read_file('scripts_template.min.js');
} else {
  $output = \JShrink\Minifier::minify(file_get_contents('scripts_template.js'));
  file_put_contents('scripts_template.min.js', $output);
  echo $output;
}
like image 115
Robert K Avatar answered Sep 21 '22 17:09

Robert K


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

like image 26
Louis-Philippe Huberdeau Avatar answered Sep 22 '22 17:09

Louis-Philippe Huberdeau