Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring modules with RequireJS when config depends on RequireJS

Apologies if I have missed this in the docs. Basically I want to use the RequireJS module configuration feature. I would like to centrally manage the config values given to modules in a package.

This is an example from the docs:

requirejs.config({
    config: {
        'bar': {
            size: 'large'
        },
        'baz': {
            color: 'blue'
        }
    }
});

//bar.js, which uses simplified CJS wrapping:
define(function (require, exports, module) {
    //Will be the value 'large'
    var size = module.config().size;
});

//baz.js which uses a dependency array,
define(['module'], function (module) {
    //Will be the value 'blue'
    var color = module.config().color;
});

My problem is that my configuration info will be a little more complex, and will itself have dependencies. I would like to do:

requirejs.config({
    config: {
        'bar': {
            path: path.dirname(module.uri)
            key: crypto.randomBytes(64)
        },
    }
});

Where variables in my config need to use requireJS to evaluate.

To me it would make sense for there to be a logical separation between the RequireJS configuration - the config necessary to load modules - and the user's module configuration. But I am currently struggling to find this :(

like image 228
greTech Avatar asked Jan 25 '13 09:01

greTech


People also ask

Is RequireJS still relevant?

RequireJS has been a hugely influential and important tool in the JavaScript world. It's still used in many solid, well-written projects today.

What is RequireJS config?

RequireJs is basically a JavaScript for the specific module. RequireJs use AMD Standards. RequireJs don't allow to run global JavaScript. If you have to use JavaScript then add into RequireJS configuration file. RequireJs share code and data between modules and programs.

What is the difference between RequireJS CommonJS and AMD loaders?

The main difference between AMD and CommonJS lies in its support for asynchronous module loading. "The main difference between AMD and CommonJS lies in its support for asynchronous module loading."

What is RequireJS used for?

RequireJS is a JavaScript library and file loader which manages the dependencies between JavaScript files and in modular programming. It also helps to improve the speed and quality of the code.


2 Answers

For this sort of solution, I would have the module depend on a "config" module that you can swap for a different one using paths config. So if "bar" needed some config, "bar.js" would look like:

define(['barConfig'], function (config) {
});

Then barConfig.js could have your other dependencies:

define(['crypto'], function (crypto) {
    return {
      key: crypto.randomBytes(64)
    }
});

Then, if you needed different configs for say, production vs. dev, use paths config to map barConfig to other values:

requirejs.config({
  paths: {
    barConfig: 'barConfig-prod'
  }
});
like image 86
jrburke Avatar answered Oct 14 '22 06:10

jrburke


I think the proper way to do this is to make a config module...

// config.js
define(['module', 'path', 'crypto'], function(module, path, crypto) {
    return {
        path: path.dirname(module.uri)
        key: crypto.randomBytes(64)
    };
}); 

Then use it in other modules...

// bar.js
define(['config'], function (config) {
    var key = config.key;
});

You can then make it as complicated as you like!

EDIT: You could pollute the global namespace for this special class...

define(['module', 'path', 'crypto'], function(module, path, crypto) {
    window.config = {
        path: path.dirname(module.uri)
        key: crypto.randomBytes(64)
    };
}); 

Add it to the top level require call:

require(['config', 'main']);

Then you can use it without always adding it to your define:

// bar.js
define([], function() {
    var key = config.key;
});
like image 31
Felix Avatar answered Oct 14 '22 06:10

Felix