[英]AMD modules with transitive dependencies
Suppose I want to create an AMD module that itself depends on some other AMD module. 假设我想创建一个本身依赖于其他AMD模块的AMD模块。 Doesn't matter what, but to be tangible, suppose its some new kind of date-picker widget that depends on moment.js 没关系,但实际上,假设它依赖于moment.js的某种新型日期选择器小部件
So I setup a bower.json that looks something like this: 所以我设置了一个bower.json,看起来像这样:
{
"name": "sampleLib",
"version": "0.0.1",
"description": "test",
"main": "myControl.js",
"moduleType": [
"amd"
],
"dependencies": {
"moment": "~2.8.1"
}
}
OK, cool. 好的。 No problem there. 没问题。
My question is about how it gets consumed... I see two options, neither of which I love: 我的问题是关于它如何被消耗的...我看到两个选择,我都不喜欢:
Concat my code and moment.js into a unified script, and publish that. 将我的代码和moment.js连接到一个统一的脚本中,并将其发布。 Its easy for clients to consume since they only have to depend on my module. 客户端只需使用我的模块即可轻松使用。 In this scenario: 在这种情况下:
Don't concat my code and keep moment.js declared as a dependency in bower.json. 不要合并我的代码,并在bower.json中将moment.js声明为依赖项。 So when a client bower install
s my library, they'll also bring down moment.js. 因此,当客户bower install
我的库时,它们还将关闭moment.js。 Thats cool. 这很酷。 But the thing I worry about is namespacing... Suppose I depend on moment as 'lib/moment', but the client is structured so that it just depends on moment as 'moment'? 但是我担心的是命名空间...假设我将瞬间视作“ lib / moment”,但客户端的结构使其仅依赖于瞬间视作“ moment”? At runtime, does my code fail because lib/moment isn't defined? 在运行时,我的代码是否会因为未定义lib / moment而失败? I worry this is too brittle, is there a way to harden this, besides good documentation? 我担心这太脆弱了,除了良好的文档记录之外,还有其他方法可以强化这一点吗?
What is the most robust way to publish AMD modules with dependencies? 发布具有依赖性的AMD模块的最可靠方法是什么?
Option 1 - While concatenating is simpler for distributing your code, you hit on one of the issues with doing so - you run the risk of conflicts with other libraries. 选项1-虽然串联更容易分发代码,但这样做却遇到了一个问题-冒着与其他库冲突的风险。 You also increase the size of your js library. 您还增加了js库的大小。
Option 2 - I don't think you're really referring to namespacing here, but rather the path of the module. 选项2-我认为您并不是真正在这里指代命名空间,而是模块的路径。 Is that correct? 那是对的吗? RequireJS has a config block that you can change base paths and mappings that should solve the issue you're referring to. RequireJS有一个配置块,您可以更改基本路径和映射来解决您所指的问题。 http://requirejs.org/docs/1.0/docs/api.html#config http://requirejs.org/docs/1.0/docs/api.html#config
So, in your case if you want to publish an AMD module and dependencies, I would still go with option 2. 因此,在您的情况下,如果要发布AMD模块和依赖项,我仍然会选择选项2。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.