-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Fix generated code order when using TypeScript, ES6 and decorators #7293
Fix generated code order when using TypeScript, ES6 and decorators #7293
Conversation
|
Only ran a subset of tests locally (as they take so long); will investigate those. |
Thanks @thewilkybarkid for doing this. Hopefully it will fix #6676 so I can finally upgrade to Parcel 2 and still support certain legacy browsers! |
Not sure if this entirely safe to do, circular imports might be problematic and also:
|
Are you able to write a test that breaks? |
@mischnic As far as I'm aware the export order is still fine (it's just at the end of the module). The test I added contains circular imports too, but maybe you had a different idea. I've just tried this out on a real project and it works fine, but still has the same problem with scope hoisting enabled (as the order is changed back)... |
That just came to mind because we had problems with this in the past as well. I haven't had time yet to try and test such a situation |
Here's an example: a.mjs import { c } from "./b.mjs";
console.log(c); b.mjs export const foo = "foo";
export { c } from "./c.mjs"; c.mjs import { foo } from "./b.mjs";
export const c = "c:" + foo; When run, this should throw "Cannot access 'foo' before initialization" (as it does when running the modules natively in Node or browsers). However, after applying this patch, it logs "c:undefined". |
Thanks @devongovett, I've opened #7350 with that test case. I'll move this back to draft and will keep looking. |
I've just been able to find angular/angular@401ef71 and microsoft/TypeScript#27519 which seem to describe this problem: it's a TypeScript limitation. I'm not sure how it was working in Parcel Not sure what to do with this problem now though. (The project where I've had this problem is inherited, and I've never used decorators before as they are known to be experimental. This gives me the excuse to remove them.) |
I think the reason it may have "worked" in a previous beta is because we weren't spec compliant yet. Are you sure this circular dependency works with normal TSC or Babel? |
Seems to work with TSC: I've expanded https://github.com/thewilkybarkid/parcel-access-before-initialization to show a failure with Parcel but success with TSC. |
Decorators are an experimental feature in TypeScript, which is known to have issues. Changes in Parcel means that it can no longer build the app, so their use is blocking updating Parcel to a stable release. This change removes the ORM decorators in favour of defining entities programmatically. I'm hoping that the tests and migration-creating script are enough to have caught any mistakes, but given that areas of the app remain untested, it's hard to be sure. Also, this change has to disable Parcel's scope hoisting, as it renames the entity classes. The rename wasn't a problem when using decorators as the generated code saw the class name left unchanged. Now, however, the ORM breaks if they are changed. Refs #399, parcel-bundler/parcel#7293 (comment), microsoft/TypeScript#27519
↪️ Pull Request
This fixes the problem I mentioned in #6676 (comment). I'm not sure I understand the actual problem, but it's something to do with the hoisting difference between
var
andlet
, and decorators. (I didn't manage to recreate it without decorators.)Moving the call to
parcelHelpers.export()
to the end (i.e. after thelet
) seems to resolve the problem.💻 Examples
I've managed to reproduce the problem in https://github.com/thewilkybarkid/parcel-access-before-initialization, where you can see the compilation result and the failure. (It's basically the same as the test case in the PR.)
🚨 Test instructions
✔️ PR Todo