Context and nested modules in requireJS

I'm having context issues in requireJS. I would like to create a “mycontext” context at the configuration stage (before I load any modules), and then save that context. This is complicated because I, unfortunately, need (<- ha!) To use CommonJS syntax for my modules. So, if this is my base file, it looks like this:

base.js

contextReq = require.config({
    context: 'mycontext',
    baseUrl: 'http://www.example.com/src/',
    paths:{
        jquery: 'http://ajax.cdnjs.com/ajax/libs/jquery/2.0.3/jquery.min',
    },
});

(function(){
    contextReq(['require', 'topModule'], function(require, topModule){
        topModule.initialize();
    });
})();

Then I load topModule:

http://www.example.com/src/topModule.js

define(['require', 'jquery', 'nestedModule'], function (require) {
    var $ = require('jquery');
    var other = require('nestedModule');
});

Will jQuery only load in mycontext? What if I go further:

http://www.example.com/src/nestedModule.js

define(function (require) {
    var $ = require('jquery');
    var oneMore = require('someOtherModule');
});

jquery , "someOtherModule" "_"? , , ?

!

+4
1

, . , , ".s", , , . "require.s.contexts" requrie:

base.js

contextReq = require.config({
    context: 'mycontext',
    baseUrl: 'http://www.example.com/src/',
    paths:{
        jquery: 'http://ajax.cdnjs.com/ajax/libs/jquery/2.0.3/jquery.min',
    },
});

(function(){
    contextReq(['require', 'topModule'], function(require, topModule){
        topModule.initialize();
    });
})();

//Timeout, then see where we stand
setTimeout( function () {
    console.log(require.s.contexts._);
    console.log(require.s.contexts.mycontext);
}, 500);

:

//Console.log for context '_' (the default require context)
{
     [...]
     defined: [], //empty array, nothing has been loaded in the default context
     [...]
}

//Console.log for context 'mycontext' (the default require context)
{
     [...]
     defined: [ //Filled out array; everything is loaded in context!
          topModule: Object
          nestedModule: Object
          jquery: function (e,n){return new x.fn.init(e,n,t)} //jQuery function
     ],
     [...]
}

, , : requireJS , , , , .

+7

All Articles