Unverified Commit c30c14da authored by Donny/강동윤's avatar Donny/강동윤 Committed by GitHub

Improve swc transforms (#45083)

x-ref: https://vercel.slack.com/archives/C02HY34AKME/p1674048645326239

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] [e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent c254b74c
...@@ -59,6 +59,7 @@ pub mod react_server_components; ...@@ -59,6 +59,7 @@ pub mod react_server_components;
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
pub mod relay; pub mod relay;
pub mod remove_console; pub mod remove_console;
pub mod server_actions;
pub mod shake_exports; pub mod shake_exports;
mod top_level_binding_collector; mod top_level_binding_collector;
...@@ -122,6 +123,9 @@ pub struct TransformOptions { ...@@ -122,6 +123,9 @@ pub struct TransformOptions {
#[serde(default)] #[serde(default)]
pub font_loaders: Option<next_font_loaders::Config>, pub font_loaders: Option<next_font_loaders::Config>,
#[serde(default)]
pub server_actions: Option<server_actions::Config>,
} }
pub fn custom_before_pass<'a, C: Comments + 'a>( pub fn custom_before_pass<'a, C: Comments + 'a>(
...@@ -252,6 +256,11 @@ where ...@@ -252,6 +256,11 @@ where
Some(config) => Either::Left(next_font_loaders::next_font_loaders(config.clone())), Some(config) => Either::Left(next_font_loaders::next_font_loaders(config.clone())),
None => Either::Right(noop()), None => Either::Right(noop()),
}, },
match &opts.server_actions {
Some(config) =>
Either::Left(server_actions::server_actions(&file.name, config.clone())),
None => Either::Right(noop()),
},
) )
} }
......
This diff is collapsed.
...@@ -17,6 +17,7 @@ use next_swc::{ ...@@ -17,6 +17,7 @@ use next_swc::{
react_server_components::server_components, react_server_components::server_components,
relay::{relay, Config as RelayConfig, RelayLanguageConfig}, relay::{relay, Config as RelayConfig, RelayLanguageConfig},
remove_console::remove_console, remove_console::remove_console,
server_actions::{self, server_actions},
shake_exports::{shake_exports, Config as ShakeExportsConfig}, shake_exports::{shake_exports, Config as ShakeExportsConfig},
}; };
use std::path::PathBuf; use std::path::PathBuf;
...@@ -295,3 +296,20 @@ fn next_font_loaders_fixture(input: PathBuf) { ...@@ -295,3 +296,20 @@ fn next_font_loaders_fixture(input: PathBuf) {
Default::default(), Default::default(),
); );
} }
#[fixture("tests/fixture/server-actions/**/input.js")]
fn server_actions_fixture(input: PathBuf) {
let output = input.parent().unwrap().join("output.js");
test_fixture(
syntax(),
&|_tr| {
server_actions(
&FileName::Real("/app/item.js".into()),
server_actions::Config {},
)
},
&input,
&output,
Default::default(),
);
}
export function Item({ id1, id2 }) {
async function deleteItem() {
"use action";
await deleteFromDb(id1);
await deleteFromDb(id2);
}
return <Button action={deleteItem}>Delete</Button>;
}
\ No newline at end of file
export function Item({ id1 , id2 }) {
async function deleteItem() {
return $ACTION_deleteItem(deleteItem.$$closure);
}
deleteItem.$$typeof = Symbol.for("react.action.reference");
deleteItem.$$filepath = "/app/item.js";
deleteItem.$$name = "$ACTION_deleteItem";
deleteItem.$$closure = [
id1,
id2
];
return <Button action={deleteItem}>Delete</Button>;
}
export async function $ACTION_deleteItem(closure) {
await deleteFromDb(closure[0]);
await deleteFromDb(closure[1]);
}
async function myAction(a, b, c) {
"use action";
console.log('a')
}
export default function Page() {
return <Button action={myAction}>Delete</Button>;
}
\ No newline at end of file
async function myAction(a, b, c) {
console.log('a');
}
myAction.$$typeof = Symbol.for("react.action.reference");
myAction.$$filepath = "/app/item.js";
myAction.$$name = "$ACTION_myAction";
export const $ACTION_myAction = myAction;
export default function Page() {
return <Button action={myAction}>Delete</Button>;
}
// app/send.ts
"use action";
export async function myAction(a, b, c) {
console.log('a')
}
\ No newline at end of file
// app/send.ts
async function myAction(a, b, c) {
console.log('a');
}
myAction.$$typeof = Symbol.for("react.action.reference");
myAction.$$filepath = "/app/item.js";
myAction.$$name = "$ACTION_myAction";
export const $ACTION_myAction = myAction;
...@@ -70,6 +70,7 @@ fn test(input: &Path, minify: bool) { ...@@ -70,6 +70,7 @@ fn test(input: &Path, minify: bool) {
emotion: Some(assert_json("{}")), emotion: Some(assert_json("{}")),
modularize_imports: None, modularize_imports: None,
font_loaders: None, font_loaders: None,
server_actions: None,
}; };
let options = options.patch(&fm); let options = options.patch(&fm);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment