Simple Modules: lazy dependency evaluation

Wes Garland wes at
Thu Jan 27 06:38:18 PST 2011

On Wed, Jan 26, 2011 at 5:04 PM, Brendan Eich <brendan at> wrote:

> CommonJS may do that on the server side, assuming fast enough file i/o.
> It's not necessarily a good idea even there (Ryan Dahl has talked about
> this). On the client, it's right out, which is why client-side CommonJS-like
> module systems require a preprocessor or else a callback.

There is work under way at CommonJS to rectify this problem. A few different
proposals have emerged, which all have approximately the same theme:

   -  Explicitly decouple exports-lookup and module loading.
   -  require() remains the exports-lookup interface
   -  Introduce a way to know which modules are needed by a program, such as
   the explicit declaration of dependencies, or static analysis of the source
   code looking for require() statements.
   - Before the main module is executed, load all dependencies (recursively)

Adding a mandatory function wrapper to the module source code also allows
these modules to be loaded and executed directly by DOM script tag injection
(an important technique as XHR has cross-domain restrictions); the function
wrapper also provides a convenient place to hang dependencies.

Here is what a module in one proposal (Modules/2.0-draft7) looks like:

module.declare([*"lib/dialog"*], function(require, exports, module) {
  /* Any valid Modules/1.1.1 modules goes here */
  require(*"lib/dialog"*).notify("hello, world");

(Oh -- main-modules are modules which are executed automatically by the
CommonJS host environment; e.g. by exec(3) shebang, HTML script tag, or
other mechanism; the mechanism is not part of the specification)

I should also say that all the proposals also have a way to explicitly load
a particular module at run-time, rather than via the dependency graph.  The
interface specifies a module name (or names) and a callback. Once the module
and its dependent modules are loaded, the callback is executed. This lets us
do lazy-loading, like Simple Modules loaders, without breaking

On Wed, Jan 26, 2011 at 6:25 PM, Kam Kasravi <kamkasravi at> wrote:

> Are you guys following modules 2.0 at all that seems to be a parallel
> universe of sorts under co mmonjs?

Full disclosure - I am the principle author of that document. It is one of
the proposals mentioned above. Its current status is "pompously-named
document designed to get attention" - it is not a standard, and carries only
the weight of the paper it is printed on. FWIW, it discusses much more than
the CommonJS module system -- it also attempts to nail down the execution
environment.  That said, there is no way Modules/2.0 belongs under
consideration by TC39; it is a "best-effort with limited tools" proposal;
Simple Modules gets to use new tools.

On Wed, Jan 26, 2011 at 5:40 PM, David Herman <dherman at> wrote:

> Just to flesh this out a bit: simple modules were designed to make it
> possible to import and export variables into lexical scope, and to be
> compatible with checking valid imports and exports statically, as well as
> being able to check for unbound variables statically. Including, for
> example, in the case where you say
>    import M.*;
> Moreover, they are designed to allow loading modules *without* having to
> use callbacks, in the common case where they can be pre-loaded at
> compile-time.

James' query comes as attempt to understand design decisions made for Simple
Modules with respect to the timing of the evaluation of the module factory
function.  With loading and exports now decoupled, the question becomes --
when does require actually evaluate the module body?  The balancing point
seems to be "Is it worth breaking backwards compatibility on existing
platforms in order to try and mimic Simple Modules?".

"Breaking backwards compatibility", in this case, means evaluating the
factories eagerly, as they are loaded into the environment as the dependency
tree is satisfied (before the main module runs). Currently, factories are
executed as side-effects of the first require() call (per module - our
modules are singletons).  This timing is important, as factories have
observable side effects (consider the module above).

So, what we're talking about is not just the Simple Modules loader, but the
static declaration form as well.  Static declaration is analogous to loading
a list of modules which is comprised of the main module and its recursive
dependencies and then executing the main module.

FWIW - my take on this is that porting CommonJS to Simple Modules is going
to require a code-audit anyhow; I believe that breaking backwards
compatibility within the CommonJS family is not worth trying to mimic such a
small sliver of Simple Modules semantics. There are much larger mismatches
(singletons, module-keyword, lexical scope, require() to name just a few)
which make it a moot point IMO.

Kris Kowal's query is interesting: is lazy evaluation worth considering for
Simple Modules?

   module M {
       export var foo = 42;
       export function bar() { return foo; }
       alert("hello, world");

In the example above, the alert statement would occur when the first import
from M statement were executed, rather than when the page containing the
<SCRIPT type="es-next"> were loaded.


Wesley W. Garland
Director, Product Development
PageMail, Inc.
+1 613 542 2787 x 102
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list