Friday, November 6, 2009

Calling the Closure Compiler from Java

Someone asked how to deal with Compiler input and output as strings on the closure-compiler-discuss Google group, so here is a code snippet that should help out:

  /**
* @param code JavaScript source code to compile.
* @return The compiled version of the code.
*/
public static String compile(String code) {
Compiler compiler = new Compiler();

CompilerOptions options = new CompilerOptions();
// Advanced mode is used here, but additional options could be set, too.
CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(
options);

// To get the complete set of externs, the logic in
// CompilerRunner.getDefaultExterns() should be used here.
JSSourceFile extern = JSSourceFile.fromCode("externs.js",
"function alert(x) {}");

// The dummy input name "input.js" is used here so that any warnings or
// errors will cite line numbers in terms of input.js.
JSSourceFile input = JSSourceFile.fromCode("input.js", code);

// compile() returns a Result, but it is not needed here.
compiler.compile(extern, input, options);

// The compiler is responsible for generating the compiled code; it is not
// accessible via the Result.
return compiler.toSource();
}

This is packaged in a runnable example in CallCompilerProgrammaticallyExample.java.

Note that the recommended way of extending compiler behavior is to subclass CompilerRunner (most likely createOptions() will be the method you are interested in overriding). Your subclass should have a main() method and be set as the Main-Class in a jar. Such a jar can then be used with the -c option in the calcdeps.py utility.

26 comments:

  1. Thanks a lot!
    This saved me 2 hours.

    ReplyDelete
  2. Can you give me a hint/help to how can i also, generate the SourceMap from java??? I cant seem to find anyway, other than from the commandLine!!

    Thanks

    ReplyDelete

  3. JavaScript is quite possibly the most utilized programming dialects on the planet that makes the page intelligent by having various systems and projects to play out a specific errand. Aurelia, ReactJS, EmberJS, NodeJS, among different systems stay the main apparatuses for building web applications. As per w3techs.com information, 95% of all destinations on the web were created with the utilization of JavaScript. With such an interest for this language, there are various offshore JavaScript developers to help you fabricate your group. "Recruit frontend designer Kyiv" is a significant basic solicitation on the web as Ukraine stays an alluring objective for West European nations for offshoring their devoted development groups. Web parts are significantly more modest in measure and don't need extra runtime as they are fundamentally program based. Web segments work on the vast majority of JS systems subsequently empower engineers of various mastery to create web parts. Another requesting system that shows its sensational interest organizations. Planned by Facebook, this structure is focused on worker side runtime for APIs. The structure recognizes the sorts and fields of customer's cooperations and prerequisites, and afterward gives specific capacities to those. ES-modules are remembered for the norm of ES6 that portrays crafted by modules inside the program. This innovation, subsequently, empowers designers to add and concentrate usefulness to a specific module which can be additionally downloaded through CDN.

    ReplyDelete
  4. IoT-consulting helps companies to improve, because there is an individual approach to each and takes into account the wishes that often differ. Consulting firms on the Internet of Things can offer not only collection but also analysis, data transfer. Our company from Ukraine offers high-quality specialists who make a great contribution to the development of any company.

    ReplyDelete
  5. Hello! Thank you so much for the information in this article about "Calling the Closure Compiler from Java". But I wanna say a few words about studying! I with one of my classmates are studying at the same college. In order to make his life so much happier and easier, I recommended him this writing company's https://rankmywriter.com/trustmypaper-com-review website with examples of correct essays that can be used to do his own masterpiece. Maybe, you will need to take this help in the nearest future!!!

    ReplyDelete
  6. i study a lot of things and I discovered that the signifies of crafting to clearifing that just want to say was Exceptional so i am amazed and ilike to return all over again in long-term..สล็อตออนไลน์

    ReplyDelete
  7. It proved to become Extremely handy to me and I am certain to the many commentators right in this article!สล็อต 999

    ReplyDelete
  8. pretty interesting write-up.That is definitely my originally time Look into in this post.I found so mmany interesting stuff in your Web page In particular its dialogue..a lot of many thanks to your article!สล็อตวอเลท

    ReplyDelete
  9. I like how you compose and share your spot of desire! Extremely interesting and various! Maintain it coming!สล็อตแตกง่าย

    ReplyDelete
  10. Great day, I've browsed most within your posts. This write-up is almost certainly wherein I obtained by far one of the most useful details for my exploration. Lots of thanks for Placing up, Most likely we can certainly see added on this. Will you be aware of any other Web-web pages on this subject material.บา คา ร่า วอ เลท

    ReplyDelete
  11. This actually is this sort of a great supply that you are furnishing as well as you give it absent completely totally free. I love observing site that realize the worth of furnishing a high quality useful resource fully free of charge.บา คา ร่า วอ เลท

    ReplyDelete
  12. That is quite awesome to browse..instructional post is superb to look at..quite a few many thanks a great deal!บา คา ร่า วอ เลท

    ReplyDelete
  13. pleasant weblog! its intriguing. thanks for sharing....บาคาร่าวอเลท

    ReplyDelete
  14. I'm ready to see that you are an authority at your self-discipline! I'm launching a Website shortly, and your info and info might be unbelievably helpful for me.. Thanks in your guidance and wishing you the many accomplishment in your online business.สล็อตทรูวอเลท

    ReplyDelete
  15. Excellent make-up, I am a massive believer in commenting on weblogs to tell the location writers know which they’ve extra some point worthwhile in your Internet!..สล็อตแตกง่าย

    ReplyDelete
  16. I havent any term to appreciate this set up.....Seriously i am astonished from this publish....the a single who make this publish it Totally was an incredible human..several thanks for shared this with us.สล็อตxo

    ReplyDelete
  17. Remarkable submit. I Are by now eager to understand about this problem, so thanks for putting up. Relatively fantastic put up.It 's significantly exceptionally enjoyable and Useful publish.Several thanksเว็บสล็อต

    ReplyDelete
  18. I’ve been trying to find some decent things on The subject and also have not experienced any luck up until this stage, You simply bought a new most important admirer!..เว็บสล็อตเว็บตรง

    ReplyDelete
  19. it were an incredible chance to look into this kind of Web content And that i am delighted to understand. many thanks Significantly for supplying us an opportunity to own this chance..เว็บตรงสล็อต

    ReplyDelete
  20. Your weblog provided us with useful aspects to work with. Just about every & Each individual person ideas of 1's compose-up are great. Many thanks a good deal for sharing. Sustain operating a web site..เว็บ ตรง

    ReplyDelete
  21. Thanks for this kind of appropriately composed report. It’s stuffed with insightful particulars and entertaining descriptions. Your viewpoint is the best among the A good number of.เกมสล็อต

    ReplyDelete
  22. Thanks for yet another wonderful report. Just in which else could Anybody get that sort of knowledge in these kinds of an excellent means of crafting? I've a presentation upcoming week, And that i'm in excess of the test to find these kind of information.สล็อต เว็บ ตรง

    ReplyDelete
  23. เพราะเว็บของ pg betflix มีความทันสมัย ดูแลผู้เล่นทุกท่านไม่ว่าจะเป็นผู้เล่นใหม่ หรือ ผู้เล่นเก่า อย่างทั่วถึงไม่ รวมเว็บ betflix สมัครสมาชิกกับเราเพียงไม่กี่นาที ผู้เล่นสามารถที่จะทดลองเล่นได้ฟรี

    ReplyDelete
  24. สำหรับผู้ที่ชื่นชอบภาพยนตร์ และผู้ชมทุกท่าน ที่ชื่นชอบ ดูหนังออนไล ซีรีย์ที่ชื่นชอบในการรับชมภาพยนตร์ ซีรีย์ ดูซีรีย์ออนไลน์ อัพเดทใหม่ตลอดทั้งเดือน

    ReplyDelete