feat(rsc): support custom server function directives#1246
Conversation
79377ac to
c44f424
Compare
205122e to
1aabe31
Compare
1aabe31 to
142fb07
Compare
commit: |
There was a problem hiding this comment.
Can you explain higher level motivation (than implementation explanation)? From a quick look, I cannot tell things like:
- whether this includes some fixes in existing
transformswrap/hoist and thus existing builtin"use server"support. - whether new plugin
vitePluginServerFunctionDirectivesneeds to be inside rsc plugin. for example, if we land changes intransforms/*, then can you buildvitePluginServerFunctionDirectivesoutside based on the exported transform utils? - whether
vitePluginServerFunctionDirectiveshas some new mechanism compared to current builtin"use server"support that also benefit for builtinuse serverto have.
Yeah, there's some improvements to how object & class methods and named expressions are treated, which the 'use server' directive will benefit from. The other part is how certain forms that can't be hoisted safely are handled and tracking metadata about parameters, which is mainly for custom directives to use. I can break those out into separate PRs if needed.
I believe we may be able to, however, the more I was looking at some of the 'use cache' directive stuff we were having to do as I started fixing bugs, the more it felt like we were touching more and more on the internals of how the plugin processes directives. By that, I'm referring to:
It felt a lot to me like we began implementing our own generic approach to register custom directives rather than there being an API for one, and in the process, attaching onto everything the RSC plugin was already doing internally.
The 'use server' directive could actually be changed to route through this new plugin as well. I was originally going to do that and have one single way for people to register custom directives that was also used for registering the 'use server' directive by default. The way this works was based on how that directive works as its starting point. Couldn't help but feel a tad risky without proving the new approach works well before doing that... |
5586d70 to
e539c58
Compare
Summary
Adds an experimental
serverFunctionDirectivesAPI for framework directives such as Next.js-style"use cache".This is additive to the existing
rsc:use-serverimplementation:vitePluginUseServertransform remains in placevitePluginUseServeris preserving custom manifest metadata during cleanup and merging custom export names when a module also has"use server"API
Reused functionality
The custom-directive plugin delegates to the same transform helpers used by
"use server"for:It also supports object methods, static class methods, destructured exports, named re-exports, stable custom hoist names, stateful regular expressions, and export filtering before async validation.