HTML5 boilerplate has a really nice build script. It get’s your website in tip top and minified shape in no time. However what happens when you want to use require.js on a big project and don’t need the scripts.js file that is provided? In a few steps we can get your website in tip top shape with require.js as well!
Getting started
For the sake of simplicity we will assume you already have h5bp in your project. Now that is settled we can get started.
First off we are going to need a way to differentiate our require.js stuff from the rest of our site. H5bp already has a good mechanism for doing this with external libraries via the libs & mylibs folders. From what I can gather libs & mylibs are where you stick generic libraries that the project may need. (jQuery, Backbone, etc)
For our require.js code we are going to create a separate folder called “app”. This way we can isolate those files from the h5bp build script. By default h5bp will copy & minify everything from the js folder when executed. This not what we want since we need to trace the require.js files for dependancies first. Your default js folder structure should be like below.

Cleaning up after scripts.js
Since we are no longer using the default scripts.js file we will need to modify the build script accordingly. Again, by default h5bp will try to run the -js.scripts.concat target when building. This will fail the checksum since we don’t have a scripts.js file. To remedy this problem remove any references to the ‘-js.scripts.concat’ target in the build targets. Doesn’t make sense? See below.

And… So close. One last modification is needed. When building, the ‘-usemin’ target is ran before any other minification/concat targets. Currently, it has a dependency on the ‘-js.scripts.contact’ target. We will need to get rid of this. However, this would be a good spot to trace our require module dependancies since it will run before any copying, minifying, etc. Go ahead and change the depends attribute to “-r.js,-css”. In the next section we will create the -r.js target.

Creating the -r.js target
<!-- Require.js target, run separately --><target name="-r.js" description="Concatenates JS files using require.js dependencies"> <java classname="org.mozilla.javascript.tools.shell.Main"> <classpath> <pathelement location="./${dir.build.tools}/${tool.rhino}" /> </classpath> <arg value="tools/${tool.require}" /> <arg value="-o" /> <arg value="${require.build}" /> </java> <echo message="optimized ${require.build}"></target>Let’s break this down a bit. First, it invokes Java. Next, it adds rhino to the java classpath. Finally, it sets three arguments.
If you notice there are three variables within the target. tool.rhino, tool.require, and require.build. The convention when defining variables within h5bp is to define them in the project.properties file. These variables override the default.properties file with your project specific variables. This is a good thing since if you update html5 boilerplate it won’t break your already defined variables with a later build. But what are these variables? The are simply string variables defined in our project.properties file.
${tool.rhino}
tool.rhino is defined in many places within the h5bp build.xml file. It is the string variable representing the rhino jar file.
tool.rhino = rhino.jar
${tool.require}
tool.require is a custom variable which represents the path to the r.js file that comes with the require.js optimizer. r.js will trace your javascript files written in require.js AMD format for dependencies, then concatenate them together and finally minify them with uglify. You will need to download this file and drop it in the tools directory within the build folder.
tool.require = r.js
${require.build}
require.build is another custom variable that links to our require.js build file. As described above r.js will preform a build on your javascript files. However, it needs to know how and where to build from. This is where a custom build file comes in. Require.js can build your site based on this custom build file which is represented in json format. In the example below we have set the require.build variable to point to a file name app.build.js. This file contains the instructions on how to build our require.js modules.
require.build = app.build.js
app.build.js
To get the full rundown of what can be done in a require build file visit http://requirejs.org/docs/optimization.html
A simple example of a build file is below. In this example we define our base url in the app folder, then we define the name of our module we want to traverse and finally, the location where we want to publish out the generated file(s).
Note: we are publishing out to the intermediate folder created by the h5bp build script.
({
baseUrl: "../src/www/js/app",
name: "main",
out: "../intermediate/js/app/main.js"
})
Putting it all together
Now that we have defined the variables above the -r.js target should make a bit more sense. Again, all it is doing is reading our require build file and tracing the module dependancies. It does this using the rhino shell to execute the r.js optimizer script that comes with require.js.
${dir.js.app}
Almost done. I promise! We will need to exclude our app folder from any processes that might get run by h5bp internally. We can do this in three steps.
- Create a custom variable to our app path in project.properties.
dir.js.app = ${dir.js}/app - Add ${dir.js.app} to the list of files/folders to ignore in project.properties.
file.exclude = ${dir.js.app}/** - Add an exclude statement to the -js.all.minify target. This will prevent h5bp from minifiying and copying the app folder. See below.

Conclusion
Ok, maybe that was more complicated that I thought! Anywhooo, if you had trouble following along I have attached the modified build folder below. Hopefully this helps development teams that are looking to incorporate require.js into their current build processes. Please feel free to post suggestions or enhancements.
