Managing Javascript & CSS source files

3 September 2010
Early on in the development of Hive, we realised we needed a better way to manage javascript and CSS. As the javascript files got bigger we started having source code merges (shudder) and we occasionally would ship a version with debug code still in it.

There are some really good systems out there for optimising javascript & CSS, but we thought we’d start simple and see how it went. We’ve now rolled this system out over four applications and its working great.

To avoid source code merges, small and frequent check-ins are good, so using a source control system like Accurev where each developer has their own workspace is perfect. Its also easier to maintain code and avoid merges if you can have one javascript class per file – but you end up with lots more files, which can make loading a page slower.

To solve this, we create as many javascript and CSS files as we need, then merge them on the fly. This allows a single url /scripts?version=x to download all required scripts and an XML file manages which scripts will be merged and returned. We also threw the Yahoo YUI library in there to minimize the javascript and CSS and reduce the overall size.

The other issue we were having was with debug code, swapping between debug and production libraries and remembering to remove console.log commands. To solve this our XML file specifies a target, so the web.config can control which javascript files will get included. We also use some REs to strip out debug commands on the fly.

Overall, a dead simple solution to solve 5 problems:

1. More files means easier to maintain and fewer source control merges
2. On-the-fly merging means only one javascript & one CSS per page, resulting in faster page loads
3. Minifying the code means smaller javascript and CSS
4. Stripping out console.log and other debug code means we can leave it in and not worry about it in production
5. Swapping between debug and production versions of libraries is automatic

We can highly recommend the YUI compressor for .NET – which we use in this code snippit:

public string GetJavascript()
{
    try
    {
        // Get the files from the XML index
        List<string> scriptFiles = GetJavascriptFiles();

        // Load the contents and put into a string
        var javascript = MergeFiles(scriptFiles);

        // Optimise
        if (_Target.ToLower() == "live")
        {
            // Strip out console.log, etc
            javascript = RemoveDebugCode(javascript);

            // Minimise using Yahoo YUI
            javascript = JavaScriptCompressor.Compress(javascript);
        }

        // return the result
        return javascript;
    }
    catch (Exception e)
    {
        return e.Message;
    }
}

Business Cards!

1 September 2010

We’ve been in business for 7 months so its about time we got some business cards printed, especially since we’ve finally settled on a logo!

The company is called EndGame, so for a bit of fun we thought we’d put a maze on the back of the card.  The point of a maze is to get to the end, ie that’s your EndGame.  Beautiful.  The logo is sort of half isometric, so we figured the maze needed to be isometric so that it could join up with the logo as the finish point.

Anyway, one thing led to another and soon I’m playing with a depth-first algorithm to create the maze, a winding number algorithm to keep the maze inside the right space and drawing lines at 30 degrees in SVG.

But that wasn’t enough, if we can draw one maze, then why not draw 1,000 mazes, so every card can have a unique solvable maze.  Everyone’s EndGame in life is different, so this seemed like a good idea.  To quote our printer, “that is out there and very different haha “awesome” and no -one has ever done this in my 20 + years of printing so your a first … ”.

That’s it.  If we’re the first, then there’s no going back.


Follow

Get every new post delivered to your Inbox.