-
Notifications
You must be signed in to change notification settings - Fork 10.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
perf(gatsby/babel): Optimize hook destructuring #22619
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
um, ok :)
@blainekasten We have section on Gatsby internals that it could fit well in! We recently updated the webpack config with @wardpeet's new chunking functionality. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this as separate package or can it be just a file inside our gatsby-preset-gatsby
? As soon as we release this is separate package we defacto become maintainers for this? (If there is no standalone package like this already).
So I'm not certain we need this at all... I looked at the output from next when you build and have a useState: It compiles to this:
Which is peculiar, because it's different than the test that is written in general. But then I tested my changes with gatsby and I got the same results. Seems great, but then I tested against gatsby master and got the same results So I'm not sure if this plugin in general is just not doing what it's supposed to do for us or next, or the way it's working means we don't actually need this plugin for some reason. cc @developit for advice because I'm not really sure what the aim still is. Do we want to see |
This only affects modern output where destructuring is preserved, or legacy output when Babel is in non-loose mode (example). Babel's loose mode output already dissolves destructured arrays (example) and treats them as objects. It's safe to apply this optimization for both modern and non-modern output, but it will only provide value in modern output. |
Just to clarify, here's a breakdown: Input: const [count, setCount] = useState(0); Output: (non-modern / ES5) (note that this is identical to the output without hook destructuring optimization) var _temp = Object(Q.useState)(0),
count = _temp[0],
setCount = _temp[1]; Output: (modern) var { 0: count, 1: setCount } = Object(Q.useState)(0); |
@developit Thanks for the clarification :) I was talking to @wardpeet shortly before you posted this and realized exactly what you described. Good news, I tested this in a gatsby project by creating a .babelrc with this config:
and saw the expected object destructured from the plugin. So I would say this plugin is ready to be merged, even though the value it gives won't be seen until we ship modern builds (soon!) |
@laurieontech I think I'm going to skip adding to docs for this one. This is a very small benefit that users don't really need to think about or know about. modern builds will be a more applicable effort to document. Let me know if you disagree 👍 |
|
@blainekasten I think that's ok given the changes. |
Errors for the build are unrelated to this. Let's merge it. |
@blainekasten @pieh @developit I am not completely sure whether it is babel issue or my/gatsby misconfiguration. But after updating gatsby this optimization broke my build, because of the following syntax: let [{ someProp, ...rest }, setState] = useState() I assumed that this is a babel plugin issue (even though I wasn't able to reproduce it in babel repl). |
---edited Ignore this comment I just tried yours and what our current babel transform generate is (check snapshot):
So theoretically it should be fine? What's exact error you are seeing? Is it actually babel or syntax error or runtime error? Also do you actually use the code as is - are you not passing any initial state value? I think it would be Also as sidenote - we diverged a bit from initial implementation in this PR already because of #23438 (but this doesn't seem to affect your example) |
Ah, ok I just checked your PR in babel - yeah - seems like it you accurately found real culprit and seems like your PR already getting approvals, so it's bit of our hands (other than maybe trying to temprorily disable this transform while we wait for new release for Sorry for sending previous comment before checking on the linked PR (please ignore :P) |
Description
Thanks to @developit for giving us a great rundown of the benefit of this feature and also shipping it to next.
I took the liberty to make it into a package so other ecosystems and companies outside of Gatsby/next could benefit from it. Is there a problem with this approach?
Documentation
Do we document our babel setup anywhere? or micro optimizations we do? cc @gatsbyjs/learning
Related Issues
#19943