External and internal modules

Sam Tobin-Hochstadt samth at cs.indiana.edu
Mon Sep 9 06:26:59 PDT 2013


On Mon, Sep 9, 2013 at 3:33 AM, Dmitry Soshnikov
<dmitry.soshnikov at gmail.com> wrote:

> 2. As I understand, this `module "foo" { ... }` way of defining is either for inner modules (inside a parent external modules), or inside an external file which is not a module, but a script (and therefore, cannot be loaded as module).
>
> The first case isn't that useful -- I defined an inner module inside a parent external module, and then what? -- import it exactly in the same file? What is that mean? Naming collisions happen not that often inside a module (this is why it's a module), that sub-devide it on sub-modules.
>
> Or can I probably export this inner submodule from the parent? How this double import will look like, if it's even possible? First import the inner module form the parent, then bindings from the inner?

There are no nested modules.

> If the parent file is not actually a module, but a script -- how this script is loaded? On the web using <script>? On Node.js? -- using ..."require"? Two module systems?

Bundled collections of modules are loaded primarily by loading them or
referencing them using a module loader, and then importing from the
individual modules they define.

> 3. For external modules, will it be possible to omit filename extension? `import foo from "foo.js"` -- sounds obsolete, unless we'll be able to import modules written on different languages.

In the browser (this is not intended to be part of the language
semantics), the default rule for going from a module name like "foo"
to a URL like "http://example.com/foo.js" will add ".js".

> 4. What is export from module?
>
> import {foo} from "foo.js";
> export {foo} from "foo.js"
>
> First makes sense to me (well, syntax is debatable, but anyways), the second I don't understand. Can I, as a caller, re-export (i.e. make public) something from not my module, and that was specially not exported?

No, you can't.  The latter means the same as:

    import { foo } from "foo";
    export { foo };

except without adding `foo` to the local scope.  It doesn't expose
anything unexported.

Sam


More information about the es-discuss mailing list