This provides implementations of Asynchronous Module Definition's define()
and require()
functions. The minified code comes out around 550 bytes.
This enables module definitions, but it doesn't actually fetch any modules that are missing. If you want to automatically fetch modules, see "Extending" below.
The "CommonJS"-style hacks are not supported (e.g. special behaviour for dependencies "module"
, "require"
or "exports"
).
// module definition
define('my-module', ['some-dep'], function (someDep) {
// you can also use require() synchronously
assert(someDep === require('some-dep'));
return {/* some API */};
});
// Asynchronous require()
require(['my-module'], function (myModule) {
...
});
If you want to plug into this to make a full-featured module loader, then you can hook into a few special private properties:
This is an array of pending modules, as triples [moduleName, deps, factory]
. The first (moduleName
) may be null
, indicating it's an asynchronous require()
callback, not a module definition.
This should be a function you define. When present, it is called after every define()
or asynchronous require()
call - this would be a good time to inspect define._p
and attempt fetching of missing modules.