Grails 3.0.0.M1 Asset-Pipeline Tips & Tricks

Posted by David Estes on Jan 29, 2015

Filed under Grails, Open Source, Groovy

As many of you may have seen. In the past few hours the pre-release version of grails 3.0.0.M1 was released. It features many great improvements such as moving to the gradle build tool, easier testing stack, better IDE support, and tons of other little nuggets.

Grails 3.0 is also the first official framework release running on the new AP 2.0 stack exclusively. As this is a pre-release expect improvements down the line but at the moment, you can already take advantage of the full suite of AP extensions that exist in mavenCentral. This grants the new grails platform with a 2-3x compile time improvement on static assets alone.

Using LESS

LESS is great when you want to compile your bootstrap code from source or just to use in place of css in general.

At the moment, in order to use less you must define both a buildscript dependency as well as a runtime dependency. This allows you to both use the less processor in debug / development mode as well as during war assembly for precompilation.

buildscript {
    ext {
      grailsVersion = project.grailsVersion
    }  
    repositories {
        mavenLocal()
        maven { url "http://repo.grails.org/grails/core" }
    }
    dependencies {
        classpath "org.grails:grails-gradle-plugin:$grailsVersion"
        classpath 'com.bertramlabs.plugins:asset-pipeline-gradle:2.1.1'
        classpath 'com.bertramlabs.plugins:less-asset-pipeline:2.0.8'
    }
}

dependencies {
  runtime 'com.bertramlabs.plugins:asset-pipeline-core:2.1.1' //Add this for some bug fixes
  runtime 'com.bertramlabs.plugins:less-asset-pipeline:2.0.8'
}

Thats pretty much all there is to it.

NOTE: You might notice I added a definition for a newer version of asset-pipeline-core as well as gradle. This release contains some css render fixes that might be useful down the line.

Now you can simply rename your application.css file to application.less and go to town. It is even possible to download Bootstrap 3.2.2 from source and use that in your runtime directly.

Coffeescript

You can also take advantage of coffeescript processing by adding the com.bertramlabs.plugins:coffee-asset-pipeline:2.0.6 file to your dependencies as well as your buildscript dependencies.

This plugin has some nice features such as being able to take advantage of the node coffeescript binary, if available, for faster processing times.

Handlebars

Just like the others, simply add the com.bertramlabs.plugins:handlebars-asset-pipeline:2.1.1 plugin to your project. At first you will have to keep your own copy of handlebars-runtime.js to render the resultant compiled .hbs files. However, in the next AP release for grails, the application will be capable of accessing the included copy of handlebars from its META-INF/assets folder.

Build Options

Grails takes advantage of using the already existant gradle plugin for asset-pipeline. This means we can use those config options directly in our build.gradle file to control how these files are compiled for production.

assets {
    minifyJs = true
    minifyCss = true
    excludes = ['**/*.less','bootstrap-3.3.2/**/*'] //Example Exclude GLOB pattern
    includes = ['application.less','bootstrap-3.3.2/fonts/*']
}

Above is an example config that excludes the raw source of bootstrap 3 from being compiled, while still including the base less file as well as the font files included with the package. You can also see that minification can be controlled here. You can also pass a configOptions map that can be ready by the asset-pipeline extension plugins. For example, in the case of LESS we may want to switch to the standard compiler instead of utilizing the much more performant less4j library. This can be done by adding the following:

assets {
    configOptions = [
      less: [
        compiler: 'standard' //or less4j
      ]
    ]
}

NOTE: At the moment, this configuration is not propagated down to the grails runtime. Therefore if you want your runtime to also use the standard compiler you must add this to your grails config application.yml

grails: 
  assets:
    less:
      compiler: standard

That should be all the differences you need to know. Go to grails.org right now and get the latest 3.0.0.M1 release to try out! You will not regret it. While not all AP plugins from grails 2.x are available, such as SASS (working on it I swear!). There are several plugins available out of the gate including some useful ones from @craigburke for use with angularjs.

Resources