使用bower的具有非AMD传递依赖项的RequireJS AMD组件
我正在开发一个AMD模块,它有一个在bower中定义的依赖项。使用模块的应用程序也具有此依赖关系。有没有办法在模块的amd定义中定义依赖项,这样使用模块的应用程序就不必按照组件期望的方式命名模块 模块的bower.json使用bower的具有非AMD传递依赖项的RequireJS AMD组件,requirejs,dependencies,amd,bower,Requirejs,Dependencies,Amd,Bower,我正在开发一个AMD模块,它有一个在bower中定义的依赖项。使用模块的应用程序也具有此依赖关系。有没有办法在模块的amd定义中定义依赖项,这样使用模块的应用程序就不必按照组件期望的方式命名模块 模块的bower.json {... "dependencies": { "my_module_dependency": "~1.2.21" } } 支持amd但不需要它的模块定义mymodule.js: (function (root, factory) { if(typeof
{...
"dependencies": {
"my_module_dependency": "~1.2.21"
}
}
支持amd但不需要它的模块定义mymodule.js:
(function (root, factory) {
if(typeof define === "function" && define.amd) {
define(function(require) {
//this works only if the app that uses this module defines my_module_dependency in main.js
var my_module_dependency = require("my_module_dependency");
return (root.mymodule = factory(my_module_dependency));
});
} else {
root.mymodule = factory(root.my_module_dependency);
}
}(this, function(my_module_dependency) {
...
}
这是可行的,但这取决于消费应用程序在其main.js中定义了一个名为my_module_dependency的requirejs依赖项,但我希望能够引用它,而不必强制消费应用程序按照组件想要的方式命名它
我知道我的模块是一个bower组件,我的模块的bower.json定义了my_module_依赖项,因此我知道依赖项相对于我的组件的位置,但我无法让requirejs使用我自己组件之外的相对路径来引用消费应用程序在其requirejs依赖项中已有的内容
也就是说,以下情况不起作用:
(function (root, factory) {
if(typeof define === "function" && define.amd) {
define(function(require) {
//we know this will be here b/c it's a bower dep and so is this module
var my_module_dependency = require("./bower_components/my_module_dependency");
return (root.mymodule = factory(my_module_dependency));
});
} else {
root.mymodule = factory(root.my_module_dependency);
}
}(this, function(my_module_dependency) {
...
}
正如预期的那样,只有一个调用来检索我的\u模块\u依赖项,因为消费应用程序已经需要它。如果我拼写错误,我会看到消费应用程序加载正确的版本,然后我的组件尝试加载拼写错误的资源并获得404。没有即时控制台错误,但一两秒钟后,此错误显示:
Uncaught Error: Load timeout for modules: bower_components/angular/angular
http://requirejs.org/docs/errors.html#timeout require.js:166
makeError require.js:166
checkLoaded require.js:692
(anonymous function)
此外,我需要消费应用程序和组件使用与我的_模块_依赖项相同的实例
猜猜看