Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play framework JS concatenation

I'm using require.js in play framework which is great. After tweaking Build.scala file I'm getting minified js files on production but I'm not able to find any information about concatenation all the require.js modules into single js file?

I'll appreciate if anyone can shed any light or guide me in correct direction.

like image 824
RuntimeException Avatar asked Oct 04 '22 02:10

RuntimeException


2 Answers

Do you know "webjars" ( http://www.webjars.org)? Webjars is a framework which provides you with Javascript and CSS libraries/frameworks in jar files. You can add them as dependencies to you build file. They also provide some helper-function, one of which "compiles" (concatenate and minify) the requirejs-stuff into one file.

Have a look at the documentation: http://www.webjars.org/documentation They provide a few simple examples.

like image 78
Peanut Avatar answered Oct 13 '22 11:10

Peanut


I ran into a few 'gotchas' regarding the requireJS integration in Play, in 2.2.x.

a) Do not change your routes file assets location. Play's convention seems to be all public javascripts go into public/javascripts and your requireJS driver goes into app/assets/javascripts.

b) The build.sbt requireJS lines go at the bottom, beneath the "playJavaSettings" or "playScalaSettings" as such:

name := "TestContcatJS"

version := "1.0-SNAPSHOT"

libraryDependencies ++= Seq(
  javaJdbc,
  javaEbean,
  cache
)     

play.Project.playJavaSettings

requireJs += "main.js"

requireJsShim += "main.js"

c) You can reference the public javascripts in your driver using a relative path. Something like (in main.js):

require.config({
    paths: {
        'public-js': '../../public/javascripts'
    }
});

require([
   "public-js/foo",
   "public-js/bar"
]);
like image 37
Justin N Avatar answered Oct 13 '22 11:10

Justin N