javascript - 在约定优于配置的开发中使用 goog.require 的任何方法?

标签 javascript google-closure google-closure-library convention-over-configur

有什么方法可以使用 Google Closure goog.require 来管理 JS 依赖项,而不必在 dependencies.js 文件中显式注册每个 namespace ?

我喜欢生产编译器的想法,但对于开发,我想要某种约定优于配置的命名空间到 JS 文件夹/路径的转换,例如 goog.require('myapp .module') 在开发模式下自动导入 myapp/module.js(如果尚未导入),而在生产模式下,它全部编译到一个文件中。

我似乎记得 dojo.require 的旧版本是这样工作的。知道 Google Closure 是否可以做同样的事情吗?

最佳答案

Is there any way I can use Google Closure goog.require to manage JS dependencies, without having to register each namespace explicitly in a dependencies.js file?

简短的回答是否定的。在未编译 JavaScript 代码中,goog.require() 依赖于通过调用 goog.addDependency(relativePath, provides, requires) 生成的依赖图。

来自DepsWriter文档:

But how does Closure Library know which files provide which namespaces? Included in Closure Library is a default dependency file named deps.js. This file contains one line for every JavaScript file in the library, each specifying the file location (relative to base.js), the namespaces it provides, and the namespace it requires.

In debug mode, a goog.require() statement looks to see if the require namespace was specified and, if so, fetch the files that provide it and all of its dependencies.

However, Closure Library only comes with a dependency file for the namespaces in the library. If you plan to write your own namespaces (and you likely will), you can use depswriter.py to make a dependency file for the namespaces you create.

也就是说,您可以使用诸如 plovr 之类的工具,它提供了一种“RAW”模式,无需编译即可连接您的 JavaScript 源代码。这避免了生成 deps 文件的需要,但这也意味着在浏览器中调试代码时,确定包含代码的原始文件更具挑战性。

关于javascript - 在约定优于配置的开发中使用 goog.require 的任何方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12875532/

相关文章:

javascript - 在 Javascript 中切换开发/测试/生产变量

javascript - 使用 javascript 将 JSON 对象转换为数组

javascript - js迁移问题

javascript - 为什么 Google 的 Closure 库不托管在他们的 CDN 上?

javascript - 使用 Google Closure 从 LabelInput 访问数据

javascript - 通过类名获取祖先

javascript - 为什么在 isFinite() 之后检查 !isNaN()?

javascript - ReactJS的疑惑和疑问

javascript - 解决猜词游戏中的 js 错误

javascript - 为什么我应该使用 goog.ui.Checkbox 而不是常规输入?