Skip to content

Instantly share code, notes, and snippets.

@jasonsanjose
Last active August 29, 2015 14:03
Show Gist options
  • Save jasonsanjose/6e5d86dc6609f97d3d3b to your computer and use it in GitHub Desktop.
Save jasonsanjose/6e5d86dc6609f97d3d3b to your computer and use it in GitHub Desktop.
requirejs-config.json

Advanced RequireJS Configuration

Background

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.

Why?

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.

How?

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment