How do I use the Optimization Tool?§ 1

See the general optimization page for basic set-up. Also see the jQuery doc page for a good way to set up your project, even if you are not using jQuery.

How can I provide a library to others that does not depend on RequireJS?§ 2

If you are building a library for use on web pages that may not use RequireJS or an AMD loader, you can use the optimizer to combine all your modules into one file, then wrap them in a function and use an AMD API shim. This allows you to ship code that does not ship with all of RequireJS, and allows you to export any kind of API that works on a plain web page without an AMD loader.

almond is an AMD API shim that is very small, so it can be used in place of require.js when all of your modules are built into one file using the RequireJS optimizer. The wrap build config option will put a function wrapper around the code, or you can provide your own wrapper if you need to do extra logic.

See the almond project for details on how to build with the API shim and with wrap.

If you need to dynamically load code after a build, then using almond+wrap will not be sufficient as almond cannot dynamically load code. Instead, you may want to namespace your use of require/define. See next section.

How can I namespace my code to play well in other people's pages?§ 3

If you want to provide your code to web sites that may not use an AMD loader, and you need to dynamically load code, doing a simple one file build with a wrapper is not enough. You also may want isolate your loading needs from the page's AMD loader.

There is a namespace build option that does the following:

  • Renames requirejs, require and define uses to have "namespace." in front of them.
  • If the file does an existence check for define, in the following form typeof define === 'function' && define.amd, then it will prefix the define references with "namespace.".
  • If require.js is included in the built file, it will make sure it exposes the "namespace." versions of the API.

Do not code your source with namespace.require()/namespace.define() calls, but rather use require()/define() as you normally would, then use the optimizer to do the renaming.