rweng
rweng

Reputation: 7230

web dependencies for play asset pipeline

I am coming from the Rails world and are now playing with play (2.1). I like that it comes with a kind of asset pipeline, but one thing I am missing is to include web-dependencies there. In Rails, I can add the jquery-rails dependency and include it in my application.js file to make it available.

I stumbled upon http://webjars.org which seems nice, but as far as I can tell I can only include the files directly in a html file.

Is there any way to enable this kind of behavior, or is it planed to be implemented in the near future?

Upvotes: 1

Views: 864

Answers (1)

Marius Soutier
Marius Soutier

Reputation: 11274

It's not supported in the same way as in Rails (apparently), so the best I've come up with is the following:

  1. Include your WebJars dependencies
  2. Define the WebJars route as documented here
  3. Put your JavaScript file in /app/assets/javascripts
  4. Use RequireJS (as documented here) and the WebJars RequireJS loader to include the WebJars dependency

So, for example, let's say you want to use AngularJS.

Build.scala:

val appDependencies = Seq(
  "org.webjars" % "webjars-play" % "2.1.0-2",
  "org.webjars" % "angularjs" % "1.0.7"
)

conf/routes

GET     /webjars/*file      controllers.WebJarAssets.at(file)

app/assets/javascripts/main.js

require(["webjars!angular.js"], function() {
  console.log(angular.isString(3));
}

app/views/example.scala.html

...
@helper.requireJs(core = routes.Assets.at("javascripts/require.js").url, module = routes.Assets.at("javascripts/main").url)
...

Now the JavaScript console prints "false".

Upvotes: 3

Related Questions