Fork me on GitHub

Exporting Modules

Declarative web global rootExports and noConflict()

uRequire allows you to easily export root (global) variables, along with noConflict() on the Web side.

Simplified rootExports

uRequire can generate the rootExports boilerplate. You can declaratively export one (or more) global variables from your UMD/AMD module on the web side.

You simply include an object literal on the top of your (source) module file like this:

({urequire: { rootExports: 'uBerscore' } });

Or use an array :

({urequire: { rootExports: ['uBerscore', '_B']}});

in case you want many global vars.

These globals be created as keys on 'root' (eg window on browsers, global on nodejs), with the module as the value (possibly overwriting existing keys).

Alternativelly, if you dont want to pollute your modules with exporting info, you can handle it in the config, in the bundle.dependencies.exports.root key.

Generated noConflict functionality

You can inject code to your module, that provides the familiar jQuery inspired noConflict() function to your module. With noConflict enabled, your module saves the existing root keys before exporting onto them. Then it can return them to their original value, if you call noConflict() which in turn returns the module.

Again, you do this declaratively:

  urequire: {
    rootExports: ['_B', 'uBerscore'],
    noConflict: true

At some point in your code, you can call var myB = _B.noConflict(); which will revert all rootExports variables to their original values, returning the module which you can store elsewhere (eg myB).

In future uRequire versions you'll be able to a) pass an 'exclude' array param & b) do a rootExports() to re-export root vars.

dependencies.exports.bundle, a dependencies-injection mechanism:

With dependencies.exports.bundle config option you can declare bundle-wide global-looking dependencies, that work on Web & nodejs. These are implicitly available in all your modules, without repeating the requires.

For example, if you use ['lodash', 'backbone', 'myLib', ...] in all your bundle modules, just use

  exports: bundle: ['lodash', 'backbone', 'myLib']

and its saving you from having to require 'em in every module of your bundle.

If you want to have precise control over the variables that hold you exported modules, use this format:

dependencies: exports: bundle: {
    'lodash': '_',
    'backbone': 'Backbone',
    'myLib': ['myLib', 'myLibOtherName']

Most times, uRequire can discover the variable names, if its 'define'd even once.