next.js
9471fb85 - Improve Server Actions compiler (#58391)

Commit
2 years ago
Improve Server Actions compiler (#58391) Currently to make inline-defined Server Actions work, the compiler hoists the actual `"use server"` function to the module level and convert the inlined function to a parentheses expression that creates a noop wrapper function and wraps it with the proxy. This works fine however expressions are still different from declarations (#57392). So there're some details that can't be aligned well. With this change, we're going to make the compilation for the two types of inline-defined Server Actions more robust and more lightweight: #### 1. Expressions ```jsx const action = async () => { "use server" ... } const action = async function () { "use server" ... } const action = async function named () { "use server" ... } foo={async function () { "use server" ... }} ... ``` These expressions can directly be replaced with a new expression `createActionProxy("id", hoisted_action)`. A `.bind(...)` member call can be followed if it needs to bind any variables from the closure level. #### 2. Declarations ```js async function named () { "use server" ... } ``` In this case, we replace all the same `named` idents to be the expression `createActionProxy("id", hoisted_action)`, and removed that function declaration. With these changes, these will be fewer structural changes to the AST and the code is more performant. The PR also changes it to use React's `registerServerReference` method directly instead of our in-house implementation inside `createActionProxy`. Another small change is to stabilize the comment header to use `BTreeMap` inside the SWC transform. Otherwise the test snapshots will randomly mismatch. Closes #57392.
Author
Parents
Loading