-
Notifications
You must be signed in to change notification settings - Fork 26.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve the Server Actions SWC transform (#61001)
This PR improves the Server Actions SWC transform to make it able to handle nested Action declarations (check `fixture/server-actions/server/28/input.js` for more details). It is also a simplification of that transform's internal states and methods, with the removal of an extra AST pass (`stmts.visit_mut_with(&mut ClosureActionReplacer { replaced_action_proxies: &self.replaced_action_proxies, })`). The generated code is also smaller in some cases. So overall I'd expect the compilation and runtime performance to improve as well. ## Details With this change, we're now using `self.declared_idents` and `self.names` to track closure arguments. `declared_idents` keeps the identifiers **declared** in the current closure and above. `names` keeps identifiers **appeared** in the current closure and above. In an example of the following cursor: ```ts let x async function foo() { "use server" let y async function bar() { "use server" let z console.log(x, y, z) } // <- cursor } ``` `declared_idents` would be `y` (`x` isn't in a closure), and `names` would be `x, y, z`. By manipulating these two states we're able to track closure closed-up variables recursively. Closes NEXT-2189
- Loading branch information
Showing
23 changed files
with
284 additions
and
326 deletions.
There are no files selected for viewing
358 changes: 140 additions & 218 deletions
358
packages/next-swc/crates/next-custom-transforms/src/transforms/server_actions.rs
Large diffs are not rendered by default.
Oops, something went wrong.
3 changes: 2 additions & 1 deletion
3
...xt-swc/crates/next-custom-transforms/tests/errors/server-actions/server-graph/6/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
/* __next_internal_action_entry_do_not_use__ {} */ import { createActionProxy } from "private-next-rsc-action-proxy"; | ||
import { encryptActionBoundArgs, decryptActionBoundArgs } from "private-next-rsc-action-encryption"; | ||
export default (()=>{}); | ||
export default createActionProxy("6d53ce510b2e36499b8f56038817b9bad86cabb4", $$ACTION_0); | ||
export async function $$ACTION_0() {} | ||
import { ensureServerEntryExports } from "private-next-rsc-action-validate"; | ||
ensureServerEntryExports([]); |
6 changes: 3 additions & 3 deletions
6
...xt-swc/crates/next-custom-transforms/tests/errors/server-actions/server-graph/7/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
/* __next_internal_action_entry_do_not_use__ {"188d5d945750dc32e2c842b93c75a65763d4a922":"$$ACTION_1"} */ import { createActionProxy } from "private-next-rsc-action-proxy"; | ||
/* __next_internal_action_entry_do_not_use__ {"6d53ce510b2e36499b8f56038817b9bad86cabb4":"$$ACTION_0"} */ import { createActionProxy } from "private-next-rsc-action-proxy"; | ||
import { encryptActionBoundArgs, decryptActionBoundArgs } from "private-next-rsc-action-encryption"; | ||
const foo = createActionProxy("188d5d945750dc32e2c842b93c75a65763d4a922", $$ACTION_1); | ||
export async function $$ACTION_1() {} | ||
const foo = createActionProxy("6d53ce510b2e36499b8f56038817b9bad86cabb4", $$ACTION_0); | ||
export async function $$ACTION_0() {} |
6 changes: 3 additions & 3 deletions
6
...xt-swc/crates/next-custom-transforms/tests/errors/server-actions/server-graph/8/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 4 additions & 3 deletions
7
...s/next-swc/crates/next-custom-transforms/tests/fixture/server-actions/server/15/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 10 additions & 10 deletions
20
...s/next-swc/crates/next-custom-transforms/tests/fixture/server-actions/server/16/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,36 @@ | ||
/* __next_internal_action_entry_do_not_use__ {"188d5d945750dc32e2c842b93c75a65763d4a922":"$$ACTION_1","9878bfa39811ca7650992850a8751f9591b6a557":"$$ACTION_2","9c0dd1f7c2b3f41d32e10f5c437de3d67ad32c6c":"$$ACTION_4"} */ import { createActionProxy } from "private-next-rsc-action-proxy"; | ||
/* __next_internal_action_entry_do_not_use__ {"188d5d945750dc32e2c842b93c75a65763d4a922":"$$ACTION_1","6d53ce510b2e36499b8f56038817b9bad86cabb4":"$$ACTION_0","9878bfa39811ca7650992850a8751f9591b6a557":"$$ACTION_2"} */ import { createActionProxy } from "private-next-rsc-action-proxy"; | ||
import { encryptActionBoundArgs, decryptActionBoundArgs } from "private-next-rsc-action-encryption"; | ||
import deleteFromDb from 'db'; | ||
const v1 = 'v1'; | ||
export function Item({ id1, id2 }) { | ||
const v2 = id2; | ||
const deleteItem = createActionProxy("188d5d945750dc32e2c842b93c75a65763d4a922", $$ACTION_1).bind(null, encryptActionBoundArgs("188d5d945750dc32e2c842b93c75a65763d4a922", [ | ||
const deleteItem = createActionProxy("6d53ce510b2e36499b8f56038817b9bad86cabb4", $$ACTION_0).bind(null, encryptActionBoundArgs("6d53ce510b2e36499b8f56038817b9bad86cabb4", [ | ||
id1, | ||
v2 | ||
])); | ||
return <Button action={deleteItem}>Delete</Button>; | ||
} | ||
export async function $$ACTION_1($$ACTION_CLOSURE_BOUND) { | ||
var [$$ACTION_ARG_0, $$ACTION_ARG_1] = await decryptActionBoundArgs("188d5d945750dc32e2c842b93c75a65763d4a922", $$ACTION_CLOSURE_BOUND); | ||
export async function $$ACTION_0($$ACTION_CLOSURE_BOUND) { | ||
var [$$ACTION_ARG_0, $$ACTION_ARG_1] = await decryptActionBoundArgs("6d53ce510b2e36499b8f56038817b9bad86cabb4", $$ACTION_CLOSURE_BOUND); | ||
await deleteFromDb($$ACTION_ARG_0); | ||
await deleteFromDb(v1); | ||
await deleteFromDb($$ACTION_ARG_1); | ||
} | ||
const f = (x)=>{ | ||
var g = createActionProxy("9878bfa39811ca7650992850a8751f9591b6a557", $$ACTION_2).bind(null, encryptActionBoundArgs("9878bfa39811ca7650992850a8751f9591b6a557", [ | ||
var g = createActionProxy("188d5d945750dc32e2c842b93c75a65763d4a922", $$ACTION_1).bind(null, encryptActionBoundArgs("188d5d945750dc32e2c842b93c75a65763d4a922", [ | ||
x | ||
])); | ||
}; | ||
export async function $$ACTION_2($$ACTION_CLOSURE_BOUND, y, ...z) { | ||
var [$$ACTION_ARG_0] = await decryptActionBoundArgs("9878bfa39811ca7650992850a8751f9591b6a557", $$ACTION_CLOSURE_BOUND); | ||
export async function $$ACTION_1($$ACTION_CLOSURE_BOUND, y, ...z) { | ||
var [$$ACTION_ARG_0] = await decryptActionBoundArgs("188d5d945750dc32e2c842b93c75a65763d4a922", $$ACTION_CLOSURE_BOUND); | ||
return $$ACTION_ARG_0 + y + z[0]; | ||
} | ||
const g = (x)=>{ | ||
f = createActionProxy("9c0dd1f7c2b3f41d32e10f5c437de3d67ad32c6c", $$ACTION_4).bind(null, encryptActionBoundArgs("9c0dd1f7c2b3f41d32e10f5c437de3d67ad32c6c", [ | ||
f = createActionProxy("9878bfa39811ca7650992850a8751f9591b6a557", $$ACTION_2).bind(null, encryptActionBoundArgs("9878bfa39811ca7650992850a8751f9591b6a557", [ | ||
x | ||
])); | ||
}; | ||
export async function $$ACTION_4($$ACTION_CLOSURE_BOUND, y, ...z) { | ||
var [$$ACTION_ARG_0] = await decryptActionBoundArgs("9c0dd1f7c2b3f41d32e10f5c437de3d67ad32c6c", $$ACTION_CLOSURE_BOUND); | ||
export async function $$ACTION_2($$ACTION_CLOSURE_BOUND, y, ...z) { | ||
var [$$ACTION_ARG_0] = await decryptActionBoundArgs("9878bfa39811ca7650992850a8751f9591b6a557", $$ACTION_CLOSURE_BOUND); | ||
return $$ACTION_ARG_0 + y + z[0]; | ||
} |
3 changes: 2 additions & 1 deletion
3
...s/next-swc/crates/next-custom-transforms/tests/fixture/server-actions/server/17/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
...es/next-swc/crates/next-custom-transforms/tests/fixture/server-actions/server/2/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
/* __next_internal_action_entry_do_not_use__ {"6d53ce510b2e36499b8f56038817b9bad86cabb4":"$$ACTION_0","9878bfa39811ca7650992850a8751f9591b6a557":"$$ACTION_2"} */ import { createActionProxy } from "private-next-rsc-action-proxy"; | ||
/* __next_internal_action_entry_do_not_use__ {"188d5d945750dc32e2c842b93c75a65763d4a922":"$$ACTION_1","6d53ce510b2e36499b8f56038817b9bad86cabb4":"$$ACTION_0"} */ import { createActionProxy } from "private-next-rsc-action-proxy"; | ||
import { encryptActionBoundArgs, decryptActionBoundArgs } from "private-next-rsc-action-encryption"; | ||
var myAction = createActionProxy("6d53ce510b2e36499b8f56038817b9bad86cabb4", $$ACTION_0); | ||
export async function $$ACTION_0(a, b, c) { | ||
console.log('a'); | ||
} | ||
export default function Page() { | ||
return <Button action={createActionProxy("6d53ce510b2e36499b8f56038817b9bad86cabb4", $$ACTION_0)}>Delete</Button>; | ||
return <Button action={myAction}>Delete</Button>; | ||
} | ||
export const action = withValidate(createActionProxy("9878bfa39811ca7650992850a8751f9591b6a557", $$ACTION_2)); | ||
export async function $$ACTION_2() {} | ||
export const action = withValidate(createActionProxy("188d5d945750dc32e2c842b93c75a65763d4a922", $$ACTION_1)); | ||
export async function $$ACTION_1() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 7 additions & 5 deletions
12
...s/next-swc/crates/next-custom-transforms/tests/fixture/server-actions/server/22/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
/* __next_internal_action_entry_do_not_use__ {"c18c215a6b7cdc64bf709f3a714ffdef1bf9651d":"default","f14702b5a021dd117f7ec7a3c838f397c2046d3b":"action"} */ import { createActionProxy } from "private-next-rsc-action-proxy"; | ||
import { encryptActionBoundArgs, decryptActionBoundArgs } from "private-next-rsc-action-encryption"; | ||
import { validator } from 'auth'; | ||
export const action = validator(async ()=>{}); | ||
export default $$ACTION_0 = validator(async ()=>{}); | ||
var $$ACTION_0; | ||
export const action = validator(createActionProxy("6d53ce510b2e36499b8f56038817b9bad86cabb4", $$ACTION_0)); | ||
export async function $$ACTION_0() {} | ||
export default $$ACTION_1 = validator(createActionProxy("9878bfa39811ca7650992850a8751f9591b6a557", $$ACTION_2)); | ||
var $$ACTION_1; | ||
export async function $$ACTION_2() {} | ||
import { ensureServerEntryExports } from "private-next-rsc-action-validate"; | ||
ensureServerEntryExports([ | ||
action, | ||
$$ACTION_0 | ||
$$ACTION_1 | ||
]); | ||
createActionProxy("f14702b5a021dd117f7ec7a3c838f397c2046d3b", action); | ||
createActionProxy("c18c215a6b7cdc64bf709f3a714ffdef1bf9651d", $$ACTION_0); | ||
createActionProxy("c18c215a6b7cdc64bf709f3a714ffdef1bf9651d", $$ACTION_1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.