Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

"dependencies" section lacks details #15

Open
knutkj opened this issue Feb 26, 2014 · 1 comment
Open

"dependencies" section lacks details #15

knutkj opened this issue Feb 26, 2014 · 1 comment

Comments

@knutkj
Copy link

knutkj commented Feb 26, 2014

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
Copy link

ghost commented Apr 5, 2014

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):

  • if ["exports"] is in the dependencies list exports will be exported
  • if ["module"] is in the dependencies list module.exports will be exported, note that module.exports and exports are the same object if module.exports is not reassigned within the factory
  • if factory function returns a value (an object, function, or any value that coerces to true) it will be exported

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;
});

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant