简体   繁体   English

AMD模块整形:如何只加载一个JS函数?

[英]AMD Module Shaping: How to load only one JS function?

Many JavaScript libraries have a Builder tool that will allow you to "shape" what features of the library you depend on, both in terms of download bandwidth cost to the client and in terms of isolating functionality you actually need. 许多JavaScript库都有一个Builder工具,它允许您“塑造”您所依赖的库的哪些功能,包括客户端的下载带宽成本和隔离实际需要的功能。

For example, I like many things in sugar.js, but I simply don't need or want katakana and hiragana character set handling. 例如,我喜欢sugar.js中的很多东西,但我根本不需要或想要片假名和平假名字符集处理。 As the most trivial example, I want to be able to "shape" sugar.js to only export string.isBlank(). 作为最简单的例子,我希望能够“塑造”sugar.js以仅导出string.isBlank()。

Is there a tool available to do this for me? 有没有可以为我这样做的工具? Is there any ongoing effort by the EcmaScript committee to do something like this in a future version of JavaScript? 在未来的JavaScript版本中,EcmaScript委员会是否正在继续努力做这样的事情? Do any higher-level languages like TypeScript and CoffeeScript, offer hidden support for such "shaping"? 像TypeScript和CoffeeScript这样的高级语言是否为这种“整形”提供了隐藏的支持? I can do such "shaping" in C# for .NET DLLs via monolinker . 我可以通过monolinker在C#for .NET DLL中进行这样的“整形”。

Basically, it looks to me like AMD handles the Loader aspect of a modern compiler, but does not handle the Linker aspect. 基本上,它看起来像AMD处理现代编译器的Loader方面,但不处理链接器方面。 Builders for jquery and dojo only work for a specific module, and aren't true Linkers, just Builders. jquerydojo的构建器只适用于特定的模块,并不是真正的连接器,只是构建器。

UPDATE: Google Closure Compiler is a compiler that takes JavaScript as input and produces JavaScript as output. 更新: Google Closure Compiler是一个编译器,它将JavaScript作为输入并生成JavaScript作为输出。 The Advanced Compilation and Externs documentation suggests there is an API call to do this: Advanced Compilation和Externs文档建议有一个API调用来执行此操作:

If putting together these export statements seems too tedious, you can use a function to do the exporting for you. 如果将这些导出语句放在一起看起来太单调乏味,您可以使用函数为您进行导出。 See the Closure Library functions goog.exportSymbol() and goog.exportProperty() for examples of exporting functions. 有关导出函数的示例,请参阅Closure Library函数goog.exportSymbol()goog.exportProperty()

However, this seems pretty convoluted, and makes me directly dependent on Google Closure Compiler. 但是,这看起来很复杂,让我直接依赖于Google Closure Compiler。 At this point, I am looking for info about future standards from the EcmaScript Committee, with regards to CommonJS, and for any wisdom from people who have thought about this problem and attempted to tackle it. 在这一点上,我正在寻找关于EcmaScript委员会关于CommonJS的未来标准的信息,以及那些思考这个问题并试图解决它的人的任何智慧。 Especially from fellow TypeScript developers; 特别是来自TypeScript开发人员; I don't want to have to create declare files in TypeScript for sugar.js, then use Google Closure Compiler against my TypeScript compiler output. 我不想在TypeScript中为sugar.js创建声明文件,然后对我的TypeScript编译器输出使用Google Closure Compiler。 It just sounds impossibly complex and hard to debug. 它听起来很复杂,难以调试。

Unfortunately, there's nothing built into Javascript to do this "shaping", and really what you want is a compiler anyway, since roughly one of the roles it serves is to automate "shaping" at many levels (not just with methods). 不幸的是,Javascript没有内置到这个“整形”中,你真正想要的是编译器,因为它所服务的角色大致是在多个层面自动化“塑造”(不仅仅是方法)。

The Closure Compiler is mature and open source (JQuery is actually minified using the Closure Compiler). Closure Compiler是成熟且开源的(JQuery实际上是使用Closure Compiler缩小的)。 So if you're going to begin annotating your JS code for a compiler, it might as well be this one. 因此,如果您要开始为编译器注释JS代码,那么它也可能就是这样。

There a huge number of side benefits of using a compiler, btw. 顺便说一句,使用编译器有很多好处。 They will reduce your file sizes (and thus interpretation/run time) a great deal more than simply shaping your included libraries. 它们将减少您的文件大小(以及解释/运行时间),而不仅仅是简化您所包含的库的形成。 And while you're developing it will produce a lot of helpful messages to catch bugs early. 当你正在开发它时会产生很多有用的信息来及早发现bug。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM