避免使用 requirejs 加载已经注入到 DOM 中的模块
Avoid loading modules already injected into the DOM with requirejs
有没有办法避免将可能已经存在的模块加载到 DOM 中?
例:
require.config({
paths: {
// jquery here is needed only if window.jQuery is undefined
'jquery': '//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min'
}
});
能够使用类似此代码段的内容会很棒
require.config({
paths: {
'jquery': {
uri: '//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min',
// if this function returns false or undefined load the script from the url
define: function(){ return window.jQuery; }
}
}
});
-----------------------------------------------------------------
更新
-----------------------------------------------------------------
我向 github 上的@jrburke发送了一个拉取请求,https://github.com/jrburke/requirejs/issues/886 我的提案。requirejs 的固定版本可以在这里进行测试:
http://gianlucaguarini.com/experiments/requirejs/requirejs-test3.html
这里根据我的 API 建议进行 requirejs 配置
require.config({
paths: {
// jquery here is needed only if window.jQuery is undefined
'jquery':'//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min',
'lodash':'//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.0.0/lodash.underscore.min',
'backbone':'//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.0.0/backbone-min'
},
shim:{
'jquery':{
// with my fix now I detect whether window.jQuery has been already defined
// in this case I avoid to load the script from the cdn
exports:'jQuery',
// if this feature is missing I need to load the new jQuery from the cdn
validate: function(){
return window.jQuery.Defferred;
}
},
'lodash':{
// lodash will be loaded only if it does not exist in the DOM
exports:'_',
// if this function returns false or undefined load the script from the cdn
validate: function() {
// is the lodash version already available in the DOM new enough for my application?
return window.parseInt(window._.VERSION) >= 2;
}
},
'backbone':{
deps:['lodash','jquery'],
// if backbone exists we don't need to load it twice
exports:'Backbone'
}
}
});
正如@jrburke在您的拉取请求中指出的那样,这样做的方法是:
require.config({});
if (typeof jQuery === 'function') {
define('jquery', function() { return jQuery; });
}
// start module loading here
require(['app'], function(app) {});
如果已定义模块,则不会(重新)加载该模块。在这里,定义只是重用已经加载的全局jQuery
对象。
由于 jQuery 与 AMD 兼容,如果它已经在页面 Require.js 不会再次加载它。
更广泛地说,Require.js 仅在尚未定义模块时查看路径配置。因此,一旦您定义了模块,Require.js 就不会再次加载它:
define('jquery', [], function() { /* stuff */ });
// ^ Module 'jquery' is defined here. Require.js won't load it twice.
查看此 JsBin 以获取工作示例:http://jsbin.com/OfIBAxA/2/edit
相关文章:
- 注入的HTML仅在Dom中解释为字符串
- 将Backbone.View重新注入DOM,保留事件而不是创建新事件
- chrome扩展在加载dom之前注入动态css
- 是通过JS注入的DOM元素
- 使用 Directives 注入 DOM 元素 AngularJS
- 在通过AJAX加载的DOM中运行动态注入的javascript(尝试使用历史记录ajax化网站.js)
- 避免使用 requirejs 加载已经注入到 DOM 中的模块
- 是否可以将javascript注入使用ajax调用后加载的dom
- 与<选择>以及DOM注入
- 经过大量JS DOM注入/操作后的DOM验证
- Chrome扩展:将元素追加到DOM中没有立即出现的新注入的元素中
- 使用DOM注入动态预取资源是否有效或有意义
- 是一个ReactJS渲染函数,注入了全部或部分添加的DOM元素
- 你怎么能要求DOM在Angular注入正确的变量之前不加载图片呢?
- 加载iframe而不将其注入DOM
- 当注入javascript代码覆盖DOM中存在的代码时,会出现问题
- Chrome扩展:连接"内容脚本"注入DOM
- 在JSON内部创建链接,并通过AngularJS将其注入dom
- 如何在新对象被注入DOM后使用jQuery
- 在页面加载时将部分注入DOM