FAQ: RequireJS Advanced Usage
How can I rename require/define/requirejs?§ 1
RequireJS and its optimization tool need to be version 0.26.0 or higher for this to work.
Why would you want to do this? You may have very strict global namespace requirements or you may be using code that already defines require/define and you want to avoid interference.
Some notes on this capability:
- Make sure to use a source version of require.js, not a minified version.
- Make sure that version of require.js is included in the optimization because the require.js file contents are altered for the namespacing to work.
- Code your modules using require/define as normal, then do a build to namespace the values. Do not code your modules using the namespaced require/define. It will make your code less portable and usable by others.
- This transformation/optimization only works once. Do not use the output of this optimization as input to another optimization/build stage.
The following example optimization config is based on the directory structure used in the example on the optimization page. This config combines require.js with main.js into a new foo.js. file. define() is renamed to foo.define() and require() is renamed to foo.require():
{
appDir: "../",
baseUrl: "scripts",
dir: "../../appdirectory-build",
//Put in a mapping so that 'requireLib' in the
//modules section below will refer to the require.js
//contents.
paths: {
requireLib: 'require'
},
//Indicates the namespace to use for require/requirejs/define.
namespace: "foo",
modules: [
{
name: "foo",
include: ["requireLib", "main"],
//True tells the optimizer it is OK to create
//a new file foo.js. Normally the optimizer
//wants foo.js to exist in the source directory.
create: true
}
]
}
Once this optimization is done, the HTML that used to refer to require.js would need to be modified to refer to foo.js.
Thanks to Ryan Florence for help on the namespace design.
Another approach to renaming, if you prefer to have more direct control of the content, and want to commit source code with the modifications. This approach should not be used with the "namespace" optimization demonstrated above.
1) Modify the source of require.js
There needs to be a wrapper around the require.js code so you can set the require function to the name of your choosing:
var myGlobalRequire = (function () {
//Define a require object here that has any
//default configuration you want for RequireJS. If
//you do not have any config options you want to set,
//just use an simple object literal, {}. You may need
//to at least set baseUrl.
var require = {
baseUrl: '..'
};
//INSERT require.js CONTENTS HERE
return require;
}());
2) Modify loaded files
For any files you load with this new function, if those files reference require in any way, you will want to wrap them in an anonymous function to set the value of require to be your new function name that you set up in step 1:
(function (require) {
//Regular require references now work correctly in here.
}(myGlobalRequire));
Following the steps above should allow you to use the optimization tool to combine scripts together effectively. If you want your renamed require definition in the optimized script, reference your modified require.js directly in the include optimization option, or as the name option if you want to optimize that file directly.
Thanks to Alex Sexton and Tobie Langel for suggesting parts of this solution.
What about loading CSS?§ 2
Ideally RequireJS could load CSS files as dependencies. However, there are issues knowing when a CSS file has been loaded, particularly in Gecko/Firefox when the file is loaded from another domain. Some history can be found in this Dojo ticket.
Knowing when the file is loaded is important because you may only want to grab the dimensions of a DOM element once the style sheet has loaded.
Some people have implemented an approach where they look for a well known style to be applied to a specific HTML element to know if a style sheet is loaded. Due to the specificity of that solution, it is not something that would fit well with RequireJS. Knowing when the link element has loaded the referenced file would be the most robust solution.
Since knowing when the file has loaded is not reliable, it does not make sense to explicitly support CSS files in RequireJS loading, since it will lead to bug reports due to browser behavior. If you do not care when the file is loaded, you can easily write your own function to load CSS on demand by doing the following:
function loadCss(url) {
var link = document.createElement("link");
link.type = "text/css";
link.rel = "stylesheet";
link.href = url;
document.getElementsByTagName("head")[0].appendChild(link);
}