Require.js,r.js和jquery CDN。构建后jquery缺失

时间:2014-11-16 14:30:43

标签: jquery requirejs cdn

我第一次使用require.js和r.js而且我几乎已经对它进行了排序,但是在我用r.js构建应用程序并上传到服务器后,jquery丢失了。

实际上它正在寻找不在服务器上的jquery.js(因为我希望它仍然会像我原来的require config一样使用cdn)。

1)我应该包含jquery而不是从cdn加载吗?这让我有点紧张,因为我认为从cdn加载jquery总是更好。或者我应该在另一个电话中加载jquery?

2)我做错了什么并且应该加载jquery cdn?

有人能指出我为什么jquery没有加载正确的方向?我认为我在使用带有r.js

的cdn时正确地遵循了所有配置

继承我的设置

common.js

require.config({
    baseUrl: 'js/',
    enforceDefine : true,
    paths: {
        'jquery': ['//code.jquery.com/jquery-1.11.0.min','/js/lib/jquery-1.11.0.min'], 
        'modernizr':'lib/modernizr.2.8.2',
        'bootstrap':'lib/bootstrap.min.amd',
        'safeBrowsing':'lib/safe.browsing.amd',
        'owl':'lib/owl.carousel.min.amd',
        'placeholder':'lib/placeholder.amd',
        'easyPieChart':'lib/easyPieChart.amd',
        'jqueryEasing':'lib/jquery.easing.amd',
        'infoBubble':'lib/infobubble.amd',
        'async':'lib/require/async',
        'font':'lib/require/font',
        'goog':'lib/require/goog',
        'image':'lib/require/image',
        'json':'lib/require/json',
        'noext':'lib/require/noext',
        'mdown':'lib/require/mdown',
        'propertyParser':'lib/require/propertyParser',
        'blueImpGallery': 'lib/blueimpGallery/jquery.blueimp-gallery'
    },
    priority: ['jquery'],

   shim:  {
        'jquery': {
            exports: '$'
        },
        'blueImpGallery' : {
             exports : 'jquery'
        },
   }
});

main.js

define(['jquery','modernizr','bootstrap','safeBrowsing','owl','blueImpGallery','infoBubble'],function($){

console.log('foo bar');

});

build.js

({
    mainConfigFile : "../js/common.js",
    appDir: "../",
    baseUrl: "js",
    dir: "../../code-build/",
    removeCombined: true,
    findNestedDependencies: true,
    optimize: "uglify2",
    optimizeCss: "standard",
    paths: {
        'jquery': "empty:"
    },
    modules: [
    {
      name: "main",
      exclude: ['jquery']
    }
    ],
    wrapShim: 'true',
    generateSourceMaps: true,
    preserveLicenseComments: false,
    fileExclusionRegExp: /^admin$|.psd$|git-ftp|build|testnoise.sublime-project|testnoise.sublime-workspace/
})

示例链接:http://test.noise.agency/require.test.php

2 个答案:

答案 0 :(得分:0)

我很确定问题是你正在为jQuery使用shim配置。 jQuery多年来一直在调用define。版本1.11.0肯定会调用它。 (我知道,因为我已经使用过了。)所以删除你对jQuery的shim。同时验证其他模块实际上是否需要垫片。如果他们调用define向RequireJS注册自己,请检查他们的代码,不要使用填充程序。如果您使用带有调用define的模块的垫片,您将得到未定义的行为。在某些情况下它会神秘地起作用,而在其他情况下会神秘地失败。

此外,priority是RequireJS 1.x选项,在RequireJS 2.x中被忽略。

答案 1 :(得分:0)

在你的common.js文件中,有什么特别的原因你不能使用" baseUrl + paths"您的jQuery回退路径的URL样式?所以不要使用" /js/lib/jquery-1.11.0.min"你会使用" lib / jquery-1.11.0.min"?我想知道前一种路径是否导致RequireJS无法将jQuery识别为模块。

另外,在你的build.js文件中,我想知道是否真的有必要指定jQuery应该从主模块中排除(通过" exclude"选项) ?也许路径配置(通过"空"选项)足以确保jQuery(无论是来自CDN还是来自回退路径)被排除在构建之外?

无论如何都要尝试一些事情。希望他们有所帮助。

=====

再次查看代码,我也想知道这个部分的shim配置:

...
'blueImpGallery' : {
    exports : 'jquery'
},
...

假设blueImpGallery的工作方式与普通的jQuery插件类似,而jQuery是唯一的依赖项,那么应该将jQuery声明为插件的依赖项,如下所示:

...
'blueImpGallery' : {
    deps : ['jquery']
},
...

在这种情况下,blueImpGallery不需要导出任何全局变量。

我还重申路易斯所说的关于检查是否需要填充任何其他模块的内容。

相关问题