Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I Place RequireJS Config in Separate File and Make the r.js Optimizer Work?

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"
        }
    ]
})
like image 975
elanh Avatar asked Oct 03 '13 21:10

elanh


People also ask

What is RequireJS config js?

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.

What is RequireJS Shim?

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.

How add RequireJS to HTML?

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.

Why do we need RequireJS?

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 ...


1 Answers

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.

like image 181
Nikos Paraskevopoulos Avatar answered Oct 04 '22 19:10

Nikos Paraskevopoulos