Speeding up Require.js optimization in Maven
Require.js also provides an optimizer script which compiles and compresses these modules into one (or a few) files for efficient delivery to the end-user. Sadly, when running the optimizer as part of a Maven build this process can take several minutes to complete. In this article I’ll explain how to speed things up a bit.
The Rhino in the room
Using node.js in Maven
So after getting annoyed by this problem long enough for it to become a pet peeve, I decided to finally fix this. For me the ultimate solution consists of two steps:
- requirejs-maven-plugin should support running the optimizer using node.js
- node.js binary should be supplied to the build environment without using an external installation process
To implement step 1 I worked together with requirejs-maven-plugin’s creator Matthew Cheely to add node.js support. In the latest version it’s possible to supply a path to a node.js binary after which the plugin executes the optimizer using node.js.
In order to supply the node.js binary to the build environment I created a new plugin called nodejs-maven-plugin. This plugin extracts a platform-specific nodejs binary to a directory of your choosing. Currently supported platforms are Windows, Mac and Linux, all 32 and 64 bit versions.
Configuring these two plugins should look something like this:
This configuration instructs nodejs-maven-plugin to extract a node.js binary inside the target/nodejs directory of the Maven project. Requirejs-maven-plugin will then run Require.js’ optimizer script using the newly extracted node.js binary. This setup enables you to use node.js on on all your build environments having to install it manually.