New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explain scope of imported names? #8

Open
cowboy opened this Issue Jul 3, 2014 · 3 comments

Comments

Projects
None yet
3 participants
@cowboy

cowboy commented Jul 3, 2014

Right now, it's not clear whether or not I can import inside a function or how imported names are scoped. Does it follow the same rules as var? Or let? Or is it per-file?

@wycats

This comment has been minimized.

Show comment
Hide comment
@wycats

wycats Jul 3, 2014

Owner

Good point.

The answer is that imports and exports are only top-level. This disallows:

if (Math.random() > 0.5) { import foo from "bar"; }

If you need a dynamic load, you can do:

this.import("../foo").then(function(foo) {
  // do stuff here
});

in ES7, you will be able to use await syntax (inside an async function, but also likely at the top-level of a module):

var foo = await this.import("../foo");
Owner

wycats commented Jul 3, 2014

Good point.

The answer is that imports and exports are only top-level. This disallows:

if (Math.random() > 0.5) { import foo from "bar"; }

If you need a dynamic load, you can do:

this.import("../foo").then(function(foo) {
  // do stuff here
});

in ES7, you will be able to use await syntax (inside an async function, but also likely at the top-level of a module):

var foo = await this.import("../foo");
@wycats

This comment has been minimized.

Show comment
Hide comment
@wycats

wycats Jul 3, 2014

Owner

The general goal is for people to use the declarative form as much as possible, which makes building and downloading the dependency graph possible up-front. The dynamic form can be used as an escape valve if necessary, but becomes async for obvious reasons.

One pattern I see a lot that leads to conditional imports in Node is attempting to load one module, and then falling back to another. I would anticipate doing that via Loader configuration (the app author could alias graceful-fs to fs for example). At that point, modules would just load in fs and let the app author decide what that means.

This kind of thing is how polyfills work in the browser today (replacing outdated or missing "modules" with beefed up versions).

Owner

wycats commented Jul 3, 2014

The general goal is for people to use the declarative form as much as possible, which makes building and downloading the dependency graph possible up-front. The dynamic form can be used as an escape valve if necessary, but becomes async for obvious reasons.

One pattern I see a lot that leads to conditional imports in Node is attempting to load one module, and then falling back to another. I would anticipate doing that via Loader configuration (the app author could alias graceful-fs to fs for example). At that point, modules would just load in fs and let the app author decide what that means.

This kind of thing is how polyfills work in the browser today (replacing outdated or missing "modules" with beefed up versions).

@UltCombo

This comment has been minimized.

Show comment
Hide comment
@UltCombo

UltCombo Oct 15, 2014

@wycats does this.import() still exist? Or should we just use System.import() now?

UltCombo commented Oct 15, 2014

@wycats does this.import() still exist? Or should we just use System.import() now?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment