gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:20:37 32 4
gpt4 key购买 nike

有什么方法可以使用 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/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com