Skip to content
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

Export * and export default #328

Closed
jkimbo opened this issue May 10, 2016 · 6 comments
Closed

Export * and export default #328

jkimbo opened this issue May 10, 2016 · 6 comments
Labels

Comments

@jkimbo
Copy link

jkimbo commented May 10, 2016

I get the error Multiple default exports when trying to export everything from another file including the default export e.g.

export * from './foo';
export { default as default } from './foo';

It looks like that this case should throw an error (as shown by this test case) however babel compiles the code export * from './foo'; to:

Object.keys(_foo).forEach(function (key) {
  if (key === "default") return;
  Object.defineProperty(exports, key, {
    enumerable: true,
    get: function get() {
      return _foo[key];
    }
  });
});

Reference

so the second export is necessary to ensure the default export also gets exported.

Any suggestions? I'm not sure whether it's actually babel which is at fault here or if the assumption in this plugin is wrong. Happy to contribute a PR if a solution is agreed on.

@benmosher
Copy link
Member

benmosher commented May 10, 2016

Interesting. I think the spec dictates that imported/exported namespaces include the default name; it's not distinct from other exported names, it just has special extra syntax for exporting/importing.

So I would argue to start that Babel is doing it wrong (maybe in the interest of backwards-compatibility?).

edit: this is wrong, see my next comment. 😳

@jfmengels
Copy link
Collaborator

@jkimbo Are you running ESLint on the transpiled code? If you do, that could explain the error, but I don't see why you would do that.

@benmosher
Copy link
Member

benmosher commented May 10, 2016

@jfmengels: if ./foo contains a default export, it should currently does report Multiple default exports on both lines.

@jkimbo: http://www.ecma-international.org/ecma-262/6.0/#sec-getexportednames, 7.d.i. seems to be filtering default exports from export * from ... statements. As such, looks like this plugin is in error and Babel has it right.

IIRC I did this because export * as ns from '...' does include the default name binding, but that is from Lee Byron's export extensions proposal. I (apparently mistakenly) expected this to carry over to ES6 star exports.

@jkimbo
Copy link
Author

jkimbo commented May 10, 2016

Thanks for digging into it @benmosher ! I can attempt a PR to fix it (though not until tomorrow) if that would help?

@jfmengels
Copy link
Collaborator

@jkimbo Yes please, that'd be great :)

@benmosher
Copy link
Member

You're welcome to take a stab at it, but I think it goes pretty deep into the exported namespace tracking so it may get dicey. You may want to fail fast.

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

No branches or pull requests

3 participants