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:
- 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.
- .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.
- Cache type: I think cache on disk is fine. Consider APC and Memcache or Redis if they significantly improve speed.
- Capable to specify and use lazy load of .JS when necessary or at least not to block the page rendering.
- (Optional) Compress the HTML too.
- Make website development easy:
- 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
- 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.
- (optional) In development it includes a JS and a LESS console similar to the shell at https://www.squarefree.com/bookmarklets/webdevel.html
- Use short command in the .php file when you want to include .js or .css and compress them only in the production environment
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"></script>
<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.