Paul Smith
Paul Smith

Reputation: 91

Play Framework 2 & requirejs - paths not respected in dist build

I am having some trouble with the requirejs implementation in Play 2.0, where in dev mode all modules can be found, but when running dist it seems to be not respecting the paths I have setup.

Here is the setup:

/assets/javascripts/templates/template1/main.js:

require.config({
   baseUrl: "/assets/javascripts",
   paths : {
           jquery : [ 'core/lib/jquery/jquery-1.8.3' ],
           can : [ 'core/lib/canjs/can' ]
   }
});

require([ "jquery", "can", "core/global/moduleloader" ], function($, can, ml) {
   //do stuff

});

And in the template I am calling:

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

in my build.scala I am telling it which files to optimize like so:

val main = play.Project(appName, appVersion, appDependencies).settings(
    requireJs += "templates/template1/main"
)

Client side all dependencies are resolved, but when using dist to optimize, I get:

[info] RequireJS optimization has begun...
[info] app.build.js:
[info] ({appDir: "javascripts",
[info]           baseUrl: ".",
[info]           dir:"javascripts-min",
[info]           modules: [{name: "templates/template1/main"}]})
model contains 41 documentable templates

Tracing dependencies for: templates/template1/main
JavaException: java.io.FileNotFoundException:       /Users/paulsmith/Projects/Experiments/play/Moduluar/target/scala-2.10/classes/public/javascripts-min/jquery.js (No such file or directory)
In module tree:
    templates/template1/main

From what I can see, the paths config is being ignored and so it is resolving the paths incorrectly.. this seems to be due to the app.build.js overriding the config in the main.js.

Has anyone come across this issue before?

Thanks,

Paul

Upvotes: 7

Views: 2775

Answers (1)

Tommy Adamski
Tommy Adamski

Reputation: 577

I was having the identical issue, and adding the requireJsShim key to my Build.scala fixed the issue:

val main = play.Project(appName, appVersion, appDependencies).settings( 
    requireJs += "main.js",
    requireJsShim += "main.js"
}

requireJsShim tells play to use the settings in your main.js, such as paths and shims, instead of the default values in plays app.build.js

I'm using Play 2.1.0; this feature wasn't added until December 12, 2012, so I'm not sure which Release Candidates of 2.1 it's included with

References:
https://play.lighthouseapp.com/projects/82401-play-20/tickets/945-allow-specifying-your-own-requirejs-build-file#ticket-945-4
https://github.com/playframework/Play20/commit/ba71f3967c3001cc0db8a4a7b4f9a31c8eebbc45

Upvotes: 8

Related Questions