如何在 require.config 中的一个 CDN link 上使用 urlArgs 存在 2 个或更多路径以从 CDN 包含

how to use urlArgs on one CDN link in require.config in presence of 2 or more paths to include from CDN

我想使用两个项目,但我的首要任务是从 CDN 网络加载它们,因此在我的配置路径中我需要加载 bowser、jquery、html5shiv 和 facebook 填充程序。

我的配置路径中 urlArgs 的值专门用于 jquery 以提供子资源完整性 (SRI)。

但是 urlArgs 附加到所有链接并且 jquery 意外工作。

是否可以在存在其他路径的情况下使用路径特定的 urlArgs?

我的配置在下面。

require.config({
  enforceDefine:true,
  urlArgs: 'integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="&crossorigin="anonymous"',
  shim: {
    facebook : {
      exports: 'FB'
    }
  },
  paths: {
    facebook: 'http://connect.facebook.net/en_US/sdk',
    jquery: [
      'https://code.jquery.com/jquery-2.2.4.min'
     // 'jquery',
    ],
    bowser:[
      'https://cdnjs.cloudflare.com/ajax/libs/bowser/1.0.0/bowser.min',
      'bowser-min'
    ],
    html5shiv:[
      'https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.min',
      'html5shiv'
    ]
  }
});
require(['loginsignupslider','featuredslider','arrangehomepage','bowser'],function(a,b,c,bowser){
  if (bowser.msie && bowser.version < 9) {
    require(['html5shiv']);
  }
});
define(['facebook','jquery'],function(FB,$){
   FB.init({
      appId      :  '1693650154217810',
      status     :  true,
      cookie     :  true, 
      xfbml      :  true, 
      oauth      :  true,
      version    :  'v2.5' // use graph api version 2.5
   });
   FB.Event.subscribe('auth.statusChange', function(response) {
     if (response.authResponse&&response.status==='connected'){
               window.location.href = "http://localhost/influenza/scripts/loginset.php";
     }
   });
   $("#influencer-login-social >:first-child a").on("click",function(){
        FB.getLoginStatus(function(response) {
           if (response.status !== 'connected') {
               FB.login(function(response) {},{scope:'public_profile,email,user_friends'});
           }
        }, true)
   });
});

您可以编写自己的函数来测试模块 id 或 URL 和 return 适当的 URL 参数添加或 return 空字符串。根据 documentation:

As of RequireJS 2.2.0, urlArgs can be a function. If a function, it will receive the module ID and the URL as parameters, and it should return a string that will be added to the end of the URL. Return an empty string if no args. Be sure to take care of adding the '?' or '&' depending on the existing state of the URL. Example:

requirejs.config({
    urlArgs: function(id, url) {
        var args = 'v=1';
        if (url.indexOf('view.html') !== -1) {
            args = 'v=2'
        }

        return (url.indexOf('?') === -1 ? '?' : '&') + args;
    }
});