1

Is it possible to set some option in require.js to allow the default scheme to include a sub-directory with the module name?

I want to write...

require(["underscore","jquery"],function(_,$){
    // do something here
})

And I want it to find jquery and underscore in this folder structure...

/
/lib/
    /jquery/
           /jquery.js
    /underscore/
           /underscore.js

Currently, I have to write...

require(["/lib/underscore/underscore","/lib/jquery/jquery"],function(_,$){
    // do something here
})

Or some crazy wrapper...

function req(arr,cb){
  require(arr.join().replace(/(\w+)/g,function(mod){ return "/lib/"+mod+"/"+mod }).split(","),cb)
}

2 Answers 2

0

You can use the paths config option to add the paths the way you want.

var libPaths = (function() {
  var libs = ["jquery", "underscore"];

  var lib, paths = {};
  for(var i=0; i < libs.length; i++) {
    lib = libs[i];
    paths[lib] = "/lib/" + lib + "/" + lib;
  }
  return paths;
})();

requirejs.config({
  paths: libPaths
});

To add more overrides, simply add to the libs array.

0

Yes, this should work with the stating this in the requirejs.config - function, like this:

requirejs.config({
    //By default load any module IDs from js/lib
    baseUrl: 'js/lib',

    //except, if the module ID starts with "app",
    //load it from the js/app directory. paths
    //config is relative to the baseUrl, and
    //never includes a ".js" extension since
    //the paths config could be for a directory.
    paths: {
        app: '../app'
    }
});

Taken from the Require.js API-docs, so in the paths - property you could specify all paths for the libraries you'll want to use

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.