javascriptjqueryrequirejsrjs

How do I tell RequireJS to look in a subdir for certain modules?


I am not sure if this is possible, I've tried with a wildcard * in the paths config.

I want to load requested modules named jquery/* from subdir libs/jquery/src, but other files should be loaded in relation to baseUrl. This is what I've tried:

({
    baseUrl: ".",
    name: "main",
    out: "main.build.js",
    paths: {
        "jquery/*": "libs/jquery/src"
    }
})

Is this possible, one way or another?

Example:

require([
    "jquery/ajax.js",
    "view/table.js"
], function(tableView){

    var t = new tableView;

    return {
        tableView: tableView
    };

});

Would load jquery/ajax.js from ./libs/jquery/src/ajax.js and view/table.js from ./view/table.js.

Edit

I have also tried with this paths config:

paths: {
    "jquery": "libs/jquery/src"
}

In both cases I get this error: Error: Error: ENOENT, no such file or directory 'Development/rjs/jquery/ajax.js' while the expected path to look at is Development/rjs/libs/jquery/src/ajax.js


Solution

  • There is no actual pattern matching. However, RequireJS can match the start of a path to replace it with something else so you could do:

    paths: {
        "jquery": "libs/jquery/src"
    }
    

    With this, then if you do:

    require(["jquery/foo"], ...);
    

    RequireJS will seek libs/jquery/src/foo.js. Of course this will be problematic if you happen to want to load the whole of jQuery as jquery.

    This won't work if you put extensions in your dependencies. You have to remove the .js from your module names, otherwise RequireJS treats the name as a literal path and won't go through paths.