EllisLab text mark
Advanced Search
     
JSMin_PHP Library Port
Posted: 21 January 2009 01:56 PM   [ Ignore ]
Avatar
Joined: 2009-01-21
109 posts

This is a really quick port I did of the JSMin-php library.  Very little was changed, so I don’t even really want to take credit for it.  Usage is exactly what you would expect.

Usage:
Drop the library in your libraries folder, and load the library as usual:

$this->CI->load->library('jsmin'); 

Minify a string like so:

$this->jsmin->minify($string); 

I usually use it with file_get_contents(), like so:

$this->jsmin->minifyfile_get_contents($file_reference'r') )

See the attached zip file for the full library.

 Signature 

Do you use CSS or JavaScript? Carabiner makes your life easier.  I promise.

CI-Disqus makes playing with the Disqus API a snap.

 
Posted: 21 January 2009 01:59 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2009-01-21
109 posts

I forgot to mention that this is a PHP5 only library, as per the original JSMin-php file.  If there is interest in a PHP4 version, I might be able to make that happen…

 Signature 

Do you use CSS or JavaScript? Carabiner makes your life easier.  I promise.

CI-Disqus makes playing with the Disqus API a snap.

 
Posted: 28 January 2009 06:22 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2009-01-21
109 posts

New version.  Nothing has really changed, just updated the documentation to be more accurate.

 Signature 

Do you use CSS or JavaScript? Carabiner makes your life easier.  I promise.

CI-Disqus makes playing with the Disqus API a snap.

 
Posted: 28 January 2009 07:05 PM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2008-02-04
534 posts

I have been meaning to implement this myself. Saved me some work smile

Why are you creating a new jsmin object inside itself?  Couldn’t you just do this?

public static function minify($js{

    $this
->input       str_replace("\r\n""\n"$input);
    
$this->inputLength strlen($this->input);
    
    
$return $this->min();

    
// Housekeeping
    
$this->input       null;
    
$this->inputLength null;

    return 
$return;

  

(And remove that from the constructor)

 
Posted: 28 January 2009 09:17 PM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2009-01-21
109 posts

I’m not sure why it was written that way, other than for the sake of brevity. You’d have to ask Ryan Grove (or perhaps Doug Crockford.)  That is the way the class is written in the original JSMin PHP port.  In porting it to CI, I only added the log_message function call and set a default value for $js in the constructor.

Your solution should work, assuming you match variable names (changing $js to $input):

public static function minify($input{

    $this
->input       str_replace("\r\n""\n"$input);
    
$this->inputLength strlen($this->input);
    
    
$return $this->min();

    
// Housekeeping
    
$this->input       null;
    
$this->inputLength null;

    return 
$return;

  

I still prefer the way it was originally written, if only because it’s cleaner.  Thanks for the comment!

 Signature 

Do you use CSS or JavaScript? Carabiner makes your life easier.  I promise.

CI-Disqus makes playing with the Disqus API a snap.

 
Posted: 28 January 2009 10:32 PM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2009-01-21
109 posts

I’ve used this library in a full fledged asset management library called Carabiner.  Check it out!

 Signature 

Do you use CSS or JavaScript? Carabiner makes your life easier.  I promise.

CI-Disqus makes playing with the Disqus API a snap.

 
Posted: 16 June 2010 04:39 PM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Avatar
Joined: 2010-05-13
12 posts

A big thanks for porting this - very useful! smile

 
Posted: 17 June 2010 10:41 AM   [ Ignore ]   [ # 7 ]   [ Rating: 0 ]
Avatar
Joined: 2007-03-14
49 posts

yummy yummy! Looks promising, will be implemented in my current project. Thank you!

 
Posted: 07 August 2011 03:26 PM   [ Ignore ]   [ # 8 ]   [ Rating: 0 ]
Joined: 2009-03-04
149 posts

I got a string like so

  Lego (trademarked in capitals as LEGO) is a line of construction toys manufactured by the Lego Group, a privately held company based in Billund, Denmark. The company’s flagship product, Lego, consists of colorful interlocking plastic bricks and an accompanying array of gears, minifigures and various other parts. Lego bricks can be assembled and connected in many ways, to construct such objects as vehicles, buildings, and even working robots. Anything constructed can then be taken apart again, and the pieces used to make other objects. The toys were originally designed in the 1940s in Denmark[1] and have achieved an international appeal, with an extensive subculture that supports Lego movies, games, video games, competitions, and four Lego themed amusement parks.

When I try to minify it with php jsmin, I got “500 internal server error”. What’s special about that string that jsmin can’t parse ?