The Problem
RequireJS' optimizer cannot perform dependency analysis on dependencies that are generated at run time. It would at least have the capability to execute your code in a way that would mirror what happens at run time, and there would still be cases that it would not be able to handle. So for RequireJS' dependency analysis to be successful, your dependencies must be literal arrays of strings. In any other situation, RequireJS will fail to perform the analysis, and it will fail silently.
In your case, this has two consequences:
The optimizer does not actually give a name to your module. This explains the Mismatched anonymous define() module.
One of the functions of the optimizer is to give modules their final names. So a module which you put in main.js, for instance, when it is not yet optimized would be transformed so that it is defined as define("main", .... The optimizer adds the name to the start of the define call. (It does so only if the module has not yet been named.)
How can you tell your module is not named by the optimizer? Besides the error message, which is a major clue, if you look at what r.js produces, you'll find your module defined as define(deps,function. Notice how the first argument of your module is not its name.
You'll also see a stub following it. The stub looks like this define("main",function(){}). This stub should only be present if the code of the module is not an actual AMD-style module. For instance, modules for which you must use a shim configuration would have these stubs. Your module, however, does not need this stub.
At any rate, the optimizer is completely confused and does not name your module properly.
The optimizer does not find the dependencies of your module.
Solution: Adapt the Build
mfarouk has already mentioned how to modify your build so that the second consequence above is taken care of. I'll just mention that the general rule is this: you must explicitly put in the include for your module the union of all modules that the optimizer misses. It would look like this:
modules: [
...,
{
name: "mymodule",
include: [dependency1, dependency2, ...]
},
...
]
where dependency1, etc. are the dependencies in your deps array.
To take care of the first consequence, you need additional customization. I would suggest using the onBuildRead option of the optimizer:
onBuildRead: function (moduleName, path, contents) {
if (moduleName !== "main")
return contents;
return contents.replace(/define\(deps/,
'define("' + moduleName + '",deps');
}
This function is invoked when the optimizer reads each module. When it reads main, the define call is modified to add the module name. The optimizer will then see that the module is already named, will leave the name alone, and won't generate a spurious stub.