Most examples for RequireJS setup, place the configuration object in the main.js entry point, something like this:
//main.js
require.config({
"paths": {
//libs
"lib1": "assets/js/lib/lib1",
"lib2": "assets/js/lib/lib2",
"lib3": "assets/js/lib/lib3",
"lib4": "assets/js/lib/lib4"
}
});
//start the app
define(["lib1"], function(lib1){/*start the app*/});
I prefer to place the configuration object in a separate file, because as it grows, it's difficult to maintain in the same file.
The following setup works when I run it in the browser, but for some reason I get an error when running the r.js optimizer:
//config.js
define({/*all configuration here*/});
//main.js
define(["config", "require"], function(config, require){
requirejs.config(config); //set configuration
require(["app"]); //launch app, where "app" path is defined in config.js
});
When I run r.js, I get the following error:
*Tracing dependencies for: main
Error: ENOENT, no such file or directory 'C:\Work\project\target\app.js*
So it seems that r.js doesn't get the configuration settings, because it's looking for app.js as a relative script, not as a module with a defined path.
Here is my build.js file (appDir, dir and mainConfigFile are relative to the build.js file):
({
appDir: "../src",
baseUrl: ".",
dir: "../target",
mainConfigFile: "../src/main.js",
findNestedDependencies: true,
modules: [
{
name: "main"
}
]
})