1
votes

I am using require.js and r.js for the first time and i have nearly got it sorted however after i have built the app with r.js and uploaded to the server jquery is missing.

infact it is looking for jquery.js which is not on the server (as i was hoping it would still use the cdn as in my original require config).

1) should i include jquery and not load from a cdn? this makes me a little nervous as i thought it was always better to load jquery from a cdn. or should i be loading jquery in another call?

2) am i doing something wrong and jquery cdn should be loaded?

Could someone point me in the right direction to why jquery is not loading? i think i have followed all the configs correctly when using a cdn with r.js

heres my setup

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/
})

Example link: http://test.noise.agency/require.test.php

2

2 Answers

0
votes

I'm pretty sure the issue is that you are using a shim configuration for jQuery. jQuery has been calling define for ages. Version 1.11.0 certain does call it. (I know because I've used it.) So remove that shim you have for jQuery. At the same time verify that the other modules do in fact need a shim. Check their code if they call define to register themselves with RequireJS, don't use a shim. If you do use a shim with a module that calls define, you will get undefined behavior. It will mysteriously work in some cases and mysteriously fail in other cases.

Also, priority is a RequireJS 1.x option which is ignored in RequireJS 2.x.

0
votes

In your common.js file, is there any particular reason you cannot use the "baseUrl + paths" style of URL for your jQuery fallback path? So instead of using "/js/lib/jquery-1.11.0.min" you would use "lib/jquery-1.11.0.min"? I'm wondering if the former style of path is causing RequireJS to fail to recognize to jQuery as a module.

Also, in your build.js file, I'm wondering if it's really necessary to specify that jQuery should be excluded from your main module (via the "exclude" option)? Maybe the paths config (via the "empty" option) is enough to ensure that jQuery, whether it comes from the CDN or from the fallback path, is excluded from the build?

Couple of things to try anyway. Hope they're helpful.

=====

Looking at the code again I'm also wondering about this part of the shim config:

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

Assuming blueImpGallery works like a normal jQuery plugin and jQuery is its only dependency, jQuery should be declared as a dependency of the plugin like this:

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

In this scenario blueImpGallery would not need to export any global variable.

I'd also reiterate what Louis said about checking whether any of the other modules need to be shimmed.