Alexander Mills
Alexander Mills

Reputation: 99960

Naming modules with RequireJS

I am trying to figure out how to name modules without using the relative path of the module itself.

For example:

Instead of doing this:

//module is defined in app/js/routers.js
define('app/js/routers',['app/js/currentView'],function(currentView) {

});

I would rather do this:

//module is defined in app/js/routers.js
define('routers',['app/js/currentView'],function(currentView) {

});

is this possible or is it a bad idea? Obviously, the names that you give each module would have to be unique. I guess I just don't the see point of the optional first parameter in the define call if it has to be same as the path of script it's contained in.

Upvotes: 0

Views: 52

Answers (1)

Louis
Louis

Reputation: 151370

I guess I just don't the see point of the optional first parameter in the define call if it has to be same as the path of script it's contained in.

Yep, if you are going to put the module in a file whose path, relative to the baseURL you give in your RequireJS configuration, is the name of the module, then you do not have to provide a module name to define. In fact, you should not give a module name in this case. The RequireJS documentation states:

These [i.e. module names] are normally generated by the optimization tool. You can explicitly name modules yourself, but it makes the modules less portable -- if you move the file to another directory you will need to change the name. It is normally best to avoid coding in a name for the module and just let the optimization tool burn in the module names. The optimization tool needs to add the names so that more than one module can be bundled in a file, to allow for faster loading in the browser.

(Emphasis added.)

Upvotes: 1

Related Questions