| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | * |
| 7 | * @flow |
| 8 | */ |
| 9 | |
| 10 | import type {Thenable, ReactDebugInfo} from 'shared/ReactTypes'; |
| 11 | |
| 12 | import type {ImportMetadata} from '../shared/ReactFlightImportMetadata'; |
| 13 | |
| 14 | import { |
| 15 | ID, |
| 16 | NAME, |
| 17 | BUNDLES, |
| 18 | IMPORT_MAP, |
| 19 | } from '../shared/ReactFlightImportMetadata'; |
| 20 | import {prepareDestinationWithChunks} from 'react-client/src/ReactFlightClientConfig'; |
| 21 | |
| 22 | import hasOwnProperty from 'shared/hasOwnProperty'; |
| 23 | |
| 24 | export type ServerManifest = { |
| 25 | [string]: Array<string>, |
| 26 | }; |
| 27 | export type SSRModuleMap = null; |
| 28 | export type ModuleLoading = null; |
| 29 | export type ServerConsumerModuleMap = null; |
| 30 | export type ServerReferenceId = string; |
| 31 | |
| 32 | export opaque type ClientReferenceMetadata = ImportMetadata; |
| 33 | |
| 34 | // eslint-disable-next-line no-unused-vars |
| 35 | export opaque type ClientReference<T> = ImportMetadata; |
| 36 | |
| 37 | export function prepareDestinationForModule( |
| 38 | moduleLoading: ModuleLoading, |
| 39 | nonce: ?string, |
| 40 | metadata: ClientReferenceMetadata, |
| 41 | ) { |
| 42 | prepareDestinationWithChunks(moduleLoading, metadata[BUNDLES], nonce); |
| 43 | } |
| 44 | |
| 45 | export function resolveClientReference<T>( |
| 46 | bundlerConfig: null, |
| 47 | metadata: ClientReferenceMetadata, |
| 48 | ): ClientReference<T> { |
| 49 | // Reference is already resolved during the build. |
| 50 | return metadata; |
| 51 | } |
| 52 | |
| 53 | export function resolveServerReference<T>( |
| 54 | bundlerConfig: ServerManifest, |
| 55 | ref: ServerReferenceId, |
| 56 | ): ClientReference<T> { |
| 57 | const idx = ref.lastIndexOf('#'); |
| 58 | const id = ref.slice(0, idx); |
| 59 | const name = ref.slice(idx + 1); |
| 60 | const bundles = bundlerConfig[id]; |
| 61 | if (!bundles) { |
| 62 | throw new Error('Invalid server action: ' + ref); |
| 63 | } |
| 64 | return [id, name, bundles]; |
| 65 | } |
| 66 | |
| 67 | export function preloadModule<T>( |
| 68 | metadata: ClientReference<T>, |
| 69 | ): null | Thenable<any> { |
| 70 | if (metadata[IMPORT_MAP]) { |
| 71 | parcelRequire.extendImportMap(metadata[IMPORT_MAP]); |
| 72 | } |
| 73 | |
| 74 | if (metadata[BUNDLES].length === 0) { |
| 75 | return null; |
| 76 | } |
| 77 | |
| 78 | return Promise.all(metadata[BUNDLES].map(url => parcelRequire.load(url))); |
| 79 | } |
| 80 | |
| 81 | export function requireModule<T>(metadata: ClientReference<T>): T { |
| 82 | const moduleExports = parcelRequire(metadata[ID]); |
| 83 | if (hasOwnProperty.call(moduleExports, metadata[NAME])) { |
| 84 | return moduleExports[metadata[NAME]]; |
| 85 | } |
| 86 | return undefined as any; |
| 87 | } |
| 88 | |
| 89 | export function getModuleDebugInfo<T>( |
| 90 | metadata: ClientReference<T>, |
| 91 | ): null | ReactDebugInfo { |
| 92 | // TODO |
| 93 | return null; |
| 94 | } |