views:

1347

answers:

4

I've been using yuicompressor.jar on my test server for on-the-fly minimisation of changed JavaScript files. Now that I have deployed the website to the public server, I noticed that the server's policies forbid the use of exec() or its equivalents, so no more java execution for me.

Is there a decent on-the-fly JS compressor implemented in PHP? The only thing resembling this that I was able to find was Minify, but it's more of a full-blown compression solution with cache and everything. I want to keep the files separate and have the minimised files follow my own naming conventions, so Minify is a bit too complex for this purpose.

The tool, like yuicompressor, should be able to take either a filename or JavaScript as input and should either write to a file or output the compressed JavaScript.

EDIT: To clarify, I'm looking for something that does not have to be used as a standalone (i.e. it can be called from a function, rather than sniffing my GET variables). If I just wanted a compressor, Minify would obviously be a good choice.

+2  A: 

Yes there is, it's called minify.

The only thing in to worry about in the way of complexity is setting up a group, and there's really nothing to it. Edit the groupsConfig.php file if you want multiple JS/CSS in one <script> or <link> statement:

return array(
     'js-common' => array('//js/jquery/jquery-1.3.2.min.js', '//js/common.js', '//js/visuals.js',
'//js/jquery/facebox.js'),
     'css-common' => array('//css/main.css', '//css/layout.css','//css/facebox.css')
);

To include the above 'js-common' group, do this:

<script type="text/javascript" src="/min/g=js-common"></script>
karim79
As I said. I can't find a way to run minify from within an application. Heck, it even expects certain GET params to be set rather than taking a filename as argument.It's probably good, but not what I'm looking for.
Alan
Minify is not complex. You can have a specific file minified simply by using something like <script type="text/javascript" src="/min/f=somefile"></script>
karim79
It's complex in terms of its "API". Actually it doesn't really have one. It's a standalone tool that reads superglobals for input. If you can point me to a wrapper that lets me use Minify from within an application, it would be a valid option.
Alan
@Alan - Minify is based on a bunch of classes, all of which can be used from within your application. For example, this: http://code.google.com/p/minify/source/browse/trunk/min/lib/Minify/HTML.php I'm not digging for rep as I've maxed out for the day. Just trying to help :)
karim79
No allegations made. Maybe I misunderstand Minify, but the fact it comes with a huge config tool and that the controllers the main class use fetch their input arguments from GET made me think it's a pure standalone tool.If I'm mistaken, I guess I'll have to hack the source a bit to figure out how the actual compression is handled and see how I can interface with that part directly.
Alan
@Alan - the JS compression bit is based on JSMin - http://www.crockford.com/javascript/jsmin.html. The 'configurator' is not a necessity, it's just a convenience tool or 'starter kit'. The *only* thing that requires work is declaring a groups array which I have already presented in my answer. Look into it, is my advice.
karim79
Thanks for pointing that out. I just realised that if you take away the CSS and HTML compressors and the YUICompressor bridge, you might as well use JSMin directly. JSMin seems to do pretty much what I want, so I'll go and use that or Minify's own version of it. Thanks.
Alan
A: 

Dean Edward's Packer (online version here)

The second most popular compressor; I have found it matches or outperforms YUI in many cases (when variables are shrunk).

SamGoody
A: 

Try Lissa:

Lissa is a generic CSS and JavaScript loading utility. Lissa is an extension of the YUI PHP Loader aimed at solving one of the current loader limitations; combo loading. YUI PHP Loader ships with a combo loader that is capable of reducing HTTP requests and increasing performance by outputting all the YUI JavaScript and/or CSS requirements as a single request per resource type. Meaning even if you needed 8 YUI components which ultimately boil down to say 13 files you would still only make 2 HTTP requests; one for the CSS and another for the JavaScript. That's great, but what about custom non-YUI resources. YUI PHP Loader will load them, but it loads them as separate includes and thus they miss out on benefits of the combo service and the number of HTTP requests for the page increases. Lissa works around this limitation by using the YUI PHP Loader to handle the loading and sort of YUI and/or custom resource dependencies and pairs that functional with Minify.

stefpet