I don't want to expose the internal define calls externally (by flattening out all the defines) since i don't want library consumers to be able to use those internal modules.
I don't know of a simple way to declare to RequireJS a module and at the same time prevent the module from being able to be used by any code that can call RequireJS's require
. Let's suppose the code you show would work. Then module b
would be accessible to any code as soon as module a
has been loaded. ("Loaded" here means that the factory function has executed.) So while accessing b
would be dependent on loading a
first, b
would not be hidden.
The reason your code does not work is that the "synchronous" require
of RequireJS is pseudo-synchronous... or perhaps a better term is that it is a pseudo-require
which does operate synchronously but will bail right away if the module you want is not loaded. Again, "loaded" here means not only that a define
call for the module has been executed but that the module's own dependencies are already loaded and that the module's factory function has executed already. In your code, the dependencies of module b
have not been loaded and the factory function for b
has not executed yet, so your require
call fails.
You'd be able to load b
with require(['b'])
but then you'd have to return a promise of some sort from a
.
You could essentially hide what is in module b
by using scoping but not making b
a module:
define("a", ['somedep', 'anotherdep'], function(dep, dep2){
var b = (function (foo, bar) {
return { something: "x" };
})(dep, dep2);
return {
b: b.something
};
});
requirejs
was not meant to do that way – Vigneswaran Marimuthu