Do Anonymous Exports Solve the Backwards Compatibility Problem?

James Burke jrburke at gmail.com
Wed Dec 19 11:18:26 PST 2012


On Tue, Dec 18, 2012 at 12:56 PM, Kevin Smith <khs4473 at gmail.com> wrote:
> At first glance, it seems like anonymous exports might provide a way for
> pre-ES6 (read: Node) modules and ES6 modules to coexist.  After all:
>
>     exports = function A() {};
>
> just looks so much like:
>
>     module.exports = function A() {};
>
> But is that the case?  Does this oddball syntax actually help?
>
> My conclusion is that it does not, *unless* the loading environment is
> willing to statically analyze every single module it wishes to load.
> Moreover, the desired interop is not even possible without performing static
> analysis.

I feel this is mixing up backcompat dependency matching (which is has
much larger issues than exports assignment) with a preference to just
not have exports assignment. I believe the backcompat issues and
parsing things are workable. I have done some code experiments, but we
need a more info on the module loader API, specifically the runtime
API, like System.set/get before getting a solid answer on it.

exports assignment is not about backcompat specifically, although it
helps. Exports assignment is more about keeping the anonymous natures
of modules preserved. In ES modules, modules do not name themselves if
it is a single module in a file. The name is given by the code that
refers to that code.

If a module only exports one thing, and chooses a name, it is
effectively naming itself. Example from the browser world: jQuery and
Zepto provide similar functionality. If jQuery exports its value as
"jQuery", then that would mean Zepto would then need to export a
"jQuery" exports if it wanted to be used in places where the jQuery
module is used. But if someone just wanted to use Zepto as "Zepto",
then Zepto would need to add more export properties. Saying "well have
them both use $" is just as bad. It is simpler to just allow each of
them to export a function as the module value, and avoids these weird
naming issues.

Assigning a single exports also nudges people to make small modules
that do one thing.

It is a design aesthetic that has been established in the JS
community, both in node and in AMD modules, in real code used by many
people. So allowing export assignment is more about paving an existing
cowpath than a specific technical issue with backcompat.

James


More information about the es-discuss mailing list