The problem in the question has nothing to do with minification because the code shown in the question cannot work, with or without minification.
The issue is that the values that should be accessible outside the module must be exported. And outside the module they are accessible through the variable to which this module is bound by a require or define function.
To export values, you can return them from the factory function that is passed to define. (There are also other means to do this but I'm not going to get into them now.) Your foo.js file could be like this:
define(function () {
function Parent() {
}
function MainModule() {
}
MainModule.prototype = new Parent();
var bar = new MainModule();
// Here I'm exporting the values I want accessible outside.
return {
MainModule: MainModule,
bar: bar
};
});
I've obviously had to fill foo.js with the minimum amount of code to have something that would work.
Then you could have an index.html like this:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/xhtml; charset=utf-8"/>
<script type="text/javascript" src="js/require.js"></script>
</head>
<body>
<script>
require.config({
baseUrl: "./js",
});
require(["foo"], function(foo){
console.log(foo, foo.MainModule, foo.bar)
});
</script>
</body>
</html>
and foo.js and RequireJS located in the js subdirectory (relative to index.html), you'll see on the console that foo, foo.MainModule, and foo.bar are defined. The require call loads the module named foo, which resolves to js/foo.js and binds the value returned by the module's factory to the first argument of the callback passed to require. In this case the name of this argument is foo. So in the callback,
The last two bindings exist only because the module foo exports its values as shown above.