Convert anonymous functions to named ones (in development) #60
eps1lon
started this conversation in
Feature Requests
Replies: 1 comment 1 reply
-
Interesting, the use of In general the compiler could try to synthesize names for functions, but note that in general code won't even have a |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
In Next.js, we currently transform anonymous functions in Hooks to named ones to provide better stack traces.
Before:
produces this stack (with Owner Stacks):
After:
produces this stack (with Owner Stacks):
With the React Compiler, we can no longer apply our current transform since it is not valid input for the React Compiler.
Adding names back after the Compiler is non-trivial.
If you just use the Compiler, you now also get named functions e.g. with the above example
produces this stack (with Owner Stacks):
The React Compiler could have a separate transform for development that adds these names by using the same approach Next.js does e.g.
This has the downside of creating temporal dead zones though this shouldn't affect runtime since none of the compiled code is executed during module evaluation.
Alternatively,
Object.defineProperty
can be used to keep the current hoisting behaviorNote that
_temp.name = '...'
is not possible sinceFunction.prototype.name
is not writeable.Detecting the pattern Next.js transforms to is also possible. Though we might not be able to leverage that depending on how our transpilation pipeline is structured. We wouldn't want to add another SWC pass right before Babel just for the names.
You can check out the fixtures in Next.js for this transform to get a better overview about how we currently determine the name of an anonymous function passed to Hooks.
Libraries can choose whether they want to ship with better names by default or ship separate entrypoints for dev and prod. For example,
react-redux
already ships named selectors with the same trick Next.js uses.Beta Was this translation helpful? Give feedback.
All reactions