Play 2.3 Webjars - default requirejs support - How to write define or require

597 Views Asked by At

I am trying to get requirejs and webjars working, but they do not seem to work in play 2.3.7

routes:

GET      /vassets/*file     controllers.Assets.versioned(path="/public", file)
GET      /webjars/*file     controllers.WebJarAssets.at(file)

build.sbt

  "org.webjars" %% "webjars-play" % "2.3.0-2",
    "org.webjars" % "jquery" % "2.1.3"

html template

<script>
    @Html(org.webjars.RequireJS.getSetupJavaScript(routes.WebJarAssets.at("").url))
</script>
<script data-main="@routes.Assets.versioned("js/koapp/js_init")"
  src="@routes.WebJarAssets.at(WebJarAssets.locate("require.min.js"))"></script>
</head>

js_init.js (require.js data-main)

function test(){
  return console.log('test');
}
require("jquery",test);

I am getting the error "Uncaught TypeError: object is not a function"

Also, I tried using define inside the main js. It is not able to locate the dependencies then, it is looking for libs in the same folder as main js.

   define([ 'knockout'],
        function(ko) {
  console.log('hello world')       
});

is requirejs + webjars integration completely broken or am i doing something nonsense? please help

1

There are 1 best solutions below

4
On BEST ANSWER

The WebJars Play2 Activator Template has all of this stuff working in it. So you should compare what you have with what is in there.

I think that data-main="@routes.Assets.versioned("js/koapp/js_init")" needs to include the .js extension, like: data-main="@routes.Assets.versioned("js/koapp/js_init.js")".

Also, the /vassets route should be:

GET        /vassets/*file    controllers.Assets.versioned(path="/public", file: Asset)

Notice the Asset type on the file param.