requirejsamdjs-amd

requirejs - define global variable as amd module


I wrote a requirejs app and it will be included in various environments, some of them will already provide some javascript frameworks, that i would like to reuse insteadof the provided amd modules i bring along.

I am speaking about jquery and Foundation. Some of my app-scripts make use of those frameworks themselves and i would like to do something like the following example, but i cant figure out, if this can even be done with requirejs and if yes - how...

requirejs.config({
    paths: {
        "jquery": "window.$",
        "foundation": "window.Foundation",
        "foundation.module": "window.Foundation.Module"
    },
    shim: {
        "jquery": {
            exports: "window.$"
        },
        "foundation": {
            deps: ["jquery"],
            exports: "window.Foundation"
        }
    }
});

Can i achieve something like this?


Solution

  • What you are looking for is a proxy module. As the Foundation and jQuery is already loaded, before the RequireJS I assume.

    It is quiet simple, just define a module which will return a global variable and that's all, here, take a look at jQuery example:

    define('jquery', function () {
      return window.$;
    });
    

    No config needed for that. You can remove the paths and the shims as they are no needed.