CommonJS defines its own module format, although it is not supported by eXo Platform. The adapter format can be used to adapt CommonJS modules to work well in eXo Platform.
Here are two simple CommonJS modules:
math.js
exports.add = function() { var sum = 0, i = 0, args = arguments, l = args.length; while (i < l) { sum += args[i++]; } return sum; };
increment.js
var add = require('math').add; exports.inc = function(val) { return add(val, 1); };
CommonJS modules use their required function which conflicts with the RequireJS same function. So, to
make it work in the AMD enabled environment, these modules need to be wrapped and injected predefined modules: require
,
exports
and
module
provided by Requirejs (See the details
here).
eXo Platform will wrap the code basing on the configuration using the adapter format:
<module>
<name>math</name>
<script>
<adapter>
define(["require", "exports"], function(require, exports) {
<include>/commonjs/math.js</include>
});
</adapter>
</script>
<depends>
<module>require</module>
</depends>
<depends>
<module>exports</module>
</depends>
</module>
<module>
<name>increment</name>
<script>
<adapter>
define(["require", "exports", "math"], function(require, exports) {
<include>/commonjs/increment.js</include>
});
</adapter>
</script>
<depends>
<module>require</module>
</depends>
<depends>
<module>exports</module>
</depends>
<depends>
<module>math</module>
</depends>
</module>