Some extensions may require additional control over how RequireJS loads modules. Typical usage of RequireJS allows developers to configure these options via a configuration object.
Brackets' ExtensionLoader creates a unique require context for each extension to give each one it's own module namespace/sandbox. Since this can only be done with the global require
, the extension loader has to do this prior to loading the extension's main.js
.
Backbone is a good example of a 3rd party library that doesn't use define()
to declare dependencies and set a module value. In order for an extension to properly load Backbone, it first has to load Underscore (and technically jQuery but Brackets already loads this prior to loading extension).
RequireJS has a solution for this, but it relies on configuration via require.config()
.
Another common usage of the configuration object is to specify alternate paths for mapping module names that aren't found directly under your extension's root folder.
Specifying a requirejs-config.json
file allows extensions to provide a JSON object to configure it's require context before it's main.js
file is loaded. Here's an example file:
{
"paths": {
"underscore": "libs/underscore/underscore-min",
"backbone": "libs/backbone/backbone-min"
},
"shim": {
"underscore": {
"exports": "_"
},
"backbone": {
"deps": [ "underscore" ],
"exports": "Backbone"
}
}
}
Be sure to ship this file in your extension .zip
file. It is required whether or not you use the r.js optimizer to build your extension.