Sprockets: JavaScript dependency management and concatenation (SvN)
37signals.com
37signals.com
One problem I foresee with this approach is that it could make debugging harder, if you have to look at the "concatenated" javascript file in production to make sense of what code's included and not, it's harder to know what comes from where than if you just included the original source code files.
One solution would be to have a way to switch between "concatenated" and "normal" modes easily (maybe it's already possible, I don't know), so that normal development would be unimpeded. But you'd have to resort to the big concatenated file for debugging in production.
Another solution would be to somehow preserve the information about what comes from where, so that via a browser plugin you could browse the concatenated file as if it were the original modular files.
Lately I'm getting really interested in ways to preserve information about "what comes from where" so that you could point to something in a generated output and ask the system to explain the sum of all inputs and considerations (decisions, transformations) that resulted in that "atom" being generated in the output through the various "semantic levels". You usually generate a low-level representation from a higher-level representation and it can be useful to proceed carefully from one transformation to the next so that you preserve meaningful semantic information about intermediate stages.
http://code.google.com/p/django-compress/
We just started using it in a project I'm working on. Most excellent.
django-compress makes you define your source files and concatenation order in an array in a configuration file. Sprockets lets you define your dependencies with annotations in the source files themselves, meaning plugins can have their own dependencies and integrate themselves with your applicaton's build system.
Sprockets doesn't do minification yet.
Sprockets doesn't do CSS concatenation, either. But it does have an asset provisioning feature, where plugins can specify a collection of files (e.g. css, images) that should be copied into the document root. (Rails itself provides basic CSS and JS concatenation.)
It's nice to see other similar but independent implementations of the same idea.
What I described is essentially what we do in Objective-J. During development it just resolves the dependencies at runtime, but when you deploy you can flatten them into one big file.
EDIT: after reading the CGI source, they use some mod_rewrite trickery to avoid running the CGI if theres a cached file.
I like the idea of distributing a PHP wrapper. I'll try to add one for the next release.
And there's no "ploy." Interesting theory, though.
This will have very little effect on caching. You will still need to configure your web server for optimal caching, by setting the Cache-control and Expires headers, for example.
Even so, this still has two main benefits: The JavaScript is minified, which will reduce the amount of raw data sent to remote users. Plus, by concatenating all the files together, you avoid the need for multiple separate HTTP requests.
AFAIK, that includes the latest safari, firefox, and IE8.
However, it's still obviously a good idea to concat and minify all your scripts.
I had previously been told that a JavaScript download basically halts the entire processing of the remainder of the page -- including any downloads that might occur there (CSS, images, and more JS) -- because the browser can't start parsing the rest of the page until it has interpreted the JavaScript. That's why it's best to move your JavaScript down to the bottom of the page.
Is this just the way older browsers used to behave? Are there still any restrictions like this that I should be aware of?
eg gzip(concat(A, B)) compresses to a smaller file than gzip(A) + gzip(B)
For example, if I take 20 js files from Mibbit and gzip them individually, total size is 47,890 If I concat then gzip, it's 42,681