Files
react/packages/react-server-dom-esm/src/ReactFlightServerConfigESMBundler.js
T
Sebastian Markbåge fdc8c81e07 [Flight] Client and Server Reference Creation into Runtime (#27033)
We already did this for Server References on the Client so this brings
us parity with that. This gives us some more flexibility with changing
the runtime implementation without having to affect the loaders.

We can also do more in the runtime such as adding `.bind()` support to
Server References.

I also moved the CommonJS Proxy creation into the runtime helper from
the register so that it can be handled in one place.

This lets us remove the forks from Next.js since the loaders can be
simplified there to just use these helpers.

This PR doesn't change the protocol or shape of the objects. They're
still specific to each bundler but ideally we should probably move this
to shared helpers that can be used by multiple bundler implementations.
2023-07-07 11:09:45 -04:00

70 lines
1.8 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import type {ReactClientValue} from 'react-server/src/ReactFlightServer';
import type {
ClientReference,
ServerReference,
} from './ReactFlightESMReferences';
export type {ClientReference, ServerReference};
export type ClientManifest = string; // base URL on the file system
export type ServerReferenceId = string;
export type ClientReferenceMetadata = [
string, // module path
string, // export name
];
export type ClientReferenceKey = string;
export {isClientReference, isServerReference} from './ReactFlightESMReferences';
export function getClientReferenceKey(
reference: ClientReference<any>,
): ClientReferenceKey {
return reference.$$id;
}
export function resolveClientReferenceMetadata<T>(
config: ClientManifest,
clientReference: ClientReference<T>,
): ClientReferenceMetadata {
const baseURL: string = config;
const id = clientReference.$$id;
const idx = id.lastIndexOf('#');
const exportName = id.slice(idx + 1);
const fullURL = id.slice(0, idx);
if (!fullURL.startsWith(baseURL)) {
throw new Error(
'Attempted to load a Client Module outside the hosted root.',
);
}
// Relative URL
const modulePath = fullURL.slice(baseURL.length);
return [modulePath, exportName];
}
export function getServerReferenceId<T>(
config: ClientManifest,
serverReference: ServerReference<T>,
): ServerReferenceId {
return serverReference.$$id;
}
export function getServerReferenceBoundArguments<T>(
config: ClientManifest,
serverReference: ServerReference<T>,
): null | Array<ReactClientValue> {
return serverReference.$$bound;
}