Load CommonJS modules without needing to convert them to AMD.
AMD and CommonJS are actually incredibly similar formats so this plugin is only 10 lines long.
To load a CommonJS module in RequireJS simply do -
require(['cjs!my-commonjs-module'], function(mymodule) {
});
If your module looks like:
var someDep = require('a-dependency');
exports.out = 'asdf';
Then it is dynamically converted into:
define(function(require, exports, module) {
(function() {
var define = undefined; // ensures any amd detection is disabled
var someDep = require('cjs!a-dependency');
exports.out = 'asdf';
})();
});
which is the AMD module format.
Dependencies are naturally handled by referring back to the plugin.
Supports:
- Cross-origin dynamic loading
- Builds
- Precompilation with the
optimizeAllPluginResources
r.js build option - Amazingness
Built with the AMD-Loader plugin helper.
volo add guybedford/cjs
bower install cjs
If not using package management, ensure that the AMD-Loader plugin is installed.
If using bower, include the map or paths configurations:
paths: {
cjs: 'cjs/cjs',
'amd-loader': 'amd-loader/amd-loader'
}
MIT