如何分发一个使用Closure库的库,而不包含任何实际Closure库的源代码? [英] How to distribute a library which uses Closure-library without including any of the actual Closure-library source?

查看:139
本文介绍了如何分发一个使用Closure库的库,而不包含任何实际Closure库的源代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近使用Google的Closure Compiler创建了一个JavaScript库: https://github.com/bvaughn /task-runner

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

我打算让还需要完整的Closure库的应用程序使用该库,所以我希望构建库的版本(任务运行器)不是 包括Closure库的子集.如果Task Runner包含Closure库的子集,而另一个应用程序包含 full Closure库,则将存在一个竞争条件,在此条件之间,Closure最后一次加载(最后获胜).它还使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.

但是我遇到了麻烦.如果我不需要任何Closure库类,则Task Runner可以很好地构建(显然).但是,如果我需要某些东西(例如goog.dom),那么我的已编译" JavaScript文件还包括Closure库的一部分.有没有办法告诉Closure Compiler将某些JavaScript文件/模块/所有内容都排除在构建结果之外?

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,Plovr experimental-exclude-closure-library 选项似乎在某种程度上描述了我正在寻找的功能.

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

推荐答案

从表面上看,您的要求毫无意义.您想依赖/使用Closure库中的代码,但不将其包括在输出中.这实际上是不可能的,也不是库和编译器如何一起工作.

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.

在Closure库中定义的原始函数列表很少,当使用Closure-compiler进行编译时,这些原始函数将完全删除/替换. goog.requiregoog.provide是其中最突出的两个.

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.

对于大多数Closure-Library,如果您使用或依赖于类,方法或对象,则特定代码将出现在编译输出中.而且由于该库代码本身可能依赖于库的其他部分,因此编译后的结果中甚至可能包含更多代码.

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.

与更传统的JavaScript库相比,将带有Closure-library的Closure-compiler与其他传统JavaScript库相比,区别在于输出中仅包含确定要调用和使用的部分代码.这比简单的文件包含更加精细-原型,变量,常量等将全部排除,因为编译器可以确定它们从未使用过.

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.

如果要构建依赖于Closure-library的库,则有两种选择.

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

  1. 分发编译/构建的版本
    您将使用Closure-library编译您的库;导出任何公共API方法和属性.其他通过Closure-compiler或Closure-library使用您的库的人,将需要使用externs文件,并在编译后包含您的库.

  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.

将库作为源分发
您只需分发您的库源代码.其他人会将goog.require您的库作为其源代码和构建过程的一部分.如果两个项目都使用Closure-library,则在构建过程中将只包含一次.

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.

在编译代码但不包括Closure库的情况下,没有混合方法.这违反了所有源代码将被同时编译的原则.

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.

您可以细读我的地理位置标记库来查看示例.我提供了已编译的独立代码版本供使用,但是未编译的源代码也可以包含在其他使用Closure-library的项目中.

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.

这篇关于如何分发一个使用Closure库的库,而不包含任何实际Closure库的源代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆