-
Notifications
You must be signed in to change notification settings - Fork 12.8k
ES6 module variables are copied when re-exported #6366
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
Comments
@mhegazy we should discuss this ASAP (Friday?) |
Isn't this "Bug" instead of "Suggestion" ? :) For re-exports from modules with relative paths, TS could avoid the tax of getters and maintain ES3 support by rewriting the import of the re-export to use the original export, eg Edit: To clarify, the codegen for the re-export itself would still have to use a getter defined on |
This issue has been dormant since 2016, but it's still valid. And it seems like it really should be labeled as a bug. As far as I can tell, re-exports are supposed to be live views in ES6, and a re-exported variable is supposed to reflect changes. Looking at the code generation examples here, TypeScript exports map directly to ES6/ES 2015 modules when targeting ES 2015, so they will have the correct ES6 semantics in that case, right? Which makes it even more undesirable for them to have different semantics when transpiling to ES3. At any rate, regardless of the current behavior of different compilation targets, it seems like the behavior of TypeScript modules should eventually align with that of ES6 modules. One advantage of having native module support should be that there is a common, clearly defined semantics for module import/export. |
I'm running into this bug while using the barrel pattern, i.e. in an export * from "./Entity1";
export * from "./Entity2";
export * from "./Entity3"; Then later: import { Entity1, Entity2, Entity3 } from "entities";
someFunction() {
const entities = [Entity1, Entity2, Entity3]
// Entity3 is undefined
} If I change it to: import { Entity1, Entity2 } from "entities";
import { Entity3 } from "entities/Entity3"; Then it works and I do have some cross-entity imports (i.e. Entity3 pulls in Entity2) that is triggering a circular loading issue, but, once my Also, I'm technically running tests via |
Given this example:
a.ts
b.ts
c.ts
tsconfig.json
Result of runing c.js:
This happens because in b.ts this:
transpiles to
and the value of variable a is copied and not referenced.
If I run the same example through babel it works because it generates getters that references the original variable. This was also proposed here.
I propose that if the target is set to es5 then the generated code for commonjs modules uses getters instead to make sure that re-exported variables are referenced instead of copied.
The text was updated successfully, but these errors were encountered: