Modules/Transport/C
From CommonJS Spec Wiki
STATUS: PROPOSAL
Contents |
Format
Module format based on Transport B with some aspects of Transport A
Main difference with Transport B:
- use positional arguments, this allows for less hand-coded typing, so it could be hand-coded by front end developers. It also less indentation since the factory function is two levels shallower than Transport B.
- Makes the "require", "exports" and "module" explicit dependencies if they are used in the module definition function, and allows dependencies to be passed as named arguments to the definition function. "require", "exports" and "module" should be optional since not all modules need them, and explicitly specifying them as dependencies and function arguments makes it easier to transform existing CommonJS modules to this format.
- Avoids deepDependencies, not clear of its value. Better to leave it off to make the spec simpler.
require.def("ID", [ARRAY_OF_DEPENDENCY_MODULE_IDS], function () {
//This is module definition function.
});
Examples
Using require and exports
Sets up the module with ID of "alpha", that uses require, exports and the module with ID of "beta":
require.def("alpha", ["require", "exports", "beta"], function (require, exports, beta) {
exports.verb = function() {
return beta.verb();
//Or:
return require("beta").verb();
}
});
Transporting more than one module at a time
Additional methods on require() to pause dependency tracing until all modules call require.def():
require.pause(); require.def(); ... require.resume();
This approach allows for easier concatenation of files, and less code introspection.
Optional extensions
Allow the module definition function to return a value, and that value would be assigned as the exported value for the module, instead of requiring the use of exports.
Considerations for transport when used in the browser
- Requires modules to specify their name, because of browser constraints. It also makes bundling multiple modules in one file easier.
- Relative module names that start with "." and ".." are not supported in the module IDs, particularly if many modules are being transported from different paths.

