4

I wrote an GWT library for a javascript visualization library (dygraphs).
It's a simple wrapper using JavascriptObject and JSNI to call into the dygraphs code.
So far I have included the dygraphs.js script in my GWT's module xml file and it worked just fine.

However when I recently tried to use SuperDevMode I had to switch to the xsiframe linker which doesn't allow script tags.
What is the best practice for including external javascript scripts in a GWT library with the cross site linker?

The best thing I could come up with was to include the Javascript file as a TextResource in a ClientBundle and then use ScriptInjector to include it?

Is that the recommended approach or is there any better way?

I have to make sure that the dygraphs.js is fully loaded before my app that is using my GWT wrapper is going to access it.

1 Answer 1

1

Including your external javascript as a TextResource and injecting it yourself is a very good way making sure its loaded with your app. (and you are saving the extra http request while benefitting from GWT cache mechanism)

If you are running on thin client you may want to separate the actual injection in the DOM from parsing, which you could easily do if you inject the textresource with a comment from top to bottom and then later on remove it.

Sign up to request clarification or add additional context in comments.

5 Comments

Even better would be to put it into JSNI to allow the compiler to take a crack at it, performing simple renaming and inlining. Only catch here is if the library uses with - GWT JSNI doesn't support with.
only gwt 2.5 optimizes the generated javascript everything below 2.5 is not
Thanks for the feedback. I followed basically following approach (stackoverflow.com/questions/8744663/…) with the exception that I use an inlined javascript script instead of asynchronous request.
Only 2.5 is able to invoke the Closure compiler, but com.google.gwt.dev.jjs.JavaToJavaScriptCompiler.optimizeJs(JJSOptions, JsProgram) does take a crack at the JS AST in 2.4 and some earlier versions. Only simple things - rewrite if statements to ternary, intern strings, etc, but it does still run.
@DanielKurka check out gist.github.com/3840254 as a really quick example of a few pre-closure optimizations

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.