Skip to content

Javascript, Coffeescript, and CSS Minification and Compilation #125

@jtickle

Description

@jtickle

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:

  • http://leafo.net/lessphp/ - tested against Twitter-Bootstrap, works! Does not minify anything; it turns all of the LESS files into one CSS file.

CSS Minification:

Javascript Compilation and Minification:

Coffeescript Compiler:

Angular.js:

  • The only extra compilation concern for Angular code in particular is that it determines which dependencies to inject based on the function signature, and minifaction completely goobers the function signature and breaks Angular. There is a utility called ngMin that turns "inferred dependency injection" into "inline dependency injection" and solves the problem, but has no PHP implementation. Chances are if the developer is using Angular, they already have a npm/yeoman/grunt setup going on anyway and we don't need to worry about it, or they can just use the Inline Dependency Injection Syntax instead and never have any problems.

Everything Minifier:

Let Someone Else Do It:

  • https://github.com/kriswallsmith/assetic - Assetic is an asset management library for PHP that takes care of the management of all assets that aren't PHP code (images and such). One thing I like about it is that you can tell it which compiler and minifier it should use, so we don't have to say "lessphp is your only option"; we can include that by default but the end-user can configure it to use node.js utilities or whatever they prefer.

┆Issue is synchronized with this Asana task

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions