amdjs / amdjs-api

Houses the Asynchronous Module Definition API
https://groups.google.com/group/amd-implement
4.31k stars 499 forks source link

"dependencies" section lacks details #15

Open knutkj opened 10 years ago

knutkj commented 10 years ago

I think that the "dependencies" section is missing some details. What is special with the dependency name "exports"? What should happen if a module definition lists the "exports" dependency, and at the same time its factory function returns a value? Here is an example of that behavior:

https://github.com/Reactive-Extensions/RxJS-DOM/blob/921c35a28265466cda7fc2dcdbacfe688efcd456/rx.dom.js#L25

Should the return value of the factory function be used in this case, or should you save the "exports" object as the module value?

ghost commented 10 years ago

You asked:

What should happen if a module definition lists the "exports" dependency, and at the same time its factory function returns a value?

Answered here:

If the factory function returns a value (an object, function, or any value that coerces to true), then that value should be assigned as the exported value for the module.

This means the exports module will be ignored.


AMD has a priority for the different exports variations (low - high):


The modules ["require", "exports", "module"] are based on the CommonJS modules specification.

Example of CommonJS module:

var foo = require("foo"),
    bar = require("bar");

exports.foo = foo;
exports.bar = bar;

Convert it to AMD:

define(["require", "exports", "module", "foo", "bar"], function (require, exports, module) {
    var foo = require("foo"),
        bar = require("bar");

    exports.foo = foo;
    exports.bar = bar;
});