0

I've recently created a JavaScript library using Google's Closure Compiler: https://github.com/bvaughn/task-runner

I intend for this library to be used by applications that also require the full Closure Library, and so I want the built version of my library (Task Runner) not to include a subset of Closure Library. If Task Runner includes a subset of Closure Library, and another application includes the full Closure Library, a race condition will exist between which loads Closure last (last in wins). It also bloats the size of the Task Runner file.

However I'm having trouble. If I don't require any of the Closure library classes, Task Runner builds fine (obviously). However if I require something (goog.dom for instance) then my "compiled" JavaScript file also includes a portion of the Closure library. Is there a way to tell the Closure Compiler to leave certain JavaScript files/modules/whatever out of the built result?

FWIW, the Plovr experimental-exclude-closure-library option seems to somewhat describe the functionality I'm looking for.

bvaughn
  • 13,300
  • 45
  • 46
  • Uh, if you put a ! in front of a filename (or wildcard) it will not include it when you compile. Or you could just not have them in the dir you are grabbing files from. – Matt Evanoff Feb 01 '15 at 01:45
  • True, but then the compiler will error. I want to be able to reference Closure Library classes (and have the compiler verify things like type-safety, etc.) but I don't want any of the Closure Library to be included in the built version of my library. It seems to be an either/or thing. If I use the ! to exclude the classes, the project won't build. – bvaughn Feb 01 '15 at 02:07
  • https://developers.google.com/closure/compiler/docs/api-tutorial3 – Matt Evanoff Feb 01 '15 at 02:15
  • Yeah... I mean, I obviously read through the documentation before posting this, but thanks... – bvaughn Feb 01 '15 at 04:09
  • 1
    I'm a little confused. Your code depends on some `goog.dom` function but you don't want the output to include that function? How will the code actually work then? – Tyler Feb 02 '15 at 02:11

1 Answers1

1

On the surface what you are asking makes no sense. You want to depend on/use code from Closure-library, but not include it in your output. This isn't really possible nor how the library and compiler function together.

There is a rather small list of primitive functions defined in Closure-library that are completely removed/replaced when compiled with Closure-compiler. goog.require and goog.provide are the two most prominent of those.

For the vast majority of the Closure-Library, if you use or depend on a class, method or object that specific code will appear in the compiled output. And because that library code itself may depend on other parts of the library, even more code may be included in the compiled result.

The difference between using Closure-compiler with Closure-library as compared to more traditional JavaScript libraries is that ONLY the parts of the code determined to be called and used are included in the output. This is much more granular than a simple file inclusion - prototypes, variables, constants, etc will all be excluded because the compiler can determine that they are never used.

Distributing a Library

If you are building a library which depends on Closure-library, you have two options.

  1. Distribute a compiled/built version
    You would compile your library using Closure-library; exporting any public API methods and properties. Others who utilize your library with Closure-compiler or Closure-library will need to use an externs file and include your library AFTER compilation.

  2. Distribute your library as source
    You would simply distribute your library source code. Others would goog.require your library as part of their source and build process. If Closure-library is used in both projects, it will only be included once in the build process.

There is no hybrid approach where you compile your code but exclude Closure-library. This violates the principle that all of the source code will be compiled simultaneously.

You can peruse my Geolocation-marker library to see an example. I provide a compiled standalone version of the code for use, but the uncompiled source can also be included in other projects which use Closure-library.

Chad Killingsworth
  • 14,360
  • 2
  • 34
  • 57
  • Thank you for your detailed answer. I believe I did a poor job explaining my question. I'm intending my library (TR) to be used by another application that ALSO uses the full Closure Library (CL). If my library includes a subset of CL, then things break. So I'd like TR to have a runtime dependency on CL- but not to actually include any of CL. Does this make any more sense? – bvaughn Feb 03 '15 at 03:49
  • @brianvaughn I've updated the answer to reflect your clarification. – Chad Killingsworth Feb 03 '15 at 15:06
  • Thanks for updating. That's an approach I hadn't considered, but it seems reasonable. I'm used to working with JavaScript libraries that have been concat'ed and minified prior to distribution. – bvaughn Feb 03 '15 at 16:00