Redirect source paths to AMD modules when require.js baseUrl is a relative path to a redirected directory?

222 views
Skip to first unread message

Garrick Cheung

unread,
Aug 15, 2013, 2:52:01 AM8/15/13
to bust...@googlegroups.com
I'm setting up AMD module testing in browsers. I'm kind of stuck with require.js and a config that defines a baseUrl to a directory that usually has a redirect. The baseUrl is "/scripts".

require.js takes the domain of the page, appends the baseUrl to it, and then appends the path to the AMD modules. But the issue is that Buster.js serves sources from /sessions/static. And the scripts directory is actually in the web directory.

Just take a look at the directory structure below.

root
- web
-- scripts
--- config.js
--- other folders and modules
- test
-- buster.js
-- amd-test.js (I'm just running one test to.. test things out)

Based on this structure, sources actually need to be served from /sessions/static/web/scripts. How do I redirect /scripts to the correct directory (/sessions/static/web/scripts) without having to adjust the require.js config file?

The config.js file roughly looks like:

    require.config({
        baseUrl: "/scripts"
        ...
    });

Below is the buster.js config file I'm using. I'm using buster-amd and have defined a pathMapper function that's used to correct the path to load tests via require.js. The buster-amd docs state that a pathMapper may need to be defined in case that rootPath interferes with the baseUrl in require.js. With the pathMapper function, the path for a test becomes "../sessions/static/test/amd-test" which require.js will interpret and remove the "/scripts" baseUrl from the path.

    config["AMD Tests Browser"] = {
        rootPath: "../",
        environment: "browser",
        libs: [
            "web/scripts/libs/jquery-ui/1.9.0/js/jquery-1.8.2.js",
            "web/scripts/libs/requirejs/2.1.6/require.js",
            "web/scripts/config.js"
        ],
        sources: [
            "web/scripts/**/*.js"
        ],
        tests: [
            "test/amd-test.js"
        ],
        extensions: [
            require("buster-amd")
        ],
        "buster-amd": {
            pathMapper: function(path){
                var p = path.replace(/\.js$/, "").replace(/^\//, "../sessions/static/");
                return p;
            }
        }
    };

I've tried looking into defining a resources option:

    resources: [
        {
            path: '/scripts',
        }
    ]

But this doesn't seem to work, or I'm just doing something wrong?

I can also change the config's baseUrl to web/scripts, and change the regex in the pathMapper function to:
    
    var p = path.replace(/\.js$/, "").replace(/^\//, "../../");

But having to maintain two copies of the require.js config file is more likely to cause issues in the future.

-Garrick-
Reply all
Reply to author
Forward
0 new messages