|
| 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 strict-local |
| 8 | + * @format |
| 9 | + */ |
| 10 | + |
| 11 | +const {REACT_NATIVE_PACKAGE_DIR} = require('../../../shared/consts'); |
| 12 | +const path = require('node:path'); |
| 13 | + |
| 14 | +const REACT_PRIVATE_INTERFACE_IMPORT = 'react-native/react-private-interface'; |
| 15 | + |
| 16 | +const REACT_PRIVATE_INTERFACE_FILES: Set<string> = new Set([ |
| 17 | + path.join(REACT_NATIVE_PACKAGE_DIR, 'src/react-private-interface.js'), |
| 18 | + path.join(REACT_NATIVE_PACKAGE_DIR, 'src/react-private-interface.js.flow'), |
| 19 | +]); |
| 20 | + |
| 21 | +function isReactPrivateInterfaceImport(importPath: string): boolean { |
| 22 | + return importPath === REACT_PRIVATE_INTERFACE_IMPORT; |
| 23 | +} |
| 24 | + |
| 25 | +function isReactPrivateInterfaceFile(filePath: string): boolean { |
| 26 | + return REACT_PRIVATE_INTERFACE_FILES.has(path.resolve(filePath)); |
| 27 | +} |
| 28 | + |
| 29 | +function isPrivateSourceDependency(depFile: string): boolean { |
| 30 | + const relativeFromPackage = path.relative( |
| 31 | + REACT_NATIVE_PACKAGE_DIR, |
| 32 | + path.resolve(depFile), |
| 33 | + ); |
| 34 | + |
| 35 | + if (relativeFromPackage === '' || relativeFromPackage.startsWith('..')) { |
| 36 | + return false; |
| 37 | + } |
| 38 | + |
| 39 | + const privatePrefix = 'src' + path.sep + 'private' + path.sep; |
| 40 | + return ( |
| 41 | + relativeFromPackage === 'src' + path.sep + 'private' || |
| 42 | + relativeFromPackage.startsWith(privatePrefix) |
| 43 | + ); |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * `react-private-interface` is a runtime-only package export (`"types": null`). |
| 48 | + * Do not expand its `src/private/*` dependencies into generated types. |
| 49 | + */ |
| 50 | +function shouldExpandDependency(fromFile: string, depFile: string): boolean { |
| 51 | + if (!isReactPrivateInterfaceFile(fromFile)) { |
| 52 | + return true; |
| 53 | + } |
| 54 | + return !isPrivateSourceDependency(depFile); |
| 55 | +} |
| 56 | + |
| 57 | +function isPrivateModuleSpecifier(specifier: string): boolean { |
| 58 | + return ( |
| 59 | + specifier.startsWith('./private/') || specifier.startsWith('../private/') |
| 60 | + ); |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Minimal exports map for the api-extractor temp layout so TypeScript does not |
| 65 | + * treat `react-private-interface` as a typed entry point. |
| 66 | + */ |
| 67 | +const API_EXTRACTOR_TEMP_EXPORTS = { |
| 68 | + '.': { |
| 69 | + types: './react-native/index.d.ts', |
| 70 | + }, |
| 71 | + './react-private-interface': { |
| 72 | + types: null, |
| 73 | + default: './react-native/src/react-private-interface.js', |
| 74 | + }, |
| 75 | +}; |
| 76 | + |
| 77 | +module.exports = { |
| 78 | + API_EXTRACTOR_TEMP_EXPORTS, |
| 79 | + isPrivateModuleSpecifier, |
| 80 | + isReactPrivateInterfaceFile, |
| 81 | + isReactPrivateInterfaceImport, |
| 82 | + shouldExpandDependency, |
| 83 | +}; |
0 commit comments