A Variation on ES Modules

Kevin Smith khs4473 at gmail.com
Wed Dec 19 11:26:56 PST 2012


> What you _do_ want to have IMO, though, is module aliases
>
>   module Short = Long.Qualified.Module.Name
>

I agree.


> OTOH, one more other feature I could consider dropping for the time being
> is the ability to export "from" a ModuleSpecifier. I'm not convinced that
> this is a common enough use case to warrant specialised extra syntax -- you
> can already express it by pairing an export with an import. (In fact,
> allowing the "export" keyword in front of imports seems like the more
> consistent way to support re-exporting.)
>

True, except for the case of `export * from "url";`, since we don't have
`import *`.

The use case for such a feature (as I see it), lies in creating a "nearby"
module that forwards the bindings from some other "distant" module with a
long URL.

Let me explain...

I feel pretty strongly that there should not be any blessed, centralized
registry for globally naming javascript modules (or packages, or
what-have-you).  Sorry, NPM, but javascript is different!  But at the same
time, in order to unambiguously express project-external dependencies,
global names have to be used.  Well, that's what URLs are for:

    // Importing from a project-external dependency:
    import x from "//my-js-registry.org/foo-1.2.3.4";

(Of course, the loader for your app will be clever enough to remap this URL
to a "local" server, or whatever you need.)

But if you have several modules in your project which reference this same
project-external module, then you'll end up having to repeat that long URL
several times.  To alleviate that pain, you can create a local forwarding
module:

    // foo.js
    export * from "//my-js-registry.org/foo-1.2.3.4";

Then we can use the local URL in our dependent modules:

    import something from "foo.js";

If we update the version of "foo", we only have to change the global URL in
one place.  Notice this provides us with the ability to specify truly
global dependencies without the need for "package manifests" or other
out-of-language facilities.

- Kevin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20121219/e24dac58/attachment.html>


More information about the es-discuss mailing list