[play-2.x] How do you inline or combine multiple Javascript files?

273 views
Skip to first unread message

Niklas Nylund

unread,
Jul 3, 2013, 3:03:33 PM7/3/13
to play-fr...@googlegroups.com
My goal is to reduce the amount of requests for a page, is there an easy way to inline javascript files in the template or combine multiple files into one? 


Niklas

virtualeyes

unread,
Jul 3, 2013, 4:57:04 PM7/3/13
to play-fr...@googlegroups.com
Grunt JS does the trick here, but that's a 3rd party solution.

Not sure if Play's built-in assets generators can combine files (LESS yes, Coffeescript No)

Guillaume Bort

unread,
Jul 4, 2013, 3:20:45 AM7/4/13
to play-fr...@googlegroups.com
There is built-in RequireJS support.


--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--

Niklas Nylund

unread,
Jul 4, 2013, 3:25:31 AM7/4/13
to play-fr...@googlegroups.com
I am aware of RequireJS but was under the impression that this will require some kind of rewrite of my javascript code to use AMD. Is there a way to use it only for this purpose?

We are already using dojo for quite a lot of our own code but it would be nice to combine the external javascript files we use here and there to a single package. I know I could do this with a bunch of command line tools, Grunt JS etc, but preferably I would like to have everything with the same toolchain.



Niklas

Guillaume Bort

unread,
Jul 4, 2013, 12:43:35 PM7/4/13
to play-fr...@googlegroups.com
It's difficult to concatenate several javascript files without taking care of scoping. The only thing you have to do to use require js, is to export the member you want to expose:

define(function() {

// your legacy javascript here

return {
  xxx: aMemberYouWantToExport
}

})

Niklas Nylund

unread,
Jul 5, 2013, 3:56:07 AM7/5/13
to play-fr...@googlegroups.com
Thanks for the pointer, I will look into this.

Niklas

Marius Soutier

unread,
Jul 5, 2013, 4:20:01 AM7/5/13
to play-fr...@googlegroups.com
You can also wrap non-AMD libraries like this:

requirejs.config({
paths: {
"your.module" : "./path/to/module" // omit .js
}
});
Reply all
Reply to author
Forward
0 new messages