Using Node.js to Perform JavaScript Optimization for Play and Scala Applications
Last updated May 30, 2024
Table of Contents
Many sbt plugins perform operations requiring a JavaScript engine. For example, the sbt-rjs plugin performs RequireJs optimization, and the sbt-uglify plugin performs UglifyJS optimization. By default, these plugins use Trireme, which runs Javascript inside the JVM. The Trireme engine may be sufficient for small operations, but it’s performance can suffer when operating on large numbers of files.
To improve the performance of these plugins, it may be necessary to use a native Node.js engine. If you find that your build process hangs indefinitely during a JavaScript step, then you likely need to change engines. In this article, you’ll learn how to configure your project to install Node.js and use it to execute your JavaScript optimizations for a Play or Scala project.
Using multiple buildpacks
In order to use Node.js with Scala on Heroku, you must configure your project to use both the Scala buildpack and the Node.js buildpack. This is done with the heroku buildpacks
command.
First, add the Node.js and Scala buildpacks to your application by running this:
$ heroku buildpacks:clear
$ heroku buildpacks:add heroku/nodejs
$ heroku buildpacks:add heroku/scala
Then confirm the execution order by running this command:
$ heroku buildpacks
=== nameless-brushlands-4859 Buildpack
1. heroku/nodejs
2. heroku/scala
This ensures Heroku will run the Node.js buildpack first, and the Scala buildpack second.
In order for the Node.js buildpack to execute, your project must also have a package.json
file. In this file, you can define the version of Node.js to install. Create the package.json
file in the root of your project and put the following code in it:
{ "engines": { "node": "4.0.0" } }
You can also leave it empty (just {}
) to always pick up the latest version.
Then add this file to your Git repository by running the following commands:
$ git add package.json
$ git commit -m "Added package.json"
Finally, configure sbt to use the Node.js engine by setting SBT_OPTS thusly:
$ heroku config:set SBT_OPTS="-Dsbt.jse.engineType=Node"
Now you’re ready to deploy. Run the following command:
$ git push heroku master
[master 6d8842c] redeploy
Counting objects: 1, done.
Writing objects: 100% (1/1), 184 bytes | 0 bytes/s, done.
Total 1 (delta 0), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Fetching custom git buildpack... done
remote: -----> Node.js app detected
remote:
remote: -----> Reading application state
remote: package.json...
remote: build directory...
remote: cache directory...
remote: environment variables...
remote:
remote: Node engine: unspecified
remote: Npm engine: unspecified
remote: Start mechanism: Procfile
remote: node_modules source: package.json
remote: node_modules cached: false
...
remote: -----> Fetching custom git buildpack... done
remote: -----> Play 2.x - Scala app detected
remote: -----> Installing OpenJDK 1.8... done
remote: -----> Priming Ivy cache (Scala-2.11, Play-2.3)... done
remote: -----> Running: sbt update
...
remote: [info] Done packaging.
remote: [success] Total time: 4 s, completed Jan 29, 2015 2:51:10 PM
remote: -----> Dropping ivy cache from the slug
remote: -----> Dropping sbt boot dir from the slug
remote: -----> Dropping compilation artifacts from the slug
remote:
remote: -----> Discovering process types
remote: Procfile declares types -> web
remote:
remote: -----> Compressing... done, 98.8MB
remote: -----> Launching... done, v6
remote: https://play-node-test-1234567890ab.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/play-node-test.git
99109c2..6d8842c master -> master
As the deploy process executes, you will see that Heroku installs Node.js (which will generate some warning that you can ignore – these are due to the minimal package.json
file). When the sbt process executes it will use the Node.js engine.
For more information on configuring the sbt JavaScript engine, see the sbt-js-engine documentation. For more information on using Scala on Heroku, see the DevCenter documentation for Scala support.