简体   繁体   English

使用CDN jQuery的Require.js构建(r.js)未链接到良好的jQuery路径

[英]Require.js build (r.js) with CDN jQuery isn't linking to the good jQuery path

I'm having a Backbone application using Require.js for AMD. 我有一个使用Require.js for AMD的Backbone应用程序。 I'm loading jQuery from Google CDN, but after build, the path to jQuery seems to be broken. 我正在从Google CDN加载jQuery,但是在构建后,通往jQuery的路径似乎已损坏。

The build is happening without any trouble or error. 正在进行构建,没有任何故障或错误。 But once I use the build version, jQuery is added to page using this URL: 但是一旦使用构建版本,就会使用以下URL将jQuery添加到页面:

http://example.com/assets/js/jquery.js http://example.com/assets/js/jquery.js

Instead of the CDN url. 而不是CDN网址。 I feel this is due to the fact that my path config is lost and that require a dependency on "jquery" isn't taken as a reference to the path but as a normal call to a script. 我觉得这是因为我的路径配置丢失了,并且要求对“ jquery”的依赖不是作为对路径的引用而是作为对脚本的常规调用。

Here's my main file: 这是我的主文件:

main.js main.js

require.config({
    baseUrl: '/assets/js/',
    paths: {
            use: 'libs/use-0.2.0.min',
            jquery: 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min',
            underscore: 'libs/underscore-1.3.1.min',
            backbone: 'libs/backbone-0.9.2.min'
},
    use: {
            'underscore': {
                    attach: '_'
            },
            'backbone': {
                    deps: ['use!underscore', 'jquery'],
                    attach: function(_, $) {
                            return Backbone;
                    }
            }
    }
});

require(['views/app'], function(AppView){
    var app_view = new AppView();
});

app.build.js app.build.js

({
appDir: "../../www",
baseUrl: "assets/js",
dir: "../../build",
optimizeCss: "none",
optimize: "uglify",
findNestedDependencies: true,
preserveLicenseComments: false,
paths: {
    use: 'libs/use-0.2.0.min',
    jquery: 'empty:',
    underscore: 'libs/underscore-1.3.1.min',
    backbone: 'libs/backbone-0.9.2.min'
},
modules: [
    {
        name: "main",
        include: ["views/app"],
        exclude: ["jquery"]
    }
],
use: {
    'underscore': {
        attach: '_'
    },
    'backbone': {
        deps: ['use!underscore', 'jquery'],
        attach: function(_, $) {
            return Backbone;
        }
    }
}
})

(and I'm using use.js for loading non-AMD plugins) (并且我正在使用use.js加载非AMD插件)

I would first upgrade to the lastest RequireJS and check out this link: 我将首先升级到最新的RequireJS并查看以下链接:

http://requirejs.org/docs/optimization.html#empty http://requirejs.org/docs/optimization.html#empty

And the notes on CDN in this section: 以及本节中有关CDN的注释:

http://requirejs.org/docs/api.html#config http://requirejs.org/docs/api.html#config

An example of a local fallback for require.config( { paths : {} } ) : require.config( { paths : {} } )的本地后备示例:

The above pattern for detecting a load failure, undef()ing a module, modifying paths and reloading is a common enough request that there is also a shorthand for it. 上面的用于检测负载故障,对模块进行undef(),修改路径和重新加载的模式是一个足够常见的请求,它也有一个简写形式。 The paths config allows array values: 路径配置允许数组值:

requirejs.config( {
    // To get timely, correct error triggers in IE, 
    // force a define/shim exports check.
    enforceDefine : true,
    paths : {
        jquery : [
            '//ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min',
            //If the CDN location fails, load from this location
            'lib/jquery'
        ]
        // etc.
    }
} );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM