tc39 / proposal-dynamic-import

import() proposal for JavaScript
https://tc39.github.io/proposal-dynamic-import/
MIT License
1.87k stars 47 forks source link

Named vs default imports? #21

Closed ljharb closed 8 years ago

ljharb commented 8 years ago

Let's say I have a module that can be imported like so:

import foo, { bar } from 'path';

How can I use import() to get both foo and bar? In the following code:

import('path').then((x) => {
  // what is `x` here? `foo`? `bar`? an object?
});
Ltrlg commented 8 years ago

The promise is resolved with the module namespace (2.1.1, step 8.c.iv of the current draft), i.e. x will be the same as in

import * as x from 'path'

You can get the same behaviour as foo and bar with x.default and x.bar, respectively.

Or by destructuring if you don’t require the live binding:

import('path').then(({default: foo, bar}) => { /* … */ })

matthewp commented 8 years ago

It's the namespace object, so there's a bar and a default. It's equivalent of the imperative import * as path from 'path'.

edit: @Ltrlg literally beat me by a second! :)

domenic commented 8 years ago

Seems like this has been answered.

ljharb commented 8 years ago

Thanks for clarifying!

knpwrs commented 7 years ago

Any chance somebody can provide a link to "2.1.1, step 8.c.iv of the current draft"? I'm looking for a link to use for https://github.com/webpack/webpack.js.org/pull/1109.