9

I'm using a jQuery plug-in and Google Closure Compiler. The problem is that there's a bug in the plug-in when I add the URL of that plug-in into the compilation, the compilation fails. So I want to create an extern for that plug-in. Basically, I'm only using 1 object and 2 methods from the entire library; something like this:

var TheObject = $.plugin({...});

var SomeVar = TheObject.someName.otherName(SomeString, {

      prop1: [...],

      onError: function () {...}

});

TheObject.SomeMethod();

I looked at the doc on Google's website but it's written from a confusing "what-it-is" perspective and I need a simple "how-to" perspective on how to do this. What do I need to do to create an extern for what I have?

Thanks.

frenchie
  • 51,731
  • 109
  • 304
  • 510

2 Answers2

11

Here you go:

I haven't had time to finish out the series on creating externs. If this isn't enough for your project, I'll revisit the topic.

Chad Killingsworth
  • 14,360
  • 2
  • 34
  • 57
  • Thanks Chad, you're definitely the go-to person for closure compiler!! I'll get on this part of the project in 1-2 days and let you know how this goes. – frenchie Jan 14 '14 at 20:01
  • @Chad, Is there a way to run the compiler or some other tool to generate an extern file from an existing full fledge script? – Alexis Wilke Apr 08 '14 at 03:20
  • @AlexisWilke [This FAQ](https://github.com/google/closure-compiler/wiki/FAQ#how-do-i-write-an-externs-file) references the tool [introduced here](http://blog.dotnetwise.com/2009/11/closure-compiler-externs-extractor.html) and [exposed here](http://www.dotnetwise.com/Code/Externs/index.html) (to generate an extern file). – ChrisW Apr 22 '17 at 22:12
2

So I've been struggling on and off with this question for a while and I've come up with a working solution for others who have a plug-in they want to use in their code with closure compiler: instead of doing an extern, just use strings, like this:

var TheObject = $['plugin']({...});

var SomeVar = TheObject['someName']['otherName'](SomeString, {

      'prop1': [...],

      'onError': function () {...}  
});

TheObject['SomeMethod']();

That may not work for everybody but this worked for me and saved me a great deal of hassle in terms of writing an extern. I found the doc online to be very confusing: either written by techies who explain what things are, but now how to use them, or written in a professorial tone, with not many empirical examples. I hope this answer will help others.

frenchie
  • 51,731
  • 109
  • 304
  • 510
  • The array-style notation will get it to compile, but it won't reduce down to the same extent that using an externs file will. Making an externs file isn't that difficult. If you want examples, check out [some of the externs included in the project](https://code.google.com/p/closure-compiler/source/browse/#git%2Fcontrib%2Fexterns%253Fstate%253Dclosed). – Kyle Falconer Feb 27 '14 at 15:23
  • @netinept: I understand what you mean but the approach I took got it working. Since there are essentially only 3 lines of code, can you show, with code, how to write an extern for this sample plugin? – frenchie Feb 27 '14 at 17:12
  • @frenchie I've come across this problem you had in 2014, was wondering if this is still the best solution or if you have found something better? Thanks. – davidtgq Oct 20 '16 at 05:06
  • This is still what I use; nothing better. – frenchie Oct 20 '16 at 09:21