Modules/2.0 has *not* been abandoned in favor of AMD as AMD is only a
transport format for Modules/2.0. Not everyone is on board with that
(especially people writing mostly for the browser) but it has been
decided that AMD is not a compatible improvement over Modules/1.0 and we
are waiting for implementations and reviewers of Modules/2.0.
It has been a while since I looked at the details of the spec. I'll try
and answer as best as I can. You could also verify the intended behavior
by taking a look at the two loaders that implement Modules/2.0:
* http://code.google.com/p/bravojs/
* https://github.com/pinf/loader-js
There are a bunch of tests with these projects. I hope to get these
tests onto https://github.com/kriskowal/uncommonjs at some point.
> Let's call the module and declare variables in the extra-module
> environment "global.module" and "global.require," while the
> unqualified versions refer to the appropriate parameters to the module
> factory function. Then our questions are:
>
> 1. Can you call module.declare, or only global.module.declare? If the
> former, how is that supposed to work; if the latter, how should this
> be prevented?
`global.module.declare` is used in browser environments only so modules
loaded via script tags can declare themselves.
`module.declare` is used in server environments where the loader
provides a new `module` variable to each module being loaded to allow
the module to declare itself asynchronously.
Server environments should not have a `global.module.declare` unless you
have a bootstrap mode that executes some code outside of a module
context initially.
Both `module.declare` and `global.module.declare` may only be caled once
per module ID.
> 2. What is global.module.dependencies? global.module.id is specified,
> to be undefined, but not dependencies.
Should be undefined as well I believe. Only `global.module.declare`
makes sense.
> 3. If you call global.module.provide(["a"], function () { /* ... */ })
> in the EME, global.module.declare will be called for the first time
> ever, declaring the "a" module. Furthermore, if inside the provide
> callback, you call global.require("a"), the factory function for "a"
> will be necessarily run. How does this fit with the requirement
> (2.3.1.1) that the main module's factory function is invoked before
> any other module's? Does "a" become the main module? If so, what
> happens when global.module.declare is called in EME? Should
> global.module.provide even exist?
Not sure. I only deal with the EME during bootstrapping and don't recall
all the logic behind it. The BravoJS loader is probably the best place
to look.
> 4. Should module.provide accept a dependency array in the sense of
> section 3.5, i.e. one which can contain labels? The sentence "The
> dependency list format is described in �3.5" seems to indicate so.
Labels are not supported at that level.
> 5. If 5, should calling module.provide "update the knowledge" of its
> counterpart require to reflect these new labels? (Otherwise they would
> be useless.) Should module.dependencies be updated to reflect this
> knowledge, or does it only reflect the original dependencies array
> passed to global.module.declare? Similarly, do labels introduced by
> global.module.provide update the knowledge of global.require?
The signature is not:
module.provide ([], factory)
but:
module.provide ([], callback)
Meaning that `callback` is a callback with no arguments and not a
factory function. `module.provide` is simply a means to wait for the
dependencies to be loaded. It is not a way to declare a module.
Hope this helps.
Christoph
However, we've run into a few questions while deciphering the spec
that we want to get clarified before calling ourselves "done." We
tried emailing Wes about this directly, but with no response from him,
we hope that maybe this community could be more helpful. (Although,
looking at the archives, has the community abandoned Modules/2.0 in
favor of AMD? It's not entirely clear.)
Let's call the module and declare variables in the extra-module
environment "global.module" and "global.require," while the
unqualified versions refer to the appropriate parameters to the module
factory function. Then our questions are:
1. Can you call module.declare, or only global.module.declare? If the
former, how is that supposed to work; if the latter, how should this
be prevented?
2. What is global.module.dependencies? global.module.id is specified,
to be undefined, but not dependencies.
3. If you call global.module.provide(["a"], function () { /* ... */ })
in the EME, global.module.declare will be called for the first time
ever, declaring the "a" module. Furthermore, if inside the provide
callback, you call global.require("a"), the factory function for "a"
will be necessarily run. How does this fit with the requirement
(2.3.1.1) that the main module's factory function is invoked before
any other module's? Does "a" become the main module? If so, what
happens when global.module.declare is called in EME? Should
global.module.provide even exist?
4. Should module.provide accept a dependency array in the sense of
section 3.5, i.e. one which can contain labels? The sentence "The
dependency list format is described in §3.5" seems to indicate so.
5. If 5, should calling module.provide "update the knowledge" of its
counterpart require to reflect these new labels? (Otherwise they would
be useless.) Should module.dependencies be updated to reflect this
knowledge, or does it only reflect the original dependencies array
passed to global.module.declare? Similarly, do labels introduced by
global.module.provide update the knowledge of global.require?
That is not consistent with the BravoJS implementation I believe.
Looking at the code it seems to be simply a way to wait for a bunch of
modules to be loaded before the callback is triggered.
See:
* http://code.google.com/p/bravojs/source/browse/bravo.js#518
* http://code.google.com/p/bravojs/source/browse/bravo.js#222
I think it makes sense the way it is implemented. If you want labelled
dependencies use `module.declare`.
Christoph
I think that should make it into the wording of the spec.
Christoph