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
re-export module and override single export function #1031
Comments
That spec point is referring to things like: export let a = 'sadf';
export function a() {
} Which is a syntax error. The algorithm in http://www.ecma-international.org/ecma-262/6.0/#sec-getexportednames is designed so that local exports DO take priority. That is, your second code example should work, but just needs to be written:
Note though that |
I noticed that there is a difference in behavior depending on how I write the export. If I write the module like this: export * from './A'; // has a and b
function b() {
console.log("I'm b from project");
}
export { b }; Then b is overwritten. which is nice. However, when I write it like this: export * from './A';
export function b() {
console.log("I'm b from project");
} Function b is not overwritten. Shouldn't these two export statements behave in the same way? I found the reason for this in the system.js source code, it is simply a matter of which function gets assigned to the export object first (thereby later being overwritten by the other one). The bigger question here is: Is overwriting exports like in the first example a feature or a bug? We'd like to know if we can depend on it in the future, because it would be nice to be able to override exported functions. |
@peterjuras it's a feature, and as mentioned above it is not consistent in Babel's transpilation output. Traceur has what I believe to be the correct implementation of the module syntax edge cases though. |
(I'd suggest posting a Babel issue if this is something you would like to be prioritised, although unfortunately I personally have limited resources to work on Babel's implementation, but can advise) |
@guybedford thanks for the clarification. I will file a Babel issue. |
I think this Babel issue is related: https://phabricator.babeljs.io/T2438 |
Yes, note that it is module output format specific though... I believe that conversation is wrt the ComonJS output. |
posted a new issue: https://phabricator.babeljs.io/T6967 |
I have
module-a
,module-b
andmodule-c
.module-b
exports all ofmodule-a
and only changes one export. After some research and questioning on Stack Overflow the following solution was proposed (http://stackoverflow.com/questions/34745110/es2015-re-export-module-and-override-single-export-function-of-the-re-exported-m)module-a
module-b
module-c (test suite)
The down side of this approach is a forced use of default exports with objects.
My ideal solution would be to use the new
export * from 'module-a'
expression.module-b
module-c (modified test suite)
The reasoning behind this is the
first come first serve
principle. The first explicitexport b
gets a higher priority than the second implicit export of b inexport * from 'module-a'
.But this solution doesn't work because of the implicit export of b in
module-a
and explicit export of b inmodule-b
. (http://www.ecma-international.org/ecma-262/6.0/#sec-module-semantics-static-semantics-early-errors:'It is a Syntax Error if the ExportedNames of ModuleItemList contains any duplicate entries.'
)The advantage of this solution is you don't need a default export object.
Is there any other possibility to this problem besides submitting a proposal to change the ECMAScript spec or using
export default
with an object?The text was updated successfully, but these errors were encountered: