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"
}
]
})
RequireJS is a JavaScript file and module loader. It improves perceived page load times because it allows JavaScript to load in the background. In particular, it enables asynchronous JavaScript loading.
As per RequireJS API documentation, shim lets you. Configure the dependencies, exports, and custom initialization for older, traditional "browser globals" scripts that do not use define() to declare the dependencies and set a module value. - Configuring dependencies.
To include the Require. js file, you need to add the script tag in the html file. Within the script tag, add the data-main attribute to load the module. This can be taken as the main entry point to your application.
RequireJS is a basic loader, which is used to loads the JavaScript files, it is a framework to manage dependencies between JavaScript files, and in modular programming, all the functionality divides in different modules, so RequireJs is a best tool to assemble different JavaScript files from different modules by which ...
This is how I am doing it. I like having the configuration file separate because I am reusing it in the tests.
Folder structure:
PROJECT
|
+- build (build output directory)
|
+- build-scripts (contains r.js, build.js)
|
+- WebContent
|
+- index.html (application main file)
|
+- scripts
|
+- require-cfg.js
|
+- main.js
|
+- ...
The configuration file (require-cfg.js
- showing only the relevant stuff):
var require = {
baseUrl: "scripts",
paths: ...
...
};
The build file (build.js
):
({
appDir: "../WebContent/",
baseUrl: "scripts/",
mainConfigFile: "../WebContent/scripts/require-cfg.js",
paths: {
/* repeated from require-cfg.js */
},
dir: "../build",
optimize: "uglify2",
inlineText: true,
removeCombined: true,
name: "main"
})
Bootstraping code (index.html
):
<script src="scripts/require-cfg.js"></script>
<script src="scripts/lib/require-2.0.2-jquery-1.10.2.js"></script>
<script src="scripts/main.js"></script>
I execute r.js
with the build.js
configuration inside the build-scripts
folder. Optimized and combined output goes to the build
folder. You can adapt the paths to suit your needs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With