Anyone used GWT with Require.js?

823 views
Skip to first unread message

walker1c

unread,
Aug 14, 2012, 4:55:22 AM8/14/12
to google-we...@googlegroups.com
Hi,

The project I'm working on is seeing a lot of native Javascript creeping in, largely as a result of the requirement to incorporate UI features developed by other teams that specialise in Javascript.  I am hoping to manage the Javascript dependencies using require.js (http://requirejs.org/).  Correctly used, require.js will remove the problem where a script loads, executes, and fails because of a reference to another script that hasn't finished loading yet.

But I still have problems when JSNI code calls into a Javascript library before it's finished loading.  The solution would appear to be to use require.js to load the nocache script that boots the application, so that it won't start executing until all its dependencies have loaded.

Does anyone know if this is possible?  Any advice?

Thanks,

Chris

Joseph Lust

unread,
Aug 14, 2012, 10:18:26 AM8/14/12
to google-we...@googlegroups.com
Chris,

But I still have problems when JSNI code calls into a Javascript library before it's finished loading.  The solution would appear to be to use require.js to load the nocache script that boots the application, so that it won't start executing until all its dependencies have loaded.

Have you considered adding some JSNI methods published to the DOM so that your require.js code can call into your GWT code and start processes as needed, once it has loaded the needed resources?


Sincerely,
Joseph

walker1c

unread,
Aug 15, 2012, 5:10:35 AM8/15/12
to google-we...@googlegroups.com
Hi Joseph,

I think what you suggest may be the solution. I could transplant the code from onModuleLoad() to a method that's invoked from one of the scripts loaded by Requirejs.  

It's slightly awkward because onModuleLoad() is an instance method, so I need to expose the EntryPoint object in a way that makes it accessible for native scripts. This means that there would still be a race between the native scripts and the GWT application - if the native script executes before the EntryPoint has initialised, there won't be an instance available to call the JSNI method on.  But I expect this could be handled by window timeouts until everything is ready.

Clearly this needs more work.  If I finc a working solution, I'll post it here.

Chris

Joseph Lust

unread,
Nov 21, 2012, 5:20:24 PM11/21/12
to google-we...@googlegroups.com
Oh the irony. I had to "componentize" a widget created by another team which uses RequireJs today and the first Google hit is my own answer. Given that I wanted to just make a GWT module that could be inherited to use an external widget, said module would not have an entry point and onModuleLoad method. Accordingly, I found the simplest way to achieve this was just to remove the RequireJs calls and put all the needed scripts in the module.gwt.xml as <script> tags.

Not pretty, but then again, they should have gone with GWT or combined their dependencies into a single .js as RequireJs allows.


Sincerely,
Joseph


Joseph Lust

unread,
Nov 30, 2012, 10:48:32 AM11/30/12
to google-we...@googlegroups.com
An update here. The external team writing in RequireJS was not so amicable about sending RequireJS packing into the night.

So, we did 2 things:
  • Had them use the feature in RequireJS to compile to a single file with their Optimizer.
  • Had them expose the needed method in the global name space (window obj) so we can call them from GWT.

I hope this helps anyone else facing the same issue.

Sincerely,
Joseph
Reply all
Reply to author
Forward
0 new messages