AppStateESS / phpwebsite

phpWebSite Content Management System
Other
33 stars 39 forks source link

Javascript, Coffeescript, and CSS Minification and Compilation #125

Open jtickle opened 11 years ago

jtickle commented 11 years ago

We really need to rein in how many CSS and Javascript files get downloaded per page load. We probably won't be able to get away with only one CSS and one Javascript file overall, but maybe, who knows.

I also mention Coffeescript because there's no reason we shouldn't support it. It's a sort of different syntax for Javascript that, in in this issuer's opinion, makes the code easier to read. But it does NOT run straight in the browser [without help, see below] - it has to be compiled to Javascript, but it does generate relatively clean Javascript code. If you're compiling Javascript anyway, seriously consider using Coffeescript instead.

Compilation and minification is somewhat resource-intensive. Compiling and minifying CSS and Javascript once takes almost no time at all, but if you do it on every page load, a certain systems administrator is going to get punchy. Comp/min should happen at the time someone changes the javascript or the CSS on the server side. It should not even happen at all during development mode, and should happen only on install and on update in production mode, generating static, cacheable files that will be read directly by Apache.

I have done a little research on libraries we can use, which I'll outline here:

CSS Compilation:

CSS Minification:

Javascript Compilation and Minification:

Coffeescript Compiler:

Angular.js:

Everything Minifier:

Let Someone Else Do It:

┆Issue is synchronized with this Asana task

jlbooker commented 11 years ago

:+1: from me.

As we do this, I think we should also consider the "modular" way of writing javascript components, and how those get required/added to the page, and have a way phpws modules can inject/include their own javascript files (having them properly minified/complied at some point in the process).

We need to strike a balance between "download all the javascript!!1 (in one big minified file)" and "make tons of requests for each little javascript". Common files and shared libraries can obviously be minified into one file.

My proposal: each module should get one minified js file to be included when that module has content on the page. The code can be stored in many separate files, be compiled/minified once, and at run-time have the minified file requested by the browser. This keeps us from including every modules javascript all at once, but also keeps the number of HTTP requests for js resources limited to something more reasonable.