【发布时间】:2015-01-25 01:39:21
【问题描述】:
我有一个库 - 称之为 SomeLib - 它被定义为支持各种模块加载器:
(function(global, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof module !== 'undefined' && module.exports) {
module.exports = factory();
} else {
global.UriTemplate = factory();
}
})(this, function() {
...
// returns constructor function
});
我可以很容易地用 RequireJS 加载它
require.config({
paths: {
'theLibrary: '../path/to/the/lib'
}
});
然后我有另一个 3rd-party 库 - 称之为 AnotherLib - 它在内部使用 SomeLib 之类的
var the Lib = new SomeLib(...);
这意味着SomeLib 必须在全球范围内可用。
AnotherLib 只是一个普通的 JavaScript 模块函数
(function(){
// the code
})();
它不符合特定的模块加载器。
当我在 RequireJS 中包含 AnotherLib 时,我会做类似的事情
require.config({
paths: {
'theLibrary: '../path/to/the/lib',
'anotherLib: '../path/to/anotherLib'
},
shim: {
'anotherLib: [
'theLibrary'
]
}
});
问题是我在 AnotherLib 中实例化 SomeLib (new SomeLib(...)) 的行上遇到了一个未定义的异常。
这是因为 SomeLib 没有在全局对象上定义,而是作为一个 AMD 模块发布,而 AnotherLib 并不“需要”。
我能否以某种方式解决这个问题,或者 AnotherLib 是否必须符合 AMD 标准并且正确地需要 SomeLib。
【问题讨论】:
标签: javascript requirejs