Documentation

You are viewing the documentation for the 2.3.5 release in the 2.3.x series of releases. The latest stable release series is 3.0.x.

§RequireJS

According to RequireJS’ website

RequireJS is a JavaScript file and module loader. It is optimized for in-browser use, but it can be used in other JavaScript environments… Using a modular script loader like RequireJS will improve the speed and quality of your code.

What this means in practice is that one can use RequireJS to modularize your JavaScript. RequireJS achieves this by implementing a semi-standard API called Asynchronous Module Definition (other similar ideas include CommonJS ). Using AMD makes it is possible to resolve and load javascript modules on the client side while allowing server side optimization. For server side optimization module dependencies may be minified and combined using UglifyJS 2.

By convention RequireJS expects a main.js file to bootstrap its module loader.

§Deployment

The RequireJS optimizer shouldn’t generally kick-in until it is time to perform a deployment i.e. by running the start, stage or dist tasks.

If you’re using WebJars with your build then the RequireJS optimizer plugin will also ensure that any JavaScript resources referenced from within a WebJar are automatically referenced from the jsdelivr CDN. In addition if any .min.js file is found then that will be used in place of .js. An added bonus here is that there is no change required to your html!

§Enablement and Configuration

RequireJS optimization is enabled by simply adding the plugin to your plugins.sbt file when using the PlayJava or PlayScala plugins:

addSbtPlugin("com.typesafe.sbt" % "sbt-rjs" % "1.0.1")

To add the plugin to the asset pipeline you can declare it as follows (assuming just the one plugin for the pipeline - add others into the sequence such as digest and gzip as required):

pipelineStages := Seq(rjs)

A standard build profile for the RequireJS optimizer is provided and should suffice for most projects. However please refer to the plugin’s documentation for information on how it may be configured.

Note that RequireJS performs a lot of work and while it works when executed in-JVM under Trireme, you will be best to use Node.js as the js-engine from a performance perspective. For convenience you can set the sbt.jse.engineType property in SBT_OPTS. For example on Unix:

export SBT_OPTS="$SBT_OPTS -Dsbt.jse.engineType=Node"

Please refer to the plugin’s documentation for information on how it may be configured.