Your browser doesn't support the features required by impress.js, so you are presented with a simplified version of this presentation.

For the best experience please use the latest Chrome, Safari or Firefox browser.

Automating Website Optimizations


{
  "author": "Mickael Daniel",
  "github": "http://github.com/mklabs",
  "twitter": "@mklabs",
  "date": "new Date('Mon Apr 10 2012 19:00:00 GMT+0200')"
  "dependencies": {
    "your-attention": "1.0.0"
  }
}

Why use a build tool

lots of build tool choices

Ant

<target name="compile" depends="init"
        description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${src}" destdir="${build}"/>
  </target>

  <target name="dist" depends="compile"
        description="generate the distribution" >
    <!-- Create the distribution directory -->
    <mkdir dir="${dist}/lib"/>

...

Ant

Cake / CoffeeScript

Cake

fs = require "fs"

option "-o", "--output [DIR]", "directory for compiled code"

task "minify", "Minify the resulting application file after build", ->
  exec "java -jar "/home/stan/public/compiler.jar" --js lib/app.js --js_output_file lib/app.production.js", (err, stdout, stderr) ->
    throw err if err
    console.log stdout + stderr

Rake

Rake

Jake

desc("This is the default task.");
task("default", function (params) {
  console.log("This is the default task.");
});

Buildy

new Queue("release version")
    .task("files", ["./js/test1.js", "./js/test2.js"])
    .task("concat")
    .task("jsminify")
    .task("write", { name: "./build/test-min.js" })
    .run();

Grunt!

Grunt

module.exports = function(grunt) {

  // Create a new task.
  grunt.registerTask("awesome", "Print out "awesome!!!"", function() {
    var awesome = grunt.helper("awesome");
    grunt.log.write(awesome);
  });

  // Register a helper.
  grunt.registerHelper("awesome", function() {
    return "awesome!!!";
  });

};

easy optimizations

push it further

That's where things get interresting

cache busting with file transform

<link rel="stylesheets" href="file.css">

is renamed into

<link rel="stylesheets" href="e207ea1.file.css">

cache busting

3 lines of node

var fs = require("fs"),
  crypto = require("crypto");

var hash = crypto.createHash("md5");
hash.update(fs.readFileSync("./path/to/file.css"));
console.log(hash.digest("hex"));
// >> e207ea1eeb616799f29d679e1fdc2415

remove debugging code

ifdefs like

// env=prod
// dont try this at home!
while(true) console.log("log all day long!");
// end:prod

That's just the easy stuff

prefix relative URLs with CDN

<link rel="stylesheets" href="../4b6cd983.file.css">

to

<link rel="stylesheets" href="https://a248.e.akamai.net/youraccount/4b6cd983.file.css">

running your own cdn

is easy now

Replace imgs with datauri

CSSEmbed

Jammit

next h5bp/node-build-script

CSS sprite generation

but datauris are better :p (fit more or less the same role)

use DataURIs

serve IE6 & 7 MHTML


serve IE6 & 7 MHTML

Going further

appcache manifest

Facts!

Basic to Aggressive

HTML Minification

With the fantastic html-minifier from kangax

Tools and web-build systems

Ant / Make / Rake / Jake / Cake / Grunt / Bash / Python

build tools are great


;