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

Namespace object of reexported external star export #2165

Closed
guybedford opened this issue Apr 29, 2018 · 16 comments
Closed

Namespace object of reexported external star export #2165

guybedford opened this issue Apr 29, 2018 · 16 comments

Comments

@guybedford
Copy link
Contributor

guybedford commented Apr 29, 2018

Currently external star exports are not supported when reexported through namespace objects -

https://rollupjs.org/repl?version=0.58.2&shareable=JTdCJTIybW9kdWxlcyUyMiUzQSU1QiU3QiUyMm5hbWUlMjIlM0ElMjJtYWluLmpzJTIyJTJDJTIyY29kZSUyMiUzQSUyMmltcG9ydCUyMColMjBhcyUyMG0lMjBmcm9tJTIwJy4lMkZtYXRocy5qcyclM0IlNUNuJTVDbmNvbnNvbGUubG9nKG0pJTNCJTIyJTdEJTJDJTdCJTIybmFtZSUyMiUzQSUyMm1hdGhzLmpzJTIyJTJDJTIyY29kZSUyMiUzQSUyMmV4cG9ydCUyMColMjBmcm9tJTIwJ2V4dGVybmFsJyUzQiUyMiU3RCU1RCUyQyUyMm9wdGlvbnMlMjIlM0ElN0IlMjJmb3JtYXQlMjIlM0ElMjJjanMlMjIlMkMlMjJuYW1lJTIyJTNBJTIybXlCdW5kbGUlMjIlMkMlMjJnbG9iYWxzJTIyJTNBJTdCJTdEJTJDJTIyYW1kJTIyJTNBJTdCJTIyaWQlMjIlM0ElMjIlMjIlN0QlN0QlMkMlMjJleGFtcGxlJTIyJTNBbnVsbCU3RA==

I'd suggest we should output the following here instead:

'use strict';

var external = require('external');

var m = /*#__PURE__*/Object.freeze(Object.assign({}, external));

console.log(m);

effectively chaining Object.assign calls per reexported star exported module, in the precedence order, possibly with a final step to ignore the default export.

@lukastaegert
Copy link
Member

Sounds good 👍

@guybedford
Copy link
Contributor Author

The better output here to match semantics is of course:

import * as externalNs from 'external';

const ns = {
  export: 'name'
};
for (let p in externalNs)
  if (!(p in ns) && p !== 'default') ns[p] = externalNs[p];

Note that the for in is necessary as ES module namespaces don't provide hasOwnProperty(x) === true!

@lukastaegert
Copy link
Member

The better output here to match semantics is of course

Could you elaborate a little more in what way the previous approach is not spec compliant. In any case, the second approach would not require an Object.assign polyfill in old environments. We should still Object.freeze, though.

@guybedford
Copy link
Contributor Author

Object.assign, like Object.keys wont work on module namespace objects. In addition, the precedence conditional for not overwriting local exports or the default export is needed.

@lukastaegert
Copy link
Member

Thx

@guybedford
Copy link
Contributor Author

Just tested and object.keys does work, but I think browsers aren't implementing this correctly at the moment, I believe (although I may be wrong) these are supposed to fail as namespaces don't support [[OwnPropertyKeys]], but it's being discussed at the spec level - tc39/ecma262#1296.

@lukastaegert
Copy link
Member

See also #2489

@Westbrook
Copy link

🤞

@shellscape
Copy link
Contributor

@guybedford @lukastaegert thoughts on this one? Object.keys should be good to go in browsers since the OP date.

@guybedford
Copy link
Contributor Author

This is very much a still-valid spec compliance bug.

@lukastaegert
Copy link
Member

Agree. Just a note reading the above code, to my knowledge in case there is more than one namespace reexport, conflicting names should throw a specific error when accessed. Not sure if we would want to replicate this, though.

@lukastaegert
Copy link
Member

At least at the moment we throw an error, which is better than outputting nonsense.

@guybedford
Copy link
Contributor Author

Ah I missed that we now have an error here. This could be closed then I suppose.

@lukastaegert
Copy link
Member

Or we fix it 😉

@shellscape
Copy link
Contributor

I'll add this to the middle-aged bug board

@guybedford
Copy link
Contributor Author

This was resolved in PR #3474.

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

No branches or pull requests

4 participants