Unverified Commit 2fc641ef authored by Shu Ding's avatar Shu Ding Committed by GitHub

Unwrap `createServerReference`, and pass additional parameters (#69190)

For https://github.com/facebook/react/pull/30741

This PR adds several additional parameters to the
`createServerReference` calls generated by the Server Reference SWC
transform, to later enable React to map server actions that are imported
into client components back to their server locations (dev mode only).

Currently the inner `findSourceMapURL` function is not yet implemented.

In a follow-up we will unwrap `registerServerReference` as well, which
is needed for server actions in the react server layer.

---------
Co-authored-by: 's avatarHendrik Liebau <mail@hendrik-liebau.de>
parent 7418b26d
...@@ -833,7 +833,7 @@ impl ReactServerComponentValidator { ...@@ -833,7 +833,7 @@ impl ReactServerComponentValidator {
} }
} }
/// ``` /// ```js
/// import dynamic from 'next/dynamic' /// import dynamic from 'next/dynamic'
/// ///
/// dynamic(() => import(...)) // ✅ /// dynamic(() => import(...)) // ✅
......
...@@ -1345,18 +1345,38 @@ impl<C: Comments> VisitMut for ServerActions<C> { ...@@ -1345,18 +1345,38 @@ impl<C: Comments> VisitMut for ServerActions<C> {
// If it's compiled in the client layer, each export field needs to be // If it's compiled in the client layer, each export field needs to be
// wrapped by a reference creation call. // wrapped by a reference creation call.
let create_ref_ident = private_ident!("createServerReference"); let create_ref_ident = private_ident!("createServerReference");
let call_server_ident = private_ident!("callServer");
let find_source_map_url_ident = private_ident!("findSourceMapURL");
if !self.config.is_react_server_layer { if !self.config.is_react_server_layer {
// import { createServerReference } from // import {
// 'private-next-rsc-action-client-wrapper' // createServerReference,
// callServer,
// findSourceMapURL
// } from 'private-next-rsc-action-client-wrapper'
// createServerReference("action_id") // createServerReference("action_id")
new.push(ModuleItem::ModuleDecl(ModuleDecl::Import(ImportDecl { new.push(ModuleItem::ModuleDecl(ModuleDecl::Import(ImportDecl {
span: DUMMY_SP, span: DUMMY_SP,
specifiers: vec![ImportSpecifier::Named(ImportNamedSpecifier { specifiers: vec![
span: DUMMY_SP, ImportSpecifier::Named(ImportNamedSpecifier {
local: create_ref_ident.clone(), span: DUMMY_SP,
imported: None, local: create_ref_ident.clone(),
is_type_only: false, imported: None,
})], is_type_only: false,
}),
ImportSpecifier::Named(ImportNamedSpecifier {
span: DUMMY_SP,
local: call_server_ident.clone(),
imported: None,
is_type_only: false,
}),
ImportSpecifier::Named(ImportNamedSpecifier {
span: DUMMY_SP,
local: find_source_map_url_ident.clone(),
imported: None,
is_type_only: false,
}),
],
src: Box::new(Str { src: Box::new(Str {
span: DUMMY_SP, span: DUMMY_SP,
value: "private-next-rsc-action-client-wrapper".into(), value: "private-next-rsc-action-client-wrapper".into(),
...@@ -1366,6 +1386,7 @@ impl<C: Comments> VisitMut for ServerActions<C> { ...@@ -1366,6 +1386,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
with: None, with: None,
phase: Default::default(), phase: Default::default(),
}))); })));
new.rotate_right(1);
} }
for (id, export_name) in self.exported_idents.iter() { for (id, export_name) in self.exported_idents.iter() {
...@@ -1386,7 +1407,13 @@ impl<C: Comments> VisitMut for ServerActions<C> { ...@@ -1386,7 +1407,13 @@ impl<C: Comments> VisitMut for ServerActions<C> {
callee: Callee::Expr(Box::new(Expr::Ident( callee: Callee::Expr(Box::new(Expr::Ident(
create_ref_ident.clone(), create_ref_ident.clone(),
))), ))),
args: vec![action_id.as_arg()], args: vec![
action_id.as_arg(),
call_server_ident.clone().as_arg(),
Expr::undefined(DUMMY_SP).as_arg(),
find_source_map_url_ident.clone().as_arg(),
"default".as_arg(),
],
..Default::default() ..Default::default()
})), })),
}, },
...@@ -1410,7 +1437,13 @@ impl<C: Comments> VisitMut for ServerActions<C> { ...@@ -1410,7 +1437,13 @@ impl<C: Comments> VisitMut for ServerActions<C> {
callee: Callee::Expr(Box::new(Expr::Ident( callee: Callee::Expr(Box::new(Expr::Ident(
create_ref_ident.clone(), create_ref_ident.clone(),
))), ))),
args: vec![action_id.as_arg()], args: vec![
action_id.as_arg(),
call_server_ident.clone().as_arg(),
Expr::undefined(DUMMY_SP).as_arg(),
find_source_map_url_ident.clone().as_arg(),
export_name.clone().as_arg(),
],
..Default::default() ..Default::default()
}))), }))),
definite: false, definite: false,
...@@ -1520,11 +1553,6 @@ impl<C: Comments> VisitMut for ServerActions<C> { ...@@ -1520,11 +1553,6 @@ impl<C: Comments> VisitMut for ServerActions<C> {
text: generate_server_actions_comment(actions).into(), text: generate_server_actions_comment(actions).into(),
}, },
); );
if !self.config.is_react_server_layer {
// Make it the first item
new.rotate_right(1);
}
} }
// import { cache as $cache } from "private-next-rsc-cache-wrapper"; // import { cache as $cache } from "private-next-rsc-cache-wrapper";
......
/* __next_internal_client_entry_do_not_use__ default auto */ /* __next_internal_action_entry_do_not_use__ {"6a88810ecce4a4e8b59d53b8327d7e98bbf251d7":"$$RSC_SERVER_ACTION_0"} */ export async function $$RSC_SERVER_ACTION_0() {} /* __next_internal_client_entry_do_not_use__ default auto */ /* __next_internal_action_entry_do_not_use__ {"6a88810ecce4a4e8b59d53b8327d7e98bbf251d7":"$$RSC_SERVER_ACTION_0"} */ export default function App() {
export default function App() {
var fn = registerServerReference("6a88810ecce4a4e8b59d53b8327d7e98bbf251d7", $$RSC_SERVER_ACTION_0); var fn = registerServerReference("6a88810ecce4a4e8b59d53b8327d7e98bbf251d7", $$RSC_SERVER_ACTION_0);
return <div>App</div>; return <div>App</div>;
} }
export async function $$RSC_SERVER_ACTION_0() {}
// app/send.ts // app/send.ts
/* __next_internal_action_entry_do_not_use__ {"c18c215a6b7cdc64bf709f3a714ffdef1bf9651d":"default","e10665baac148856374b2789aceb970f66fec33e":"myAction"} */ export default /*#__PURE__*/ createServerReference("c18c215a6b7cdc64bf709f3a714ffdef1bf9651d"); /* __next_internal_action_entry_do_not_use__ {"c18c215a6b7cdc64bf709f3a714ffdef1bf9651d":"default","e10665baac148856374b2789aceb970f66fec33e":"myAction"} */ import { createServerReference, callServer, findSourceMapURL } from "private-next-rsc-action-client-wrapper";
import { createServerReference } from "private-next-rsc-action-client-wrapper"; export var myAction = /*#__PURE__*/ createServerReference("e10665baac148856374b2789aceb970f66fec33e", callServer, void 0, findSourceMapURL, "myAction");
export var myAction = /*#__PURE__*/ createServerReference("e10665baac148856374b2789aceb970f66fec33e"); export default /*#__PURE__*/ createServerReference("c18c215a6b7cdc64bf709f3a714ffdef1bf9651d", callServer, void 0, findSourceMapURL, "default");
// app/send.ts // app/send.ts
/* __next_internal_action_entry_do_not_use__ {"ab21efdafbe611287bc25c0462b1e0510d13e48b":"foo"} */ export var foo = /*#__PURE__*/ createServerReference("ab21efdafbe611287bc25c0462b1e0510d13e48b"); /* __next_internal_action_entry_do_not_use__ {"ab21efdafbe611287bc25c0462b1e0510d13e48b":"foo"} */ import { createServerReference, callServer, findSourceMapURL } from "private-next-rsc-action-client-wrapper";
import { createServerReference } from "private-next-rsc-action-client-wrapper"; export var foo = /*#__PURE__*/ createServerReference("ab21efdafbe611287bc25c0462b1e0510d13e48b", callServer, void 0, findSourceMapURL, "foo");
/* __next_internal_action_entry_do_not_use__ {"a0c73dd6f5af839e3335c6b19262ecb86cca6af4":"sampleFunction2","bd336abe00c3c59da66acb696fc8e151d8e54ea4":"sampleFunction","d4f2e95bc745b6500b439c0847003511748c8ece":"sampleFunction3","f03b256ee88a51700367acee3082894e25e6e7d9":"sampleFunction4"} */ export var sampleFunction4 = /*#__PURE__*/ createServerReference("f03b256ee88a51700367acee3082894e25e6e7d9"); /* __next_internal_action_entry_do_not_use__ {"a0c73dd6f5af839e3335c6b19262ecb86cca6af4":"sampleFunction2","bd336abe00c3c59da66acb696fc8e151d8e54ea4":"sampleFunction","d4f2e95bc745b6500b439c0847003511748c8ece":"sampleFunction3","f03b256ee88a51700367acee3082894e25e6e7d9":"sampleFunction4"} */ import { createServerReference, callServer, findSourceMapURL } from "private-next-rsc-action-client-wrapper";
import { createServerReference } from "private-next-rsc-action-client-wrapper"; export var sampleFunction = /*#__PURE__*/ createServerReference("bd336abe00c3c59da66acb696fc8e151d8e54ea4", callServer, void 0, findSourceMapURL, "sampleFunction");
export var sampleFunction = /*#__PURE__*/ createServerReference("bd336abe00c3c59da66acb696fc8e151d8e54ea4"); export var sampleFunction2 = /*#__PURE__*/ createServerReference("a0c73dd6f5af839e3335c6b19262ecb86cca6af4", callServer, void 0, findSourceMapURL, "sampleFunction2");
export var sampleFunction2 = /*#__PURE__*/ createServerReference("a0c73dd6f5af839e3335c6b19262ecb86cca6af4"); export var sampleFunction3 = /*#__PURE__*/ createServerReference("d4f2e95bc745b6500b439c0847003511748c8ece", callServer, void 0, findSourceMapURL, "sampleFunction3");
export var sampleFunction3 = /*#__PURE__*/ createServerReference("d4f2e95bc745b6500b439c0847003511748c8ece"); export var sampleFunction4 = /*#__PURE__*/ createServerReference("f03b256ee88a51700367acee3082894e25e6e7d9", callServer, void 0, findSourceMapURL, "sampleFunction4");
/* __next_internal_action_entry_do_not_use__ {"ac840dcaf5e8197cb02b7f3a43c119b7a770b272":"bar"} */ export var bar = /*#__PURE__*/ createServerReference("ac840dcaf5e8197cb02b7f3a43c119b7a770b272"); /* __next_internal_action_entry_do_not_use__ {"ac840dcaf5e8197cb02b7f3a43c119b7a770b272":"bar"} */ import { createServerReference, callServer, findSourceMapURL } from "private-next-rsc-action-client-wrapper";
import { createServerReference } from "private-next-rsc-action-client-wrapper"; export var bar = /*#__PURE__*/ createServerReference("ac840dcaf5e8197cb02b7f3a43c119b7a770b272", callServer, void 0, findSourceMapURL, "bar");
/* __next_internal_action_entry_do_not_use__ {"0090eaf4e1f08a2d94f6be401e54a2ded399b87c":"action0","1c36b06e398c97abe5d5d7ae8c672bfddf4e1b91":"$$RSC_SERVER_ACTION_2","6a88810ecce4a4e8b59d53b8327d7e98bbf251d7":"$$RSC_SERVER_ACTION_0","90b5db271335765a4b0eab01f044b381b5ebd5cd":"$$RSC_SERVER_ACTION_1"} */ export var action0 = /*#__PURE__*/ createServerReference("0090eaf4e1f08a2d94f6be401e54a2ded399b87c"); /* __next_internal_action_entry_do_not_use__ {"0090eaf4e1f08a2d94f6be401e54a2ded399b87c":"action0","1c36b06e398c97abe5d5d7ae8c672bfddf4e1b91":"$$RSC_SERVER_ACTION_2","6a88810ecce4a4e8b59d53b8327d7e98bbf251d7":"$$RSC_SERVER_ACTION_0","90b5db271335765a4b0eab01f044b381b5ebd5cd":"$$RSC_SERVER_ACTION_1"} */ import { createServerReference, callServer, findSourceMapURL } from "private-next-rsc-action-client-wrapper";
import { createServerReference } from "private-next-rsc-action-client-wrapper"; export var action0 = /*#__PURE__*/ createServerReference("0090eaf4e1f08a2d94f6be401e54a2ded399b87c", callServer, void 0, findSourceMapURL, "action0");
// This file must be bundled in the app's client layer, it shouldn't be directly // This file must be bundled in the app's client layer, it shouldn't be directly
// imported by the server. // imported by the server.
import { callServer } from 'next/dist/client/app-call-server' export { callServer } from 'next/dist/client/app-call-server'
// A noop wrapper to let the Flight client create the server reference. // A noop wrapper to let the Flight client create the server reference.
// See also: https://github.com/facebook/react/pull/26632 // See also: https://github.com/facebook/react/pull/26632
export function createServerReference(id: string) { // Since we're using the Edge build of Flight client for SSR [1], here we need to
// Since we're using the Edge build of Flight client for SSR [1], here we need to // also use the same Edge build to create the reference. For the client bundle,
// also use the same Edge build to create the reference. For the client bundle, // we use the default and let Webpack to resolve it to the correct version.
// we use the default and let Webpack to resolve it to the correct version. // 1: https://github.com/vercel/next.js/blob/16eb80b0b0be13f04a6407943664b5efd8f3d7d0/packages/next/src/server/app-render/use-flight-response.tsx#L24-L26
// 1: https://github.com/vercel/next.js/blob/16eb80b0b0be13f04a6407943664b5efd8f3d7d0/packages/next/src/server/app-render/use-flight-response.tsx#L24-L26 export const createServerReference = (
const { createServerReference: createServerReferenceImpl } = ( (!!process.env.NEXT_RUNTIME
!!process.env.NEXT_RUNTIME ? // eslint-disable-next-line import/no-extraneous-dependencies
? // eslint-disable-next-line import/no-extraneous-dependencies require('react-server-dom-webpack/client.edge')
require('react-server-dom-webpack/client.edge') : // eslint-disable-next-line import/no-extraneous-dependencies
: // eslint-disable-next-line import/no-extraneous-dependencies require('react-server-dom-webpack/client')) as typeof import('react-server-dom-webpack/client')
require('react-server-dom-webpack/client') ).createServerReference
) as typeof import('react-server-dom-webpack/client')
return createServerReferenceImpl(id, callServer) export const findSourceMapURL = undefined
}
...@@ -18,10 +18,10 @@ const flightServerReferenceProxyLoader: webpack.LoaderDefinitionFunction<{ ...@@ -18,10 +18,10 @@ const flightServerReferenceProxyLoader: webpack.LoaderDefinitionFunction<{
// Because of that, Webpack is able to concatenate the modules and inline the // Because of that, Webpack is able to concatenate the modules and inline the
// reference IDs recursively directly into the module that uses them. // reference IDs recursively directly into the module that uses them.
return `\ return `\
import { createServerReference } from 'private-next-rsc-action-client-wrapper' import { createServerReference, callServer, findSourceMapURL } from 'private-next-rsc-action-client-wrapper'
export ${ export ${
name === 'default' ? 'default' : `const ${name} =` name === 'default' ? 'default' : `const ${name} =`
} /*#__PURE__*/createServerReference(${JSON.stringify(id)})` } /*#__PURE__*/createServerReference(${JSON.stringify(id)}, callServer, undefined, findSourceMapURL, ${JSON.stringify(name)})`
} }
export default flightServerReferenceProxyLoader export default flightServerReferenceProxyLoader
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