A faster JSMin library for PHP

Friday May 04, 2007 @ 09:19 PM (PDT)

I wasn't happy with the performance of the existing PHP port of JSMin, so I wrote my own implementation, which is significantly smaller and faster. This new library will be included in the next release of Minify, and you're welcome to download it and use it in your own projects:

require 'jsmin.php';

// Output a minified version of example.js.
echo JSMin::minify(file_get_contents('example.js'));

Comments

Oh neat, I started checking out minify when I saw your post on wednesday but haven't put it into use yet. Thanks on bringing awareness to that while making it work better.

Thanks for this script...I was using jsmin but I am going to give this a try and see how things work out.

Unfortunately I'm on PHP 5.2.0 (0.0.1 versions away, *cries*) so when I used minify the response I got from the server was...

"Call to undefined function sys_get_temp_dir()"

This function seems to be supported as of PHP 5.2.1...reading the manual comment for this page suggested that it is possible to create this function...

Check here for details...http://us2.php.net/sys_get_temp_dir

Prob not best workaround to add this to the start of the minify file but just for experiementing with it before I upgrade seems to work ok.

Strict Standards: Redefining already defined constructor for class JSMin in jsmin-1.0.1.php on line 167

I've released version 1.1.0, which fixes the error. Thanks.

What about IE conditional comments ?

/*@cc_on

if ( @_jscript_version == 5.6 )

document.execCommand("BackgroundImageCache", false, true);

@*/

At the moment, conditional comments are clobbered. That's because this is a direct port of Doug Crockford's jsmin.c, which does the same thing.

I don't intend to change the default behavior to be different from jsmin.c, but I'll consider adding an option not to remove conditional comments.

Hello,

Thanks for the port into PHP5 and its simple usage. I looked at the source but don't think there is an option switch to further remove the newlines

Notes: Support is required for the many enhanced javascript ‘DOMLoadEvent’ scripts that DO exist: – such events require the preservation of the following conditional comments (code):

/*@cc_on
@if (@_win32 || @_win64)
// code
@end @*/

I use such a DOMLoadEvent JS loader on my website and JSMin.PHP strips out the (above) needed IE code conditionals. I am now looking to write my own filter routines into JSMin.PHP – or to use the alternate MyMin.PHP from Andrea Giammarchi. The technique used by Andrea in his MyMin.PHP module is very effective, however it is NOT a simple ‘plug-in’ replacement for JSMin.PHP despite Andrea’s statement that it ‘replaces’ it. Changes must be made (groan)...

However, apart from this essential missing support event (in JSMin.PHP) you have a some great ideas! Thank you for sharing the project. It benefits us all. Thank you

Peter Bowey

hello,

I’like to thank you first for the jsmin, library,

I have a bug with inline comments, this is my script

//AC_AX_RunContent( ‘width’,‘250’,‘height’,‘260’,‘src217;,‘http://www.youtube.com/v/XxIRHcFYOGs&hl=fr&fs=1&’,‘type’,‘application/x-shockwave-flash’,‘allowscriptaccess’,̵6;always’,‘allowfullscreen’,‘true217;,‘movie’,̵6;http://www.youtube.com/v/XxIRHcFYOGs&hl=fr&fs=1&’ ); //end AC code

after minifiyng the code remains and only // are removed, so that I need to strip inline comments before minifying files.

I hope you’ll fix this soon,

thanks again

- Thierry Bela

sorry,

I double checked my code, the problem is from me.

thank you.

- Thierry Bela

Hey, this is really nice. I just wrote a PHP class that will automatically combine external JS into a single file, then uses your script to minify it.

Post at http://any.onecancode.com/?p=18, code at http://github.com/anyonecancode/jsFold/blob/master/classes/class.jsfold.php

It’s my first time using github, or posting code publicly for that matter, so I hope I followed all the appropriate conventions for giving credit, etc. Anyhow, thanks for the script, very handy!

Copyright © 2002-2012 Ryan Grove. All rights reserved.
Powered by Thoth.