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

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

本文关键字:js jQuery 链接 路径 构建 CDN 不是 使用 Require      更新时间:2023-09-26

我有一个使用Require.js的AMD骨干应用程序。我正在从Google CDN加载jQuery,但在构建之后,jQuery的路径似乎被破坏了。

生成过程中没有出现任何问题或错误。但一旦我使用了构建版本,jQuery就会使用以下URL添加到页面中:

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

而不是CDN url。我觉得这是因为我的路径配置丢失了,并且需要对"jquery"的依赖。jquery不是对路径的引用,而是对脚本的正常调用。

这是我的主文件:

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

({
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;
        }
    }
}
})

(我使用use.js加载非AMD插件)

我会首先升级到最新的RequireJS并查看此链接:

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

以及本节中关于CDN的注意事项:

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

require.config( { paths : {} } ):的本地回退示例

上述检测加载失败、取消模块定义()、修改路径和重新加载的模式是一个常见的请求,因此也有一个简写。路径配置允许数组值:

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.
    }
} );