views:

148

answers:

3

I am trying to make development easy and have highly optimized output in production.

The goals of what I am trying to do is:

  • Make production pages fast! I would like that the Google Page Speed and YSlow return the best scores. This means:
    1. Combine and compress JS files and CSS and position the group in the right place (bottom or top of the page) in the HTML. For .js Google Closure seems the best choice.
    2. .JS and .CSS are smartly cached but be sure that they get reloaded when of the .JS or CSS componet is updated. 301 File not changed etc.
    3. Cache type: I think cache on disk is fine. Consider APC and Memcache or Redis if they significantly improve speed.
    4. Capable to specify and use lazy load of .JS when necessary or at least not to block the page rendering.
    5. (Optional) Compress the HTML too.
  • Make website development easy:
    1. Use short command in the .php file when you want to include .js or .css and compress them only in the production environment
      • Use syntax like pack_js(['first.js', 'second.js' 'third.js']) and pack_css(['first.less', 'second.less' 'third.css'], true)
      • It is easy to configure develpment or production environment. Maybe just a call to SetDebug(true or false). Default production
      • Easy to set up cache folders and source folders
    2. Use of LESS to make CSS development sucks less. Automatically compile LESS files in CSS in production but use of LESS.js in development so that each time you change the .less file in development it is updated on the server.
    3. (optional) In development it includes a JS and a LESS console similar to the shell at https://www.squarefree.com/bookmarklets/webdevel.html

Note: it is OK to use Apachee modules and .htaccess files if they significantly speed up the process. But it should be able to set them up quicky, ideally with just a setup command.

Is there something that do this? Or what are the best resources to start?

I would prefer a solution that consists of a PHP script (eventually few .php files, .htaccess and compressing executables) that compresses the .JS with the Google Closure Compiler and compress/compile the CSS/LESS files stripping out useless comments and white spaces. A special cookie could be used on production server to output the development version.

I would like to have:

A php function usable like this: pack_js(['first.js', 'second.js', 'third.js']) that write something like:

On developments servers:

<script type="text/javascript" src="static/js/first.js"></script>
<script type="text/javascript" src="static/js/second.js"></script>
<script type="text/javascript" src="static/js/third.js"></script>

On production servers (if the special cookie is not present):

<script type="text/javascript" src="cache/12a42323bfe339ea9w.js"></script>

Another function: pack_css(['first.less', 'second.less', 'third.css'], true) that write something like:

On developments servers:

<link rel="stylesheet/less" href="/static/css/first.less" type="text/css" />
<link rel="stylesheet/less" href="/static/css/second.less" type="text/css" />
<link href="/static/css/third.css" type="text/css" />
<script src="http://lesscss.googlecode.com/files/less-1.0.21.min.js"&gt;&lt;/script&gt;

<script type="text/javascript" charset="utf-8">
    less.env = "development";
    less.watch();
</script>

On production servers (if the special cookie is not present):

<link href="/cache/46537a8b8e876f7a8e7.css" type="text/css" />

the second parameter specify if the less.js should be output on the development server

Obviously 12a42323bfe339ea9w.js and 46537a8b8e876f7a8e7.css are the optimized, packed and compiled version of the script. This solution should be able to detect when a source file change and recompile the scripts for production. It should be configurable to regarding locations of the script and type of caching (disk is fine). Ideally the pack_js should probably have an option to make possible lazy load of the js in production.

Every suggestion is welcomed.

+2  A: 

Why don't you use a project build system to deploy your application to the production server that does just that? For PHP you might like Phing, since it would allow you to write extra plugins in PHP, that you can execute while deploying. Other options you might want to consider of you take this route are Ant and Capistrano (and there's lots of others), but these require knowledge of other languages (granted, you could start the php parser from them if you really wanted to...).

wimvds
That may be a possible route. I did not know Phing. I will look into that. Do you have something to suggest that already do what I ask? Maybe some example files?I would like to keep a easy syntax into the .php so I can control the order and position of groups of .js and .css are in the output HTML?
Chris Cinelli
A: 

Great Question!

I've worked with a process with similar requirements, but we took somewhat of a different approach. I realise its maybe not exactly what you're after, but would still make some recommendations:

  1. Make the compression/compilation part of the shipping process.(You may already be doing this, but it wasn't clear from the above).
  2. Compress/compile it on the development servers too. It might be a hassle for debugging/testing but you want to be able ensure the production version and test versions are as similar as possible. If you have the luxury of multiple development stages, then you could possibly compress on one of them.
  3. Only do the compression/compilation if it passes some sort of quality scan (e.g. jslint)
  4. Don't combine the modules - keep them separate. The performance benefits you will gain will be so negligible, as to be almost pointless.
  5. Don't change the HTML, just change the dependant modules content.

I realise its a bit different from what you were intending, but in my experience it leads to a more robust development promotion model.

James Wiseman
I meant to have the compilation part on fly the 1st time a .js change and then cache the file generated. (1) It may be Ok but it must be automatic at deployment time. (2) I agree. Test should run on prod. ver. of the files. (3) Yes, creating a nice way to signal error in the JS is something that I would like to have. (4+5) Not true. Each module is a http request to the server. To make the site fast you need to reduce them to the min plus less.js must be only in production. HTML can be also stripped out of useless comments and white spaces. Do you have any suggestions on where I should start?
Chris Cinelli
There's nothing I know that already does this for you. It sounds like you have quite a specific process to your organsiation, and you'll need to build it yourself. I don't know the scale of your modules, or the expected traffic, but would still contest 4 and 5 with you. I guess we'll ahve to agree to disagee ;)
James Wiseman
Re 4-5: There are a few articles online that describe the benefits of combining js and css files (ex: http://www.sitepoint.com/blogs/2007/04/10/faster-page-loads-bundle-your-css-and-javascript/ ). I suggest to read High Performance Web Sites -> http://oreilly.com/catalog/9780596529307/ and Even Faster Web Sites -> http://oreilly.com/catalog/9780596522315 and
Chris Cinelli
+1  A: 

Still working on exploring the best solution leveraging the resource already available.

CSS-JS-Booster and Turbine so far looks like the best starting point: http://github.com/Schepp/CSS-JS-Booster and http://turbine.peterkroener.de/

Other JS/CSS Combiners solutions and articles

Resource for JS compression and caching:

LESS compilers/tutorials/tools:

At Deployment time options:

Chris Cinelli